Contents
Looping Or Iteration Structure (Looping Structure and Nested Loop) in C
Introduction
The term repetition or looping means executing the same section of code more than once if the given condition is true. A section of code may either be executed a fixed number of times or while the condition is true. If we want to print “C is a programming language” 10 times. It is difficult to write 10 times printf() statement than with the help of looping such type of program can be written very easily.
This is a type of structure or logic that controls the track of the block of statements to be executed up to a fixed number of times or until a condition is satisfied. This is also known as repetitive structure.
For loop, while loop, do-while loop, etc. are looping structure statements.
Flowchart

Example:
Program to print even numbers from 2 to 100.
#include <stdio.h> #include <conio.h> void main() { clrscr(); int i = 1; do { printf("%d", i); i = i + 2; } while (i <= 100); getch(); }
Looping Statements
The statements which are used to repeat and execute a series or block of statements up to a fixed number of times while the condition is true are known as looping or iteration statements. The following are the common looping statements used in C language.
i. for loop
This is one of the simple and common looping statements which execute the program code up to a fixed number of times so it is called a definite looping statement. The syntax of for loop is as follows:
for (initialization; test condition; increment or decrement) { Statement(s); }
Example:
// Program to print 1 to 100 using for loop in C programming language #include <stdio.h> #include <conio.h> main() { int i; clrscr(); for (i = 1; i <= 100; i++) { printf("n%d", i); } getch(); }
ii. while loop
The while is a looping statement that keeps repeating an action until an associated condition returns false. This is useful where the programmer does not know in advance how many times the loop will be traversed.
Syntax:
while (test condition) { statement; ………..; }
Flowchart

// WAP to print the sum of digit of any number using C programming #include <stdio.h> #include <conio.h> void main() { int n, sum = 0, rem; printf("enter the number: "); scanf("%d", &n); while (n > 0) { rem = n % 10; //taking last digit of number sum += rem; n /= 10; //skipping last digit } printf("sum of the digits =%dn", sum); }
iii. do…while loop
The do-while is a looping statement also similar to a while loop which executes a block of statements while the condition is satisfied. But in comparison to the while loop, this loop checks the condition after the loop body is executed. This ensures that the loop body is run at least once even if the condition is false.
Syntax of do…while loop
do { statement(s); }while (test condition);

In a do-while loop, the body of the loop is executed at least once before the condition is evaluated. Then the loop repeats body as long as the condition is true.
// WAP to print 1 to 100 using do...while loop using C programming #include <stdio.h> #include <conio.h> void main() { int i = 1; do { printf("%d", i); i = i + 1; } while (i <= 100); getch(); }
Nested Looping
When a loop is written inside the body of another loop, then it is known as the nesting of loops. Any type of loop can be nested inside any other type of loop. So a loop inside another loop is called a nested loop. For example: nested for loop.
for (counter1=start value; condition; increment/ decrement) { statements for (counter2=start value; condition; increment/ decrement) { statement(s); } }
Example:
Program to print multiplication table of 2 to 10 up to 10 multiples using nested for loop using C programming.
// WAP to print multiplication table of 2 to 10 upto 10 minutes using nested for loop using C programming. #include <stdio.h> #include <conio.h> main() { int i, j, c; for (i = 2; i < 10; i++) { for (j = 1; j <= i; j++) { c = i * j; printf("%d ", c); } printf("n"); } getch(); }
Some workout examples with looping statements.
// WAP to print multiplication table of 1 to 10 upto 10 minutes using nested for loop using C programming. #include <stdio.h> #include <conio.h> main() { int i, j, c; for (i = 1; i < 10; i++) { for (j = 1; j <= i; j++) { c = i * j; printf("%dt", c); } printf("n"); } getch(); }
// WAP to print fibonacci series upto n terms using C programming #include <stdio.h> #include <conio.h> void main() { int i, a = 1, b = 1, c; int n; printf("how many number"); scanf("%d", &n); printf("%d/t%d", a, b); for (i = 0; i < n - 2; i++) { c = a + b; printf("t%d", c); a = b; b = c; } getch(); }
// WAP to findout factorial of a given number using C programming #include <stdio.h> #include <conio.h> void main() { int n, fact = 1; printf("Enter the number: "); scanf("%d", &n); while (n > 0) { fact = fact * n; n--; } printf("Factorial of number is %d", fact); getch(); }
// WAP to check prime or composite number using C programming #include <stdio.h> #include <conio.h> void main() { int i, num, c = 0; printf("Enter the number: "); scanf("%d", &num); for (i = 1; i <= num; i++) { if (num % i == 0) { c = c + 1; } } if (c == 2) { printf("The entered number is prime and the number is %d", num); } else { printf("The entered number is composite and the number is %d", num); } getch(); }
// WAP to reverse given string using C programming language. #include <stdio.h> #include <conio.h> #include <string.h> main() { char ch[50]; printf("Enter a string to be reversedn"); scanf("%s", &ch); for (int i = strlen(ch) - 1; i >= 0; i--) { printf("%c", ch[i]); } getch(); }
Some Important Practice Questions
- To display the name of the day on the basis of entered numbers 1 to 7 for example 1 for Sunday…
- To find out the sum of 50 natural numbers, i.e. 1+2+3…50.
- To display the sun of ‘n’ terms of even numbers.
- To find out the sum of the first 10 square numbers.
- To find out the reverse of a number.
- To read 20 positive integers and their multiplication table.
- To print the first ten terms of any series.
- To input a number and check whether the number is Armstrong or not.