#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:
First run
Enter a number
10
sum=55
EXPLANATION:
n,i and sum are declared to be variables of int type.n is to collect a natural number;i is to select each natural number from 1 t on; sum is to collect the sum of natural numbers up to n.
First we accept a number into the variables n and initialize sum with zero.The following segment finds the sum of the first n Natural numbers.
while (i<=n)
{
sum+=i;
i++;
}
The sum is collected by the variable sum.The value of the variable sum is then displayed
#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:
First run
Enter a number
10
sum=55
EXPLANATION:
n,i and sum are declared to be variables of int type.n is to collect a natural number;i is to select each natural number from 1 t on; sum is to collect the sum of natural numbers up to n.
First we accept a number into the variables n and initialize sum with zero.The following segment finds the sum of the first n Natural numbers.
while (i<=n)
{
sum+=i;
i++;
}
The sum is collected by the variable sum.The value of the variable sum is then displayed
No comments:
Post a Comment