Home » C programs » Sum of series C programs

In this program, we are going to learn **how can we find the solution (calculate the sum) of the series 1^3 -2^3 + 3^3 - 4^3 + ... N^3**, where N will be given by the user.

Submitted by **IncludeHelp**, on March 06, 2018

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

#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 = 5Then the series will be: = 1^3 -2^3 + 3^3 - 4^3 + 5^3 = 1 - 8 + 27 -64 + 125 = 81

**Read more...**

Are you a blogger? Join our Blogging forum.

Comments and Discussions

.resCodeAS1 { display: block; width: 320px; height: 50px; }
@media(min-width: 300px) { .resCodeAS1 { display: none; } }
@media(min-width: 480px) { .resCodeAS1 { display: none; } }
@media(min-width: 750px) { .resCodeAS1 { display: block; width: 336px; height: 280px; } }
(adsbygoogle = window.adsbygoogle || []).push({});

(adsbygoogle = window.adsbygoogle || []).push({});

(adsbygoogle = window.adsbygoogle || []).push({});

solved programs: » C » C++ » DS » Java » C# |

aptitude que. & ans.: » C » C++ » Java » DBMS |

interview que. & ans.: » C » Embedded C » Java » SEO » HR |

close Like other websites, this site uses cookies to deliver relevant ads based on your interest, by using our website, you acknowledge that you have read our privacy policy.