WAP to print the sum of 2 + 22 + 222 + … + nth term

Write a program to print the sum of 2 + 22 + 222 + … + nth, up to nth term

#include<stdio.h>

int main() {
       int sum=1, term=1, n=0;
	   printf("Enter term: ");
	   scanf("%d",&n);
       for(int i = 2; i<=n; i++) {
              term = term*10 + 1;
              sum+= term;
       }
       printf("Sum = %d",sum*2);
       return 0;
}

Output

Enter term: 4

Sum = 2468

Backend

2 + 22 + 222 + 2222 = 2468

0 0 votes
Article Rating
Subscribe
Notify of
0 Comments
Inline Feedbacks
View all comments