A PHP Error was encountered

Severity: Warning

Message: fopen(/var/cpanel/php/sessions/ea-php56/PHPSESSID7kbhircr2nhu6i1jmoouodar5crsllet): failed to open stream: No space left on device

Filename: drivers/Session_files_driver.php

Line Number: 174

A PHP Error was encountered

Severity: Warning

Message: session_start(): Cannot send session cookie - headers already sent by (output started at /home/nerd/public_html/system/core/Exceptions.php:271)

Filename: Session/Session.php

Line Number: 144

A PHP Error was encountered

Severity: Warning

Message: session_start(): Cannot send session cache limiter - headers already sent (output started at /home/nerd/public_html/system/core/Exceptions.php:271)

Filename: Session/Session.php

Line Number: 144

A PHP Error was encountered

Severity: Warning

Message: Cannot modify header information - headers already sent by (output started at /home/nerd/public_html/system/core/Exceptions.php:271)

Filename: controllers/Questions.php

Line Number: 268

C program to calculate sum of the series 1 + 11 + 111 + 1111 + ... N terms
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

A PHP Error was encountered

Severity: Warning

Message: Cannot modify header information - headers already sent by (output started at /home/nerd/public_html/system/core/Exceptions.php:271)

Filename: questions/question_for_visitors.php

Line Number: 568

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)

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now

A PHP Error was encountered

Severity: Warning

Message: Unknown: Failed to write session data (user). Please verify that the current setting of session.save_path is correct (/var/cpanel/php/sessions/ea-php56)

Filename: Unknown

Line Number: 0