Program to find out the sum of cubes of natural numbers


Default
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
// Write a program to find out the sum of cubes of natural numbers
// Ex: 4 -> 100; 2 -> 9
// Author: AimForJob.com
#include<stdio.h>
int main(){
    int num;
    printf("Enter a number to find sum of cubes upto that number:");
    scanf("%d",&num);
    while(num<=0){
        printf("Please enter only a positive integer: ");
        scanf("%d",&num);        
    }
    // calculating sum of cubes
    long long int sum;
    sum = (num*num/4.0)*(num+1)*(num+1);
    int i=1;
    printf("\n");
    if(num<=4){
        while(i<num){
            printf("%d^3+",i);      
            i++;
        }
        printf("%d^3\n",i);
    }
    else{
        printf("1^3+2^3+...+%d^3+%d^3\n",num-1,num);
    }
    printf("\nSum of cubes of natural numbers upto %d is: %lld",num,sum);
getch();
}

No comments:

Post a Comment