The positive numbers 1, 2, 3, 4... are known as natural numbers. The programs below takes a positive integer as an input from the user and calculates the sum up to n.
#include<stdio.h> #include<conio.h> void main() { int a,i,sum=0; clrscr(); printf("enter a range:"); scanf("%d",&a); i=1; while(i<=a) { sum=sum+i; i++; } printf("the sum of the natural no is:=%d\n",sum); getch(); } |
Output:-
Explanation:
enter the number:3=1+2+3 =6
enter the number:5=1+2+3+4+5=15
______________________________________________________
______________________________________________________
______________________________________________________
_____________________________________________________
Post a Comment