Thursday, 1 February 2018

Example While Loop in C Language

1.Write a program to evaluate the factorial value of a integer no.


#include<stdio.h>
#include<conio.h>
void main( )
{
clrscr( );
int n,i,fact=1;
printf(“enter the no”);
scanf(“%d”,&n);
i=1;
if(n>1)
while(i<=n)
{
fact=fact*i;
i++;
}
printf(“\n factorial value=%d”,fact);
getch( );
}

Output:

enter the no 5
factoria value=120


2.Write a program tofind out the sum of n integer numbrs(using while loop).

#include<stdio.h>
#include<conio.h>
void main( )
{
clrscr( );
int n,i,sum=0;
printf(“enter the no”);
scanf(“%d”,&n);
i=1;
while(i<=n)
{
sum=sum+i;
i++;
}
printf(“\n sum=%d”,sum);
getch( );
}

Output:

enter the no 100
sum=5050

Share this

0 Comment to "Example While Loop in C Language"

Post a Comment