0% found this document useful (0 votes)
3 views53 pages

Chapter 5.2 Control Statement(loop)

Chapter 5 discusses control statements in programming, specifically focusing on loops for repetition of code execution. It explains three types of loops: for, while, and do...while, detailing their syntax and usage with examples. The chapter also highlights the differences between these loops and provides various programming examples to illustrate their application.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views53 pages

Chapter 5.2 Control Statement(loop)

Chapter 5 discusses control statements in programming, specifically focusing on loops for repetition of code execution. It explains three types of loops: for, while, and do...while, detailing their syntax and usage with examples. The chapter also highlights the differences between these loops and provides various programming examples to illustrate their application.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 53

Chapter 5

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

• The for loop is mostly used loop for repetitive structure.


• It is an entry control loop because it checks condition at the entry point.
• The for loop consists of three expressions.
• The first expression is used to initialize the index value(initial value), the
second to check whether the loop is to be continued again and third to
change the index value(increment or decrement) for further repetition.
• Syntax:
for(initialization; test condition; increment or decrement)
{
Body of loop
}
Flowchart Example 2: WAP to display the
numbers from 0 to 10 using for
loop.
#include <stdio.h>
#include<conio.h>
void main()
Example 1: WAP to display “Welcome to C Programming {
Class” 10 times using for loop.
#include <stdio.h>
int i;
#include<conio.h> clrscr();
void main()
{ for(i=0;i<=10;i++){
int i; printf("%d\n",i);
clrscr();
for(i=0;i<10;i++){ }
printf(" Welcome to C Programming Class”); getch();
}
getch(); }
Example 3: Write a C program that Example 4: WAP to count all the integers greater
prints all even numbers between 1 and than 100 and less than 200 that are divisible by 7.
50. #include <stdio.h>
#include <stdio.h> #include <conio.h>
int main() void main()
{ {
int i; int i, count=0;
printf("Even numbers between 1 clrscr();
to 50 \n"); for(i=100;i<200;i++) {
for (i = 1; i <= 50; i++) if (i%7 = = 0)
{ {
if(i%2 == 0) count = count + 1;
{ }
printf("%d ", i); }
} printf("The count is = %d", count);
} getch();
return 0; }
}
Example 5: WAP to find the sum of first 10 natural numbers.

#include <stdio.h>
void main()
{
int j, sum = 0;

printf("The first 10 natural number is :\n");

for (j = 1; j <= 10; j++)


{
sum = sum + j;
printf("%d ",j);
}
printf("\nThe Sum is : %d\n", sum);
}
CW: A program to find the sum and the average of given numbers.
Example 6: A program to find the sum and the average of given Example 7: WAP to calculate the factorial of a given
numbers. number
#include <stdio.h>
void main()
{
int n, i;
float sum=0.0, a, average; #include <stdio.h>
void main()
printf("enter the amount of number we want to {
add"); int i,f=1,num;
scanf("%d",&n);
for(i=1; i<=n; i++) printf("Input the number : ");
scanf("%d",&num);
{
printf("Enter a number to be added:"); for(i=1;i<=num;i++)
scanf("%f ",&a); {
sum=sum+a; f=f*i;
} }
printf("The Factorial of %d is: %d\n",num,f);
average=sum/n; getch();
printf("Sum=%f ", sum); }
printf("\nAverage= %f ", average);
getch();
Example 8: A program to create the
multiplication table of a number.
#include <stdio.h>
void main()
{
int j,n;
printf("Input the number (Table to be
calculated) : ");
scanf("%d",&n);
for(j=1;j<=10;j++)
{
printf("%d X %d = %d \n",n,j,n*j);
}
}
Example 9: If two numbers #include<stdio.h>
n1 & n2 are input through #include<conio.h>
void main()
the keyboard. WAP to find
{
sum of those numbers int n1, n2, sum=0, i;
which is exactly divisible printf("enter n1 & n2");
by 5 between n1 and n2. scanf("%d%d",&n1, &n2);
for(i=n1; i<=n2; i++)
{
if(i%5==0)
sum=sum+i;
}
printf("required sum=%d",sum);
getch();
}
Example 10: WAP to display the
Fibonacci series.
#include <stdio.h>
int main()
In Fibonacci series, The first two { int a=0, b=1, c, i, n;
numbers are 0 and 1, and each
subsequent numbers are the sum of
printf(“ enter the number of terms”
previous two numbers. For scanf(“%d”,&n);
example- 0 1 1 2 3 5 8 printf("%d %d ",a,b);
13 ............
for(i = 1; i <= n-2; i++)
{ c = a + b;
printf("%d ", c);
a = b;
b = c;
}
return 0;}
while loop
• The second type of loop statement is while loop.
• while loop first checks whether the initial condition is true or false and
finding it to be true, it will enter to the body of loop and execute the
statement.
• Thus, it is also an entry control loop (perform pre-test).

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

• The do...while loop is another repetitive loop used in C programs.


• It differs from the for loop and while loop.
• The do…while loop is an exit control (post-test) loop because it tests the
condition at the exit point.
• The while loop tests the condition before executing any of the statements
inside the while loop but the do…while loop test the condition after
executing the statements.
• This means that the do…while loop would execute its statements at least
once even if the condition fails for first time. The while loop will not
execute statements if the condition fails for the first time.
Example: A program to find the sum of odd numbers
using do…while loop.
#include <stdio.h>
#include<conio.h>
void main()
{
int n, sum=0, i=1;
clrscr();
printf("Enter the value of n:");
scanf("%d",&n);
do
{
sum=sum + i;
i=i+2;
}while(i<=n);
printf("The sum of odd number is %d", sum);
getch();
Example: WAP to find the sum of numbers
entered by user. The program should take
input from user until the user enters zero
‘0’.
#include<stdio.h>
#include<conio.h>
int main()
{
int n,s=0; //loop controlling variable
do
{
printf("enter the number");
scanf("%d",&n);
s=s+n;
}
while(n!=0);
printf("the sum is %d",s);
getch();
return(0);
}
Difference between while loop and do-while loop
Example: WAP to check whether for(i=1;i<=l;i++)
user given no is palindrome or not {
r=n%10;
by using loop.
rev=rev*10+r;
#include<stdio.h> n=n/10;
#include<conio.h> }
#include<math.h> if(k==rev)
int main() {
{ printf("%d is palindrome no",k);
}
int n,r,rev=0,i,l,k;
else
printf("enter the number\n"); {
scanf("%d",&n); printf("%d is not a palindrome no",k);
k=n; }
l=log10(n)+1; getch();
HW: WAP to check whether user given no is Armstrong
return(0);
or not by using loop. }
Using while loop:
if(k==rev)
#include<stdio.h>
{
#include<conio.h>
printf("%d is palindrome no",k);
int main()
}
{
else
int n,r,rev=0,i,k;
{
printf("enter the number\
printf("%d is not a palindrome no",k);
n");
}
scanf("%d",&n);
getch();
k=n;
return(0);
while (n!=0)
}
{
r=n%10; Q. WAP to input multi-digit number and calculate the sum of individual digit
rev=rev*10+r; number.
n=n/10; Q. WAP to input multi-digit number and calculate the sum of square of individual
digit
} Number.
Q. WAP to print the terms of the series which is less if(t<400)
than 400
1,4,9,16,25.......n terms {
#include<stdio.h> printf("%d\t",t);
#include<conio.h>
#include<math.h> }
int main() }
{ getch();
int i,n,t;
return(0);
printf("enter the limit of series\n");
scanf("%d",&n); }
for(i=1;i<=n;i++)
{
t=pow(i,2);
1. WAP to find the sum of following series: 1+2+3+4+5+6……………………......+n
#include<stdio.h>
int main()
{
int n, sum=0;

printf("Enter n value: ");


scanf("%d",&n);

for(int i=1; i<=n; i++)


{

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

for(int i=1; i<=n; i++)


{

sum = sum + i*i;


}

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

for(int i=1; i<=n; i++)


{

sum = sum + 2*i;


}

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;

printf("Enter the limit (n Value): ");


scanf("%d",&n);

for(i=1;i<=n;i++)
{
sum = sum + 1.0/ i ;
}

printf("Sum = %f\n", sum);

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

for(int i=1; i<=n; i++)


{

sum = sum + i*i*i;


}

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

Outer_loop for (initialization; condition; update) while(condition)


{ { {
Inner_loop for(initialization; condition; update) while(condition)
{ { {
// inner loop statements. // inner loop statements. // inner loop statements.
} } }
// outer loop statements. // outer loop statements. // outer loop statements.
} } }
Write the #include<stdio.h>
#include<stdio.h> Q.2
following
1 #include<conio.h>
pattern : #include<conio.h> 12 int main ()
Q.1.
12345
int main () 123
{
{ int i,j; 1234
12345
12345 int i,j;
12345 for(i=1;i<=5;i++) for(i=1;i<=5;i++)
12345
12345
{ {
for(j=1;j<=i;j++)
for(j=1;j<=5;j++) {
{ printf("%d",j);
printf("%d",j); }
printf("\n");
}
printf("\n"); }
}
}
}
Q.3 #include<stdio.h> Q.4
1 5
22 #include<conio.h> 5 4 #include<stdio.h>
333 int main () 5 4 3 #include<conio.h>
4444 { 5 4 3 2 int main()
55555 5 4 3 2 1
int i,j; {
for(i=1;i<=5;i++) int i,j;
{ for(i=5;i>=1;i--)
{
for(j=1;j<=i;j++) for(j=5;j>=i;j--)
{ {
printf("%d\t",j);
printf("%d",i); }
printf("\n");
} }
printf("\n"); getch();
return(0);
} }
}
Q.5
5 #include<stdio.h> Q.6 Floyd's #include <stdio.h>
4 4 #include<conio.h> Triangle int main()
3 3 3
2 22 2 int main() 1 {
1 11 1 1 { 23 int rows, i, j, number = 1;
int i,j; 456 printf("Enter the number of rows: ");
7 8 9 10
for(i=5;i>=1;i--) scanf("%d", &rows);
{ for (i = 1; i <= rows; i++)
for(j=5;j>=i;j--) {
{ for (j = 1; j <= i; ++j)
printf("%d\t",i); {
} printf("%d ", number);
printf("\n"); number ++;
} }
getch(); printf("\n");
return(0); }
} return 0;
}
Q.7
#include<stdio.h> Q.8 #include<stdio.h>
* A
**
#include<conio.h> A B #include<conio.h>
int main()
*** int main ()
A B C
A B C D {
**** { int i,j; A B C D E
char i,j;
***** for(i=1;i<=5;i++) for(i=65;i<=69;i++)
{ {
for(j=1;j<=i;j++) for(j=65;j<=i;j++)
{ {
printf("*"); printf("%c\t",j);
}
printf("\n");
} }
printf("\n"); getch();
return(0);
} }
}
*********
#include <stdio.h>
*******
int main() {
*****
int rows, i, j, space;
***
printf("Enter the number of rows: ");
*
scanf("%d", &rows);
for (i = rows; i >= 1; --i)
{
for (space = 0; space < rows - i; ++space)
{
printf(" ");
}
for (j = i; j <= 2 * i - 1; ++j)
{
printf("* ");
}
for (j = 0; j < i - 1; ++j)
{
printf("* ");
}
printf("\n");
}

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.

Unknown number of iteration Known number of iteration.

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

• Continue statement is used to


bypass the execution of the
statements inside the loop.
• If we want to jump to the next
iterations of the loop by ignoring
the execution of statements
specified within the body of
loop. The keyword “continue”
allows us to do this.
• When the keyword is
encountered inside the loop,
control automatically passes to
Output: 1 2 4 5
the next iteration of the loop. A Thus, continue statement is used to skip a part of the body of loop
continue is specified using if() and continue with next iteration. Note that when the value of i
statement. equal to 3, the continue statement takes the control to the next
iteration bypassing the execution of the printf() statement.
Break Statement

• It is used to jump out of the loop.


• It terminate the execution of the
nearest enclosing loop.
• It is used with conditional statement
and with while, do…while and for
loop statements.
• WAP to enter a integer and print a
message whether the number is
Prime or not.
goto statement

• The goto statement is used to alter the program execution sequence by


transferring the control to some other part of the program.
• It is used to jump to a specific location
Extra Programs

1. Fibonacci Series until term is 500.


2. Greatest common factor(HCF)
and LCM
3. Prime or not
#include <stdio.h> if (factor==0)
int main() {
{
printf("prime number");
int n,i,factor=0;
}
printf("Enter number integers\ else
n"); {
scanf("%d", &n); printf("it is not prime number");
}
for (i=2;i<n;i++)
{
if(n % i==0) return 0;
{ }
factor++;

}
}
4. WAP to evaluate exponential series up to limit n

5. Write a program to compute sin x


And cos x for given x

#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

You might also like