Go to Top

Saturday 2 July 2011

To print and find the sum of a given series

Question 15 : Write a program in Java to print and find the sum of the given series-


S = 1+2+4+7+11+......+n


Java Program : 



public class series_1
{
 public static void main(int n)
 {
     int i,d;
     int s=1;
     d=0;
     System.out.println("The generated numbers are : ");
     for(i=0;i<=n-1;i++)
     {
         s=s+i;
         d=d+s;
         System.out.println(s);
        }
    System.out.println("\nThe sum of the generated numbers = "+d);
}
}

No comments:

Post a Comment

ShareThis