Looping is the process of executing the same program statement or block of program statements repeatedly for a specified number of times or till the given condition is satisfied. Loop structure is used to carry out looping.
Mainly there are three types of loop:- while loop
- do while loop
- for loop
It executes the program statements repeatedly until the given condition is true. It checks the condition at first; if it is found true then it executes the statements written in its body part otherwise it just gest out from the loop structure. It is also known as entry control or pre-test loop.
while(condition)
{
statements;
increment/decrement;
}
where initialization means starting point, condition means stopping point and increment/decrement means counter.
do while loop
It also executes program statements repeatedly until the given condition is true. It executes the program statements once at first then only condition is checked. If a condition is found true then it executes the program statements again, otherwise it gets out from the loop structure. As it checks the condition at last it is also known as the post-test loop or exit control loop.
do
{
statements;
increment/decrement;
}while (condition);
while loop | do while loop |
---|---|
(i) In the while loop, condition is checked in the beginning. | (i) In the do while loop, condition is checked at the end. |
(ii) It is also known as a pre-test or entry control loop. | (ii) It is also known as post-test or exit control loop. |
(iii) It is not terminated with a semicolon. | (iii) It is terminated with a semicolon. |
(iv) In the while loop, statements are not executed if the condition is false. | (iv) In the do while loop, statements are executed once even the condition is false. |
(v) It uses the keyword ‘while’. | (v) It uses two keywords ‘do’ and ‘while’. |
(vi) The syntax of while loop is as follows: initialization; while(condition) { statements; increment/decrement; } | (vi) The syntax of do-while loop is as follows: initialization; do { statements; increment/decrement; } while(condition); |
(vii) The operation of while loop can be represented using flowchart as follows: | (vii) The operation of do while loop can be represented using flowchart as follows: |
(viii) Example of while loop:
#include<stdio.h>
main()
{
int i=1;
while(i>=10)
{
printf("I love my country");
i++;
}
} Output: This program displays nothing as output; as condition is evaluated false in the beginning. |
(viii) Example of do while loop:
#include<stdio.h>
main()
{
int i=1;
do
{
printf("I love my country");
i++;
}while(i>=10);
}
Output: This program displays "I love my country" as output at once. |
for loop
It is the most common type of loop which is used to execute a program statement or block of program statements repeatedly for a specified number of times. It is a definite loop. Mainly it consists of three expressions: initialization, condition and increment / decrement. The initialization defines the loop starting point, condition defines the loop stopping points and counter helps to increment and decrement the value of counter variable.
{
statements;
}
Algorithm to calculate and display : 1 3 5 .... to 10th terms
- Start
- Set i = 0 and a=1
- Check is i<10?
3.1 If yes, Print ‘a’ and calculate a=a+2 and goto step 4
3.2 If no, go to step 5. - Calculate i=i+1 and go to step 3
- Stop
Code to calculate and display : 1 3 5 .... to 10th terms
#include <stdio.h> main() { int i=0,a=1; for(i=0;i<10;i++) { printf(" %d\t ",a); a=a+2; } }Output
1 3 5 7 9 11 13 15 17 19Algorithm to display a square series of first ‘n’ natural numbers
- Start
- Input a number as n
- Set i=1
- Check is i<=n?
4.1 If yes, Calculate sq=i*i ; Print “ sq ” and goto step 5
4.2 If no, goto step 6 - Calculate i=i+1 and go to step 4
- Stop
C Code to display a square series of first ‘n’ natural numbers
#include <stdio.h> main() { int i,n,sq; printf("Enter the value of n : "); scanf("%d",&n); for(i=1;i<=n;i++) { sq=i*i; printf(" %d\t ",sq); } }Output
Enter the value of n : 5 1 4 9 16 25Algorithm to calculate the factorial of any number given by the user
- Start
- Input a number as n
- Set f=1,i=n
- Check is i >=1?
4.1 If yes, Calculate f=f*i and goto step 5
4.2 If no, goto step 6 - Calculate i=i-1 and go to step 4
- Print f
- Stop
C Code to calculate the factorial of any number given by the user
#include<stdio.h> main() { int n,i,f=1; printf("Enter a number : "); scanf("%d",&n); for(i=n;i>=1;i--) { f=f*i; } printf("Factorial of %d = %d",n,f); }Output
Enter a number : 5Factorial of 5 = 120
Algorithm to calculate and display the multiplication table of a number
- Start
- Input a number as n
- Set i=1
- Check is i <=10?
4.1 If yes, Calculate and display n*i and goto step 5
4.2 If no, goto step 6 - Calculate i=i+1 and go to step 4
- Stop
C Code to calculate and display the multiplication table of a number
#include <stdio.h> main() { int i,n; printf("Enter the value of n : "); scanf("%d",&n); for(i=1;i<=10;i++) { printf("%d x %d = %d \n", n,i,n*i); } }Output
Enter the value of n : 2 2 x 1 = 2 2 x 2 = 4 2 x 3 = 6 2 x 4 = 8 2 x 5 = 10 2 x 6 = 12 2 x 7 = 14 2 x 8 = 16 2 x 9 = 18 2 x 10 = 20Algorithm to display Fibonacci series having ‘n’ terms
- Start
- Set a=0,b=1, i=0
- Ask the value of ‘n’
- Is i<n?
4.1 If yes, Print "a" and calculate c=a+b . Also set a=b , b=c
4.2 If no, go to step 6 - Calculate i=i+1 and go to step 4
- Stop
C Code to display Fibonacci series having ‘n’ terms
#include <stdio.h> main() { int a=0,b=1,c,n,i; printf(" Enter the value of n : "); scanf("%d",&n); for(i=0;i<n;i++) { printf(" %d ",a); c=a+b; a=b; b=c; } }Output
Enter the value of n : 10 0 1 1 2 3 5 8 13 21 34Algorithm to check if a given number is palindrome or not palindrome
- Start
- Initialize int n,r,sum=0 and a
- Ask to enter any number as n
- Set a=n
- Is n !=0?
5.1 If yes,
calculate r=n%10 ,
sum=sum*10+r and
n=n/10
5.2 If no, go to step 6 - Is sum = = a?
6.1 If yes, display “ Palindrome number ”
6.2 If no, display “ Not Palindrome number ” - Stop
C Code to check if a given number is palindrome or not palindrome
#include <stdio.h> main() { int n,r,sum=0,a; printf(" Enter the value of n : "); scanf("%d",&n); a=n; while(n!=0) { r=n%10; sum=sum*10+r; n=n/10; } if(sum==a) printf(" Palindrome number "); else printf(" Not Palindrome number "); }Output
Enter the value of n : 101 Palindrome number ----------------------- Enter the value of n : 123 Not Palindrome numberAlgorithm to calculate sum of given series
- Start
- Set sum=0
- Ask the value of n
- Set i=1
- Is i<=n?
5.1 If yes, calculate sum=sum+1/i and go to step 6
5.2 If no, go to step 7
- Calculate i=i+1 and go to step 5
- Print sum
- Stop
C Code to calculate sum of given series
#include <stdio.h> main() { float n,i,sum=0; printf(" Enter the value of n: "); scanf("%f",&n); for(i=1;i<=n;i++) { sum=sum+1/i; } printf(" Sum = %0.2f ",sum); }Output
Enter the value of n: 3 Sum = 1.831 2 3 4 5 2 4 6 8 10 3 6 9 12 15
Algorithm to display given number pattern:
- Start
- Set the outer loop(i) from 1 to 3
- Set the inner loop(j) from 1 to 5
- Calculate and print i*j
- Stop
C Code to display given number pattern:
#include <stdio.h> main() { int i,j; for(i=1;i<=3;i++) { for(j=1;j<=5;j++) { printf("%5d",i*j); } printf("\n"); } }Output
1 2 3 4 5 2 4 6 8 10 3 6 9 12 15Algorithm to check given number is prime or composite
- Start
- Initialize I=1, count=0
- Input any number as N
- Is I<=N?
4.1: If yes, Calculate remainder(R) of num divided by I and check R=0? If yes, calculate count=count+1 and go to step 5
4.2: If no, go to step 6 - Calculate I=I+1 and go to step 4
- Is count equal to two.
6.1: If yes, Print "Prime"
6.2: If no, Print "Composite" - Stop
C Code to check given number is prime or composite
#include <stdio.h> main() { int n,count=0,i; printf("Enter any number : "); scanf("%d",&n); for(i=1;i<=n;i++) { if(n%i==0) count++; } if(count==2) printf("%d is prime number.",n); else printf("%d is composite number.",n); }Output
Enter any number : 13 13 is prime number. ------------------- Enter any number : 4 4 is composite number.2 3 5 7 11 upto n
Algorithm to display prime series upto n
- Start
- Input any number as n
- Set i=1
- Is i<=n?
4.1 If yes, set j=1 , count=0
(a) Is j<=i?
(a1) If Yes, calculate remainder(R) of i divided by j
(a2) check R=0? If yes, calculate count=count+1 and j=j+1
If no, only calculate j=j+1 and go to step (a)
(b) If no, goto step 5
4.2: If no, go to step 7 - Is count equal to two.
5.1: If yes, Print i and goto step 6
5.2: If no, go to step 6 - Calculate i=i+1 and go to step 4
- Stop