Chapter 5.2 Control Statement(loop)
Chapter 5.2 Control Statement(loop)
Control Statements(Loop)
Repetition or Iteration or Loop Control Instruction
• The repetitive structure consists of program statements that are repeatedly
executed while some condition hold true.
• The computer has the ability to perform a set of instructions repeatedly.
• This involves repeating some portion of a program either for a specified
number of times or until a given condition is satisfied.
• This repetitive operation is done by loop control statement.
There are three methods for generating repetition of a certain part of the
program
• for statement
• while statement
• do…while statement
for loop
#include <stdio.h>
void main()
{
int j, sum = 0;
Flowchart
Example1: Program to print Example2: Write a program to find factorial
10 natural numbers. using while loop.
#include<stdio.h>
#include<stdio.h>
#include<conio.h>
#include<conio.h> void main()
void main() {
{ int fact=1, i=1,n;
int number=1; clrscr ( );
clrscr ( ); printf("Enter the number:");
scanf("%d",&n);
while(number<=10)
while(i<=n)
{ {
printf("%d\t",number); fact=fact*i;
number++; i++;
} }
getch(); printf("Factorial of %d is %d",n,fact);
getch();
}
}
Difference between for loop and
while loop
do…while loop statement
sum = sum + i;
}
printf("%d",sum);
return 0;
}
2. wap to find the sum of following series f(x)= 1+4+9+16+.....+(n^2)
#include<stdio.h>
int main()
{
int n, sum=0;
printf("Enter n value: ");
scanf("%d",&n);
printf("%d",sum);
return 0;
}
3. wap to find the sum of following series
2+4+6+8+10+......+n-terms. general formula->(n*2)
#include<stdio.h>int main()
{
int n, sum=0;
printf("Enter n value: ");
scanf("%d",&n);
printf("%d",sum);
return 0;
}
4. WAP to find the sum of series (use float as data type): 1+1/2+1/3+.....+1/n
#include<stdio.h>
int main()
{
int n,i;
float sum=0.0;
for(i=1;i<=n;i++)
{
sum = sum + 1.0/ i ;
}
return 0;
}
5. wap to find the sum of following series :1+8+27+64+125......+n^3
#include<stdio.h>
int main()
{
int n, sum=0;
printf("Enter n value: ");
scanf("%d",&n);
printf("%d",sum);
return 0;
}
6. Evaluate the following series: -1+4-9+16-25+36.......+(-1)^n*n^2
#include<stdio.h>
#include<conio.h>
#include<math.h>
int main()
{
int n,i,s=0.0;
printf("enter the limit of the series\n");
scanf("%d",&n); //n-->3
for(i=1;i<=n;i++) //loop to calculate sum
{
s=s+pow(-1,i)*pow(i,2);
}
printf("the sum of the series is %d",s);
getch();
return(0);
}
7. wap to find the sum of series: 1!+2!+3!+4!+......+n!
#include<stdio.h>
#include<conio.h>
#include<math.h>
int main()
{
int n,sum=0,fact,i,j;
printf("enter the limit of the series\n");
scanf("%d",&n);
for(i=1;i<=n;i++) // calculation of sum
{
fact=1;
for(j=1;j<=i;j++) // calculation of factorial
{
fact=fact*j;
}
sum=sum+fact;
}
printf("the sum of factorial is %d",sum);
getch();
return(0);
}
8. evaluate the following series:- 1+2/2!+3/3!+.....+ n/n!
#include<stdio.h>
#include<conio.h>
int main()
{
float n,i,j,s=0.0,fact;
printf("enter the limit of the series\n");
scanf("%f",&n); //n-->3
for(i=1.0;i<=n;i++) //loop to calculate sum
{
fact=1.0;
for(j=1.0;j<=i; j++) //loop to calculate factorial
{
fact=fact*j;
}
s=s+i/fact;
}
printf("the sum of the series is %f",s);
getch();
return(0);
}
9. evaluate the following series:- f(x)= x+x^2+x^3+....+x^n
#include<stdio.h>
#include<conio.h>
#include<math.h>
int main()
{
int n,x,i,s=0;
printf("enter the value of x\n");
scanf("%d",&x);
printf("enter the limit of series\n");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
s=s+pow(x,i);
}
printf("the sum is %d",s);
getch();
return 0;
}
10. evaluate the following series:- f(x)=x+x^3/3!+x^5/5!+.....+x^(2n-1)/(2n-1)!
#include<stdio.h>
#include<conio.h> for(j=1.0;j<=2*i-1;j++) //loop to
#include<math.h> calculate factorial
int main() {
{ fact=fact*j;
float n,i,j,s=0.0,fact,x; }
printf("enter the limit of the series\n");
s=s+pow(x,2*i-1)/fact;
scanf("%f",&n);
printf("enter the value of x\n"); }
scanf("%f",&x); printf("the sum of the series is
for(i=1.0;i<=n;i++) //loop to calculate sum %f",s);
{ getch();
fact=1.0; return(0);
}
Assignment
1. 1+1/2!+1/3!+.....+1/n!
2. 1-4+9-16+25-......+(-1)^n+1*(n^2)
3. x^2+x^4+x^6+.....+x^2n
4. 1+x^2/2!+x^4/4!+x^6/6!+....+x^2n/2n!
Ques: WAP to find the term and find sum of following series
1,11,111,1111,11111,......n terms
#include<stdio.h>
#include<conio.h>
int main()
{
int n,i,s=0,a=0;
printf("enter the limit\n");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
a=a*10+1;
printf(“the term is %d”,a);
s=s+a;
}
printf("the sum is %d",s);
getch();
return(0);
}
Ques: WAP to print the following series
1,2,5,10,17,26,37...........
#include <stdio.h>
int main()
{
int t=1,i=1;
do
{
printf("%d\t",t);
t=t+ (2*i-1);
i++;
}while(t<500);
return 0;
}
Nested Loop
• C supports nesting of loops in C.
• Nesting of loops is the feature in C that allows the looping of
statements inside another loop.
• Any number of loops can be defined inside another loop, i.e., there is
no restriction for defining any number of loops.
Syntax of Nested loop Syntax of Nested for loop Syntax of Nested while loop
return 0;
}
Looping Categories
Entry Control Loop Exit Control Loop
In this loop the test condition or criteria is checked and evaluated first
In this loop the test condition or criteria is checked after the loop is executed.
before loop starts executing.
for, while loop can be used as entry control loop. do-while loop can be used as exit control loop
Loop conditions are not terminated with semicolon. Loop conditions are terminated with semicolon.
Body of loop never executed if the condition is false. Body of loop is executed for at least 1 time even after the condition is false.
Syntax: Syntax:
while (condition) do
{ {
Block of statements; Block of statements;
} } while(condition);
Sentinel controlled loop
• Sentinel controlled loop are those loop where where we do not know the
number of execution in advance.
• In this case, the value of the control variable differs within a limitation and the
execution can be terminated at any moment.
• The control variable in this case is termed by sentinel variable.
• The value of this variable changes inside the loop. The loop breaks when the
value of the control variable matches the condition.
Example:
do{
printf("Input a number.n");
scanf("%d", &num);
}while(num< 0);
Counter controlled loop
• Counter controlled loops are those loops where we know the number of
the executions in advance.
• The control variable is known as counter.
• In this loop, we set the value of the counter and max limit or condition
beforehand.
Example
int sum = 0,i;
for (i=1;i<= 10;i++)
{
sum = sum + i;
}
printf(“%d”,sum);
Sentinel Controlled Loop Counter Controlled Loop
A sentinel controlled loop is the indefinite repetition loop as the number of A counter controlled loop is the definite repetition loop as the number of
repetitions is not known before the loop begins executing repetitions is known before the loop begins executing
Controlled variable used is known as sentinel variable. Controlled variable used is known as counter.
The value of the variable is not strict and it varies. The value of the variable is strict.
The Limitation of the variable is strict. The Limitation of the variable is strict also.
A do….while and while loop loop is an example of sentinel controlled loop. A for loop is an example of counter controlled loop.
Continue statements
}
}
4. WAP to evaluate exponential series up to limit n
#include<stdio.h>
#include<conio.h> //calculate sum and fact by ur self
int main(){
int n, a;
float sine, sinedegree, term, sum;
clrscr();
printf("Enter the value for Sine : ");
scanf("%f",&sine);
printf("\nEnter number of terms : ");
scanf("%d",&n);
sinedegree=sine;
sinedegree *= 3.14159/180;//
6. WAP to read values from user and find the count until user type 0, also display sum and average.
#include <stdio.h>
int main()
{
int n, s=0,c=0;
float avg;
clrscr( );
d0
{
printf(“enter a number”);
scanf(“%d”,&n);
s=s+n;
c=c+1;
}while (n!=0);
avg= (float)(s/c);
printf(“the sum and average is %d\t %f”,sum,avg);
return 0;
}
7. WAP that calculates the sum of digits entered by the user successively until the sum reduces to a single
digit number; example [ 12345=1+2+3+4+5=15 1+5=6
#include <stdio.h>
int main()
{
int n, s,r;
clrscr( );
printf(“enter a number”);
scanf(“%d”,&n);
do
{
s=0;
do
{
r= n% 10;
s=s+r;
n=n/10;
}while (n!=0);
n=s;
}while (s / 10!=0);
printf(“the sum is %d”,sum);
return 0;
}
Question:
1.WAP to convert binary number to decimal number
2.WAP to check whether the enter year is leap year or not.
3.Roots of quadratic equation using switch
4.WAP to find sum of digits of any number.
5.WAP to display Multipication table of m by n