# Series generation Program

Discussion in 'Java' started by angad_aks, Feb 15, 2011.

Joined:
Feb 15, 2011
Messages:
15
0
Trophy Points:
0
Code:
import java.io.*;
class series2
{
public void main()throws IOException
{
int ch,n,i,j;
System.out.println("enter value of n:"); //asking for a number from the user
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:");
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");
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");
}
}
}