QUESTION:- Write a program in C to display the sum of the series [ 9 + 99 + 999 + 9999 ...].
Test Data :
Input the number or terms :5
Expected Output :
9 99 999 9999 99999
The sum of the series = 111105
program
#include <stdio.h>
#include <conio.h>
int main()
{
int no, i, sum = 0, t = 9;
printf("Enter a number: ");
scanf("%d", &no);
for (i = 0; i <= no; i++)
{
sum += t; // sum = sum + t;
printf("%d ", t);
t = t * 10 + 9; // 9*10+9 = 99
}
printf("\nThe sum is %d ", sum);
return 0;
getch();
}
OUTPUT
Enter a number:
5
9 99 999 9999 99999 999999
The sum is 1111104
0 Comments