Series generation Program

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

  1. angad_aks

    angad_aks Banned

    Joined:
    Feb 15, 2011
    Messages:
    15
    Likes Received:
    0
    Trophy Points:
    0
    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");
    }
    }
    } 
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice