31. WRITE A C PROGRAM FOE SUM OF FIRST N NATURAL NUMBERS USING WHILE LOOP

/*c program foe sum of first n natural numbers using while loop*/

#include<stdio.h>
#include<conio.h>
void main()
{
int n,i,sum;
clrscr();
printf("Enter a number \n");
scanf("%d",&n);
i=1;
sum=0;
while(i<=n)
{
sum+=i;
i++;
}
printf("sum =%d\n",sum);
getch();
}

output

Enter a number
10
sum=55

Comments