#include <stdio.h>
/* Write C Program to find sum of first N natural number, N must be provided by the user.*/
/* C program to to find sum of a n number using while loop.*/
#include <stdio.h>
int main()
{
int n,total=0,i=1;
printf("\n Enter Any Natural Number:");
scanf("%d",&n);
while(i<=n)
{
total=total+i;
printf(" %d",i);
i++;
}
printf("\n Sum of first %d Natural Number=%d",n,total);
return 0;
}
Output:
Enter Any Number
10
1 2 3 4 5 6 7 8 9 10
Sum of first 10 Natural Number =55
Program Explanation:
1. In the program given above the variables n,total and i is declared as integer variable. the variable total=0 is assigned and i variables are initialized to value 1 i.e. i=1.
variable n is declared as the integer whose factorial we want to find out.
2.
printf("\n Enter Any Natural Number:");
scanf("%d",&n);
printf() function prompt the user "Enter Any number" then control jumps to the input function
scanf("%d",&n); and wait for the user input. User inputs/enters number 10 which is get stored(scaned)in variable n.
3.1 control jumps to while loop and test the condition while(i<=n) i.e. while(1<=10) which results in true, statement inside body of loop total=total+i,printf(" %d",i);and i++ executes.
Initially The value of total=0 and i=1 the expression total=total+i evaluates to total=total+1 i.e. total is assigned value 1. total=1
next printf(" %d",i); executes and display(Output) the integer value of i i.e. 1.
i++ executes (i=i+1) and the vaue of i is increment by 1 now value of i=2.
3.2 control jumps to while loop and test the condition while(i<=n) i.e. while(2<=10) which results in true.
The statement inside body of loop total=total+i,printf(" %d",i);and i++ executes.
The previous value of total=1 and i=2 the expression total=total+i evaluates to total=1+2 i.e. total is assigned value 3. total=3
Next printf(" %d",i); executes and display(Output) the integer value of i i.e. 2.
i++ executes (i=i+1) and the vaue of i is increment by 1 now value of i=3.
3.3 control jumps to while loop and test the condition while(i<=n) i.e. while(3<=10) which results in true.
The statement inside body of loop total=total+i,printf(" %d",i);and i++ executes.
The previous value of total=3 and i=3 the expression total=total+i evaluates to total=3+3 i.e. total is assigned value 6. total=6
next printf(" %d",i); executes and display(Output) the integer value of i i.e. 3.
i++ executes (i=i+1) and the vaue of i is increment by 1 now value of i=4.
this process repeats while the value of i<=10
.
.
.
let say value of became 10 i=10.
3.10 control jumps to while loop and test the condition while(i<=n) i.e. while(10<=10) which results in true.
The statement inside body of loop total=total+i,printf(" %d",i);and i++ executes.
The previous value of total is 45 total=45 and i=10 the expression total=total+i evaluates to total=45+10 i.e. 55 is assigned to variable total
total=55.
Next printf(" %d",i); executes and display(Output) the integer value of i i.e. 10.
i++ executes (i=i+1) and the vaue of i is increment by 1 now value of i=11.
3.11 control jumps to while loop and test the condition while(i<=n).
i.e. while(11<=10) which results in false and control come out of while loop and execute statements outside the body of loop.
4. Once the control come out of while loop start executing the printf("\n Sum of first %d Natural Number=%d",n,total); statement.
which shows the output
Sum of first 10 Natural Number=55.
The value of i=10 and total=55 are placed at the place of format specifiers.
Value of n=10 is placed at the first format specifier %d and value of total=55 at the second format specifier %d