×

C Programs

C Basic & Conditional Programs

C Looping Programs

C String Programs

C Miscellaneous Programs

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

Here, we are going to implement a c program that will find the sum of series 1 + 11 + 111 + 1111 + ... N terms. Submitted by IncludeHelp, on March 04, 2018

Calculating 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.

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

#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

C Sum of Series Programs »

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.