Translate

Labels

Showing posts with label NATURAL NUMBER. Show all posts
Showing posts with label NATURAL NUMBER. Show all posts

Saturday, 27 April 2013

SUM THE FIRST 50 EVEN NATURAL NUMBERS

/*PROGRAM TO SUM THE FIRST 50 EVEN NATURAL NUMBERS*/
#include<stdio.h>
main()
{
int sum=0,i=2;
while(i<=100)
{
sum+i;
i+=2;
}
printf("Sum of first 50 even natural numbers=%d\n",sum);
getch();
}

OUTPUT
Sum of first 50 natural numbers=2550


Wednesday, 20 March 2013

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:
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