Sunday 31 May 2015

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 Input\n");
        }
        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("%d\n",sum);
        }
        return 0;
  }

0 comments:

Post a Comment

 

Copyright @ 2013 Appychip.

Designed by Appychip & YouTube Channel