0% found this document useful (0 votes)
35 views

Experiment No-04 Program On Control Structure Loops

This document discusses control structure loops using the for loop as an example. It provides the syntax, parts, algorithm, and flowchart for a for loop. It then gives 5 examples of for loop programs, including calculating the sum of numbers, finding a factorial, nested for loops to print patterns, and a pyramid pattern. The conclusion states that the document covered using for loops as a control structure.

Uploaded by

anilnaik287
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
35 views

Experiment No-04 Program On Control Structure Loops

This document discusses control structure loops using the for loop as an example. It provides the syntax, parts, algorithm, and flowchart for a for loop. It then gives 5 examples of for loop programs, including calculating the sum of numbers, finding a factorial, nested for loops to print patterns, and a pyramid pattern. The conclusion states that the document covered using for loops as a control structure.

Uploaded by

anilnaik287
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 7

EXPERIMENT NO-04

PROGRAM ON CONTROL STRUCTURE LOOPS

Aim: To study about control structure (for loop)


Description:
The for is an entry controlled loop this is used when an action is to be
repeated for predefined numbers.
Syntax
for (Initial value; Test ; Increment / Decrement )
{
Action 1;
}
Action 2;
Three parts of for loop as follows
Initial Value: Variable can be defined and initialized.
Test: As long as this condition is true the statement in loop gets
executed. When condition becomes false that time program control
goes out of loop.
Increment/Decrement: In this part we may increment or decrement the
counter depends upon condition.

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++)

The Factorial is=

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

Ex 4)/*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=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

Ex 5)//program for pyramid


#include<stdio.h>
#include<conio.h>
void main()
{
int i,j;
clrscr();

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.

You might also like