Thursday 1 February 2018

Example for 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);
if(n>1)
for(i=1;i<=n;i++)
{
fact=fact*i;
}
printf(“\n factorial value=%d”,fact);
getch( );
}

Output:

enter the no 6
factoria value=720

2.Write a program tofind out the sum of n integer numbrs.

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

Output:

enter the no 20
sum=210

3.Write a program find out the table of the intger no (using for loop).

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

Output:

Enter the no 5
5*1=5
5*2=10
5*3=15
5*4=20
5*5=25
5*6=30
5*7=35
5*8=40
5*9=45
5*10=50

4.Write a program to calculates fabonacii series.

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

Output:

enter the no 10
0 1 1 2 3 5 8 13 21 34 55 89 144
n=10

Share this

0 Comment to "Example for Loop in C Language"

Post a Comment