Q:

C program to calculate sum of the series 1 + 11 + 111 + 1111 + ... N terms

0

C program to calculate sum of the series 1 + 11 + 111 + 1111 + ... N terms

To find (calculate) the sum of series 1+11+111+1111+... till N terms using C program - we are reading total number of terms by the user and using following steps:

  • Declared variables to stores sum of the values (sum), loop counter (i) and a variable to store term (temp) value (like, 1, 11, 111,.. )
  • Assigning 0 to the variable sum and initial value 1 to the temp
  • Within the loop - first we are adding the temp value to the sum and them increasing the value of temp to make it next term value.
  • And, after the loop – we are printing the value of the sum.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Program to calculate sum of series 1+11+111+1111+... till N terms in C

#include <stdio.h>

int main()
{
	int terms; //to store total terms
	int i; //to run loo;
	long int sum; //to store sum of values
	long int temp; //to add the diff or initial term value
	
	//assign 0 to sum and assign 1 as initial term value
	sum =0;
	temp =1;
	
	//input total terms
	printf("Enter total number of terms: ");
	scanf("%d",&terms);
	
	//run loop to find sum of each value 
	//and then increase it with the differences
	for(i=0; i<terms; i++)
	{
		//print the  value
		printf("%ld ", temp);
		//print '+' sign in the series
		if(i<terms-1)
			printf("+ ");
		
		//add the value and store in sum
		sum += temp;
		//update/increase the value 
		temp = (temp*10)+1;		
	}
	
	//print the value of sum of the series
	printf("\nSUM of the series is: %ld\n",sum);
	
	return 0;
}

Output

First run:
Enter total number of terms: 5
1 + 11 + 111 + 1111 + 11111 
SUM of the series is: 12345

Second run:
Enter total number of terms: 10
1 + 11 + 111 + 1111 + 11111 + 111111 + 1111111 + 11111111 + 111111111 + 1111111111 
SUM of the series is: 1234567900

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

C program to calculate the sum of series 1 +1/x^1 ... >>
<< C program to calculate the sum of the series 1-2+3...