-->

program to get sum of 1 to 10 natural numbers.

//program to get sum of 1 to 10 natural numbers.
#include<stdio.h>
#include<conio.h>
void main()
{
int k=1,sum=0;   //initialization
printf("enter value of 'n'\n");
scanf("%d",&n);
while(k<=n)
   {
      sum=sum+k;
      k++;
   }
printf("the sum=%d,",sum);
getch();
}

note:
here I have used 'n' as an input. We  can input any value, may be 10 or 20 or 100.
If you want to use 10 in loop directly then you can use and there would be no input(no scanf(..))

No comments:

Post a Comment