A PHP Error was encountered

Severity: Warning

Message: fopen(/var/cpanel/php/sessions/ea-php56/PHPSESSID335s9ivvvfh9nadre9btsod7sat7tdjc): 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 the sum of the series 1^3 -2^3 + 3^3 - 4^3 + ... N^3
Q:

C program to calculate the sum of the series 1^3 -2^3 + 3^3 - 4^3 + ... N^3

0

C program to calculate the sum of the series 1^3 -2^3 + 3^3 - 4^3 + ... N^3

To find the sum of this series using C program, we will read N by the user (which is the total number of terms) and run loop from 1 to N, within the loop body, we will check the condition for EVEN or ODD because, in this series ODD terms are being added and EVEN terms are being subtracting. Finally, we will print the calculate sum (result) of the series).

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 find the sum of the series 1^3 -2^3 + 3^3 - 4^3 + ... N^3 in C

#include <stdio.h>
#include <math.h>

int main()
{
	int n; //total number of terms
	int i; //loop counter
	long int sum; //sum of the series
	
	//read total number of terms
	printf("Enter total number of terms: ");
	scanf("%d",&n);
	
	//assign 0 to the sum
	sum = 0;
	
	for(i=1; i<=n; i++){
		//ODD terms are adding and
		//EVEN terms are subtrating
		if(i%2!=0)
			sum = sum + pow(i,3);
		else
			sum = sum - pow(i,3);
	}
	
	//print sum of the series
	printf("Sum of the series: %ld\n",sum);
	
	return 0;	
}

Output

Enter total number of terms: 5 
Sum of the series: 81

Explanation:

    Series is: 1^3 -2^3 + 3^3 - 4^3 + ... N^3
    Here, N = 5
    Then the series will be:
    = 1^3 -2^3 + 3^3 - 4^3 + 5^3
    = 1 - 8 + 27 -64 + 125
    = 81

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