9Google AdSense

Write a program to find the sum of first n natural numbers where n is entered by user (For-Loop)

#include <stdio.h>
#include <stdlib.h>


int main()
{
    int number, sum = 0, i;
    printf("Enter a number: ");
    scanf("%d",&number);

    for(i=1;i<=number;i=i+1)
    {
        printf("%d(sum)",sum);
        sum = sum + i;
        printf(" + %d(i) = %d(sum)\n",i,sum);

    }
    printf("Sum: %d",sum);


    return 0;
}


Output:

No comments: