CHAPTER 4 INTRODUCTION TO LOOPS
Q: What is a loop ?
Ans: A loop is a control structure that repeats a block of code multiple times until a codition becomes
false.
Q: How many types of loops ? describe.
Ans: There are three types of loops
• While Loop
• Do-While Loop
• For Loop
While Loop: A while loop repeatedly executes a block of code as long as a given condition is true.
Syntax:
while (condition)
{
//statements
}
//outside statements
Do-While loop: A do-while loop executes a block code at least once and then repeats it as long as
the condition is true.
Syntax:
Do {
// statements
}
While(condition);
For Loop: A for loop repeats a block of code a specific number of times , using an initialization ,
condition checking or testing expression and update expression.
Syntax:
for (initialization expression; test expression; update expression)
{
// body of the loop statements we want to execute
}
//outside statement
Q: Write a program to print “I read in class X under SEBA” 5 times.
Ans:
#include<stdio.h>
int main()
{
int i = 0;
while (i < 5)
{
printf("I read in class X under SEBA \n");
i++;
}
return 0;
}
Q: Write a program to print “I read in class X under SEBA” 5 times using a do-while loop.
Ans:
#include<stdio.h>
int main()
{
int i = 0;
do
{
printf("I read in class X under SEBA \n");
i++;
}
while (i < 5);
return 0;
}
Q: Write a program for finding summation of integers using do-while loop.
CHAPTER 4 INTRODUCTION TO LOOPS
Ans:
#include<stdio.h>
int main()
{
int var, choice =1;
int sum = 0;
do
{
printf("\n Enter a number: ");
scanf("%d", &var);
printf("\n You entered %d", var);
sum = sum + var;
printf("\n Do you want to stop? Enter 0, otherwise press any integer: ");
scanf("%d", &choice);
}
while (choice != 0);
printf("\n Summation of the numbers entered till now is: %d", sum);
return 0;
}
Q: Draw a Flowchart for finding summation of integers using loop.
Ans:
Flowchart description:
Oval → Start / End
Parallelogram → Input /Output
Rectangle → Processing ( assignments, sum calculation)
Diamond → Decision ( condition check i<=N)
Q: Write a c program to print “I read in class X under SEBA” 5 times using a for loop.
Ans:
#include<stdio.h>
int main()
{
int i;
CHAPTER 4 INTRODUCTION TO LOOPS
for (i = 0; i < 5; i++)
{
printf("I read in class X under SEBA \n");
}
return 0;
}
Q: Write a c program to print the summation of a series 1+2+3+4+ . . . +N.
Ans:
#include<stdio.h>
int main()
{
int i, N, sum=0;
printf("Enter the value of N: ");
scanf("%d",&N);
for (i = 1; i <= N; i++)
{
sum = sum + i;
}
printf("\n The summation is %d", sum);
return 0;
}
Q: Write a program to print the summation of N numbers entered using the keyboard.
Ans:
#include<stdio.h>
int main()
{
int i, N, var, sum=0;
printf("Enter the value of N: ");
scanf("%d", &N);
for (i = 1; i <= N; i++)
{
printf("\n Enter a number: ");
scanf("%d", &var);
printf("\n You have entered: %d", var);
sum = sum + var;
}
printf("\n The summation is %d", sum);
return 0;
}
Q: Write a C program to display the following pattern on our monitor.
X
XX
XXX
XXXX
XXXXX
Ans:
#include<stdio.h>
int main()
{
int i;
for (i=1; i <= 1; i++)
{
printf(" X ");
}
CHAPTER 4 INTRODUCTION TO LOOPS
printf("\n");
for (i=1; i <= 2; i++)
{
printf(" X ");
}
printf("\n");
for (i=1; i <= 3; i++)
{
printf(" X ");
}
printf("\n");
for (i=1; i <= 4; i++)
{
printf(" X ");
}
printf("\n");
for (i=1; i <= 5; i++)
{
printf(" X ");
}
printf("\n");
return 0;
}
Q: Write a C program to display the following pattern on our monitor.
XXXXX
XXXX
XXX
XX
X
Ans:
#include<stdio.h>
int main()
{
int i;
f or (i=1; i <= 5; i++)
{
printf(" X ");
}
printf("\n");
for (i=1; i <= 4; i++)
{
printf(" X ");
}
printf("\n");
for (i=1; i <= 3; i++)
{
printf(" X ");
}
printf("\n");
for (i=1; i <= 2; i++)
{
printf(" X ");
}
printf("\n");
for (i=1; i <= 1; i++)
CHAPTER 4 INTRODUCTION TO LOOPS
{
printf(" X ");
}
printf("\n");
return 0;
}
Q: Write a C program to display the following pattern on our monitor.
XXXXX
XXXX
XXX
XX
X
XX
XXX
XXXX
XXXXX
Ans:
#include<stdio.h>
int main()
{
int i;
for (i=1; i <= 5; i++)
{
printf(" X ");
}
printf("\n");
for (i=1; i <= 4; i++)
{
printf(" X ");
}
printf("\n");
for (i=1; i <= 3; i++)
{
printf(" X ");
}
{
printf("\n");
for (i=1; i <= 2; i++)
printf(" X ");
}
printf("\n");
for (i=1; i <= 1; i++)
{
printf(" X ");
}
printf("\n");
for (i=1; i <= 2; i++)
{
printf(" X ");
}
printf("\n");
for (i=1; i <= 3; i++)
{
printf(" X ");
}
printf("\n");
CHAPTER 4 INTRODUCTION TO LOOPS
for (i=1; i <= 4; i++)
{
printf(" X ");
}
printf("\n");
for (i=1; i <= 5; i++)
{
printf(" X ");
}
printf("\n");
return 0;
}
Q: Write a c program to extract the individual digits from a given integer 8724.
Ans:
#include<stdio.h>
int main()
{
int number, temp, digit;
printf("Enter the integer: ");
scanf("%d", &number);
temp = number;
while(temp > 0)
{
digit = temp % 10;
printf("\n Extracted digit is %d", digit);
temp = temp / 10;
}
printf("\n");
return 0;
}
Q:Write a program to find the summation of digits of a given integer 8724.
Ans:
#include<stdio.h>
int main()
{
int number, temp, digit, sum=0;
printf("Enter the integer: ");
scanf("%d", &number);
temp = number;
while ( temp > 0 )
{
digit = temp % 10;
printf("\n Extracted digit is %d", digit);
sum = sum + digit;
temp = temp / 10;
}
printf("\n The summation is: %d", sum);
printf("\n");
return 0;
}
Q: Write a c program to check whether a number is prime or not
Ans:
#include<stdio.h>
int main()
{
CHAPTER 4 INTRODUCTION TO LOOPS
int number, i, flag = 0;
printf("Please input a number: ");
scanf("%d", &number);
for( i=2; i <= number/2; i++ )
{
if ( number % i = = 0 )
{
flag = 1;
break;
}
}
if ( flag = = 0 && number != 1 )
printf("%d is a prime number.\n",number);
else
printf("%d is not a prime number",number);
return 0;
}
Exercise
1. Why do we use a loop in a C program?
Ans: We use a loop in C program to repeat a set of instructions multiple times without writing the
code again and again.
2. Do we need to use only one type of loop in a C program? Justify your answer by
writing a C program.
Ans: No, we don’t need to use only one type of loop in a C program.
C provides for, while, and do-while loops, and we can use whichever suits the situation.
For Example:
#include <stdio.h>
int main() {
int i = 1;
// using for loop
for(i = 1; i <= 3; i++) {
printf("For loop: %d\n", i);
}
// using while loop
i = 1;
while(i <= 3) {
printf("While loop: %d\n", i);
i++;
}
// using do-while loop
i = 1;
do {
printf("Do-while loop: %d\n", i);
i++;
} while(i <= 3);
return 0;
}
It shows we can use different loops in the same program depending on the need.
3. What will happen if we write a while loop with 1 in place of the condition ? Try it in a
simple C program. Hint:
CHAPTER 4 INTRODUCTION TO LOOPS
while (1)
{
printf("We must raise our voice against corruption \n");
}
Ans:
If we write while(1) in C, it creates an infinite loop because 1 always means true.
So, the message will keep printing forever until we stop the program manually.
For example:
#include <stdio.h>
int main() {
while(1) {
printf("We must raise our voice against corruption\n");
}
return 0;
}
Output: The line will repeat endlessly.
4. Name different portions of a for loop. Can we put more than one statement within a
portion?
Ans:
A for loop in C has 3 portions:
1. Initialization – sets the starting value (e.g., int i = 0)
2. Condition / Test expression – checked before each iteration (e.g., i < 5)
3. Update / Increment-Decrement – changes the loop variable (e.g., i++)
Yes, we can put more than one statement in a portion by separating them with commas ,.
Example:
for(int i=0, j=10; i<5 && j>5; i++, j--) {
printf("i=%d, j=%d\n", i, j);
}
5. Answer with TRUE or FALSE.
(i) If the condition of the while loop is false, the control comes to the second statement
inside the loop.
(ii) We can use at most three loops in a single C program.
(iii) The statements inside the do-while loop executes at least once even if the condition is
false.
(iv) Only the first statement inside the do-while loop executes when the condition is false.
(v) In a do-while loop, the condition is written at the end of the loop.
Ans:
(i) FALSE – If the condition is false, the loop body doesn’t execute at all.
(ii) FALSE – We can use any number of loops in a C program.
(iii) TRUE – do-while executes at least once even if the condition is false.
(iv) FALSE – All statements inside the do-while loop execute once before checking condition.
(v) TRUE – In do-while, the condition is written at the end.
6. Programming exercises:
A. Write a C program to find the summation of the following series
(a). 12 + 22 + 32 + 42 + . . . + N2
(b). 13 + 23 + 33 + 43 + . . . + N3
(c). 1*2 + 2*3 + 3*4 + . . . + N*(N+1)
135B. Write a C program to continuously take a number as input and announce whether the
number is odd or even. Hint: use do-while loop.
C. Write a C program to display the following pattern.
1
11
111
CHAPTER 4 INTRODUCTION TO LOOPS
1111
11111
D. Write a C program to display the following pattern.
5
54
543
5432
54321
E. Write a C program to display the following pattern.
54321
5432
543
54
5
Ans:
A. Summation
#include <stdio.h>
int main() {
int N, i, sum = 0;
// (a) 1² + 2² + ... + N²
printf("Enter N for sum of squares: ");
scanf("%d", &N);
sum = 0;
for(i=1; i<=N; i++) sum += i*i;
printf("Sum of squares = %d\n", sum);
// (b) 1³ + 2³ + ... + N³
printf("Enter N for sum of cubes: ");
scanf("%d", &N);
sum = 0;
for(i=1; i<=N; i++) sum += i*i*i;
printf("Sum of cubes = %d\n", sum);
// (c) 1*2 + 2*3 + ... + N*(N+1)
printf("Enter N for sum of i*(i+1): ");
scanf("%d", &N);
sum = 0;
for(i=1; i<=N; i++) sum += i*(i+1);
printf("Sum of i*(i+1) = %d\n", sum);
return 0;
}
B. Odd or Even Do- While
#include <stdio.h>
int main() {
int num, choice;
do {
printf("Enter a number: ");
scanf("%d", &num);
if(num % 2 == 0)
printf("%d is Even\n", num);
else
printf("%d is Odd\n", num);
CHAPTER 4 INTRODUCTION TO LOOPS
C.
#include <stdio.h>
int main() {
int i, j;
for(i=1; i<=5; i++) {
for(j=1; j<=i; j++) {
printf("1 ");
}
printf("\n");
}
return 0;
}
D.
#include <stdio.h>
int main() {
int i, j;
for(i=1; i<=5; i++) {
for(j=5; j>=6-i; j--) {
printf("%d ", j);
}
printf("\n");
}
return 0;
}
E.
#include <stdio.h>
int main() {
int i, j;
for(i=5; i>=1; i--) {
for(j=5; j>=6-i; j--) {
printf("%d ", j);
}
printf("\n");
}
return 0;
}
**********************************************