C program to sum cube of first n numbers


#include <stdio.h>
int main()
{
int num, cube, sum = 0, i = 1;
/* get the input value n from user */
scanf("%d", &num);
printf("n");
if((num<0) || (num>100))
{
printf("Invalid Inputn");
}
else
{
/* calculate the sum of cubes of 1st n natural nos */
while (i <= num) {
cube = i * i * i;
sum = sum + cube;
i++;
}
/* print the sum of cubes of 1st n natural nos */
printf("%dn",sum);
}
return 0;
}

Leave a Reply

Your email address will not be published. Required fields are marked *