Wednesday, 14 October 2020

Find the sum of the series 1+11+111+1111+….. up to n terms

 1+11+111+1111+….. up to n terms

Input : 4
Output : 1 + 11 + 111 + 1111 +.....
Total sum is : 1234


import java.io.*; 
 class series1_11_111 

      public static void main(String args[]) 
    { 
       { 
        int n = 4; 
        int sum = 0, j = 1; 
        for (int i = 1; i <= n; i++)  
        { 
            sum = sum + j; 
            j = (j * 10) + 1; 
        } 
          System.out.println("Sum="+sum);
       }    
    } 


Output:
Sum=1234

No comments:

Post a Comment