Go4Expert

Go4Expert (http://www.go4expert.com/)
-   Java (http://www.go4expert.com/forums/java/)
-   -   Series generation Program (http://www.go4expert.com/forums/series-generation-program-t24992/)

angad_aks 16Feb2011 01:21

Series generation Program
 
Code:

import java.io.*;
class series2
{
InputStreamReader read=new InputStreamReader(System.in);
BufferedReader in=new BufferedReader(read);
public void main()throws IOException
{
int ch,n,i,j;
System.out.println("enter value of n:"); //asking for a number from the user
n=Integer.parseInt(in.readLine());
System.out.println("Menu:"); //asking the user to choose a series pattern
System.out.println("1. s=a+(a^2)/2!+.......n terms");
System.out.println("2. 3,8,15,24,35,....n terms");
System.out.println("3. 1,4,9,16,25,....n terms");
System.out.println("4. x/3 + x/6 + x/11 +....n terms");
System.out.println("enter choice");
ch=Integer.parseInt(in.readLine()); //storing the choice
switch(ch)
{
case 1:
int a,f;
double s=0;
System.out.println("Enter value of a:");
a=Integer.parseInt(in.readLine());
for(i=1;i<=n;i++)
{
f=1;
for(j=1;j<=n;j++)
{ f=f*j;}
s=s+(Math.pow(a,i)/f);
}
System.out.println("The sum is:"+s);
break;
case 2:
for(i=2;i<=n;i++)
{System.out.print((i*i)-1+" ,");}
break;
case 3:
for(i=1;i<=n;i++)
{System.out.print((i*i)+" ,");}
break;
case 4:
int x,k;
int sum=0;
System.out.println("Enter the value of x");
x=Integer.parseInt(in.readLine());
k=3;
for(i=1;i<=n;i++)
{
sum=sum+(x/k);
k=k+2;
}
System.out.println("The sum is:"+sum);
break;
default:
System.out.println("Wrong choice");
}
}
}



All times are GMT +5.5. The time now is 22:45.