RSS
email
0

Count the lines, words and characters in a File



import java.io.*;
import javax.swing.JOptionPane;
import java.util.*;
class Count
{
public static void main(String[] args) throws IOException
{
String s;
int i=0,count=0,l=0;
s=JOptionPane.showInputDialog("enter a file name");
FileInputStream f=new FileInputStream(s);
DataInputStream d=new DataInputStream(f);
while(d.readLine()!=null)
{
i++;
StringTokenizer s1=new StringTokenizer(d.readLine());
while(s1.hasMoreTokens())
{
String b=s1.nextToken();
count++;
l=l+b.length();
}
}
JOptionPane.showMessageDialog(null,"words="+count+"\nlines="+i+"\n characters="+l,"Message",JOptionPane.INFORMATION_MESSAGE);
}
}
Read more>>
0

Calculation of Product of Two Numbers using Function - Returns a Float

This program seems to be rather simple. But there's one little thing to be noted in this particular program. The thing is that the function in this program returns a float. The function declaration is usually given outside main..but due to some other standards that I am following, I have prototyed it inside main..but that doesn't cause much of a difference in this simple program.


Write a function which receives a float and an int from main(), finds the product of these two and returns the product which is printed through main().

#include
main()
{

int i;
float j, prod;
float product (int x, float y);

printf("Enter the i(int) and j(float):");
scanf ("%d %f", &i, &j);

prod = product(i,j);

printf("Product:%f", prod);

}


product (int x, float y)
{

float product;
product = x*y;
return (product);

}
Read more>>
0

Calculation of Sum, Average and Standard Deviation using Functions and Pointers.

Write a function that receives 5 integers and returns the sum, average and standard deviation of these numbers. Call this function from main() and print the results in main().
  

#include<stdio.h>
#include<math.h>

int calc (float a, float b, float c, float d, float e, float *sum, float *avg,float *sd);

int main()
{
float a, b, c, d, e, sum=0.0, avg=0.0;

float sd=0.0;

printf("Enter Five Numbers:");
scanf("%f %f %f %f %f",&a,&b,&c,&d,&e);

calc (a, b, c, d, e, &sum, &amp;avg, &sd);


printf("\nSum=%f", sum);
printf("\nAverage=%f", avg);

printf("\nStandard Deviation=%f\n", sd);


getchar();

return 0;
}


calc (float a, float b, float c, float d, float e, float *sum, float *avg, float *sd)

{
float Calc=0.0;

*sum = a+b+c+d+e;

*avg = *sum / 5.0;

Calc += ( a - *avg) * ( a - *avg);

Calc += ( b - *avg) * ( b - *avg);

Calc += ( c - *avg) * ( c - *avg);
Calc += ( d - *avg) * ( d - *avg);

Calc += ( e - *avg) * ( e - *avg);


*sd = sqrt((double)Calc/5.0);

}
Read more>>
0

Fibonacci Sequence using Recursion

Write a recursive function to obtain the first 25 numbers of a Fibonacci sequence. In a Fibonacci sequence the sum of two successive terms gives the third term. Following are the first few terms of the Fibonacci sequence:

1 1 2 3 5 8 13 21 34 55 89 ...

  
#include<stdio.h>
main()
static int prev_number=0, number=1; // static: so value is not lost

int fibonacci (int prev_number, int number);

printf ("Following are the first 25 Numbers of the Fibonacci Series:\n");

printf ("1 "); //to avoid complexity

fibonacci (prev_number,number);
 }


fibonacci (int prev_number, int number)
 {
static int i=1; //i is not 0, cuz 1 is already counted in main.
int fibo;


if (i==25)
{
printf ("\ndone"); //stop after 25 numbers

}

else
{
fibo=prev_number+number;
prev_number=number; //important steps

number=fibo;

printf ("\n%d", fibo);
i++; // increment counter

fibonacci (prev_number,number); //recursion

}

}
Read more>>
0

Calculate Factorial of a number using Recursion

Write a program to calculate the factorial of a number. Use the concept of recursion
instead of using loops.


#include<stdio.h>

void main()

{
int a, fact;

printf("\nEnter any number: ");

scanf ("%d", &a);

fact=rec (a);

printf("\nFactorial Value = %d", fact);

}

rec (int x)
{
int f;
if (x==1)
return (1);
else
f=x*rec(x-1);
return (f);
}

Output:
Enter any number:3
Factorial Value = 6
Read more>>
 

Categories