Go to Top

Friday, 29 July 2011

To print and find the sum of a given series

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

S = 1+11+111+1111+11111+......+(11111...n)



Java Program :



class series_3
{
 int n,sum=1;
 series_3(int a)
 {
     n=a;
     cal();
    }
    void cal()
    {
        int s=1;
        System.out.print("The series : 1+");
        for(int i=2;i<=n;i++)
        {
            s=(s*10)+1;
            sum=sum+s;
            if(i==n)
            {
                System.out.print(s);
                break;
            }
            System.out.print(s+"+");
        }
        System.out.print("\nSum of the series : "+sum);
    }
}

1 comment:

ShareThis