Experiment No-04 Program On Control Structure Loops
Experiment No-04 Program On Control Structure Loops
Algorithm:
Start.
Declare the variables.
Check the condition if condition is true then goes to next
for loop.
If condition in next for loop is true then calculate the
factorial value of otherwise go to out of loop.
If condition is false then go to out of loop.
Print the result.
End program.
Flowchart:
Start
Input i,n,fact
`
`
for(i=1;i<=n;i++)
End
fact=fact*i
Programs:
Ex 1)/*Program for sum of 1 to 10 no*/
#include<stdio.h>
#include<conio.h>
void main()
{
int i,sum=0;
clrscr();
printf(\n WELCOME);
for(i=0;i<=10;i++)
{
sum=sum+i;
}
printf(\n sum of i=%d,sum);
getch();
}
Output: WELCOME
sum of i=55
Ex 2)/* Program for factorial no*/
#include<stdio .h>
#include<conio.h>
void main ()
{
int i, n, fact;
clrscr ( );
printf( Enter value of N= );
scanf(%d,&n);
fact=1;
for(i=1; i<=n; i++)
{
fact=fact*i;
}
printf( The factorial is =%d, fact);
}
getch();
}
Output:
Enter value of N = 5
The factorial is =120
Ex 3)/*program for nested for loop*/
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,n;
clrscr();
printf(\n Enter the for loop);
scanf(%d,&n);
for(i=1;i<=n;i++)
{
printf(\t);
for(j=1;j<=i;i++)
{
printf(%d,j);
}
printf(\n);
}
getch();
}
Output: Enter the no=4
1
12
123
1234
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,n;
clrscr();
printf(\n Enter the for loop);
scanf(%d,&n);
for(i=n;i>=1;i--)
{
printf(\t);
for(j=1;j<=i;j++)
{
printf(%d,j);
}
printf(\n);
}
getch();
}
Output: Enter the no =4
1234
123
12
1
for(i=10;i>=1;i--)
{
printf("\n");
for(j=1;j<=10;j++)
{
if(i<=j)
{
printf(" *");
}
else
{
printf(" ");
}
// printf("\n");
}
}
getch();
}
Output:
*
**
***
****
*****
******
*******
********
*********
**********
Conclusion:
we have studied the loop control structure by using for loop.