Introduction
The for loop is one of the most commonly used loops in C. It is useful when you know how many times a block of code needs to run. In this chapter, you will practice the for loop with 10 solved examples, starting with simple counting and tables and gradually moving to sums, factorials, numbers, and patterns. Each example focuses on practical logic so beginners can understand how initialization, condition, and update work together. For Loop in C Practice Questions with Solutions to help you understand the concepts.
Q1. Print Numbers from 1 to 10
Problem Statement
Write a C program to print numbers from 1 to 10 using a for loop.
C Program
#include <stdio.h>
int main()
{
int i;
for (i = 1; i <= 10; i++)
{
printf("%d\n", i);
}
return 0;
}
Sample Output
1
2
3
4
5
6
7
8
9
10
Explanation
The for loop has three important parts:
for (i = 1; i <= 10; i++)
i = 1→ initializationi <= 10→ conditioni++→ update
The loop continues while i <= 10.
Concepts Covered
forloop- Initialization
- Condition
- Increment
- Counter variable
Q2. Print Even Numbers from 1 to 20
Problem Statement
Write a C program to print all even numbers between 1 and 20.
C Program
#include <stdio.h>
int main()
{
int i;
for (i = 1; i <= 20; i++)
{
if (i % 2 == 0)
{
printf("%d\n", i);
}
}
return 0;
}
Sample Output
2
4
6
8
10
12
14
16
18
20
Explanation
The loop checks every number from 1 to 20.
The condition:
i % 2 == 0
checks whether the number is even.
Concepts Covered
forloopif- Modulus operator
% - Even numbers
Q3. Print Numbers in Reverse
Problem Statement
Write a C program to print numbers from 10 down to 1.
C Program
#include <stdio.h>
int main()
{
int i;
for (i = 10; i >= 1; i--)
{
printf("%d\n", i);
}
return 0;
}
Sample Output
10
9
8
7
6
5
4
3
2
1
Explanation
This time, the counter starts at 10:
i = 10
The condition is:
i >= 1
After every iteration, i decreases:
i--
Concepts Covered
- Reverse loop
- Decrement operator
forloop- Counter control
Q4. Print Multiplication Table
Problem Statement
Write a C program that accepts a number from the user and prints its multiplication table from 1 to 10.
C Program
#include <stdio.h>
int main()
{
int number;
int i;
printf("Enter a number: ");
scanf("%d", &number);
for (i = 1; i <= 10; i++)
{
printf("%d x %d = %d\n", number, i, number * i);
}
return 0;
}
Sample Output
Enter a number: 7
7 x 1 = 7
7 x 2 = 14
7 x 3 = 21
7 x 4 = 28
7 x 5 = 35
7 x 6 = 42
7 x 7 = 49
7 x 8 = 56
7 x 9 = 63
7 x 10 = 70
Explanation
The loop runs 10 times.
For every iteration, the value of i changes:
1 → 2 → 3 → ... → 10
The multiplication is calculated using:
number * i
Concepts Covered
forloop- User input
- Multiplication
- Formatted output
Q5. Find the Sum of Numbers from 1 to N
Problem Statement
Write a C program that accepts a number N and calculates the sum of all numbers from 1 to N.
For example:
N = 5
1 + 2 + 3 + 4 + 5 = 15
C Program
#include <stdio.h>
int main()
{
int n;
int i;
int sum = 0;
printf("Enter a number: ");
scanf("%d", &n);
for (i = 1; i <= n; i++)
{
sum = sum + i;
}
printf("Sum = %d", sum);
return 0;
}
Sample Output
Enter a number: 5
Sum = 15
Explanation
Initially:
sum = 0;
The loop adds each number to sum.
sum = 0 + 1
sum = 1 + 2
sum = 3 + 3
sum = 6 + 4
sum = 10 + 5
Final result:
15
Concepts Covered
forloop- Accumulator variable
- Addition
- User input
Q6. Find the Factorial of a Number
Problem Statement
Write a C program to calculate the factorial of a number using a for loop.
For example:
5! = 5 × 4 × 3 × 2 × 1 = 120
C Program
#include <stdio.h>
int main()
{
int n;
int i;
long long factorial = 1;
printf("Enter a non-negative integer: ");
scanf("%d", &n);
if (n < 0)
{
printf("Factorial is not defined for negative numbers.");
}
else
{
for (i = 1; i <= n; i++)
{
factorial = factorial * i;
}
printf("Factorial = %lld", factorial);
}
return 0;
}
Sample Output
Enter a non-negative integer: 5
Factorial = 120
Explanation
The factorial starts with:
factorial = 1;
Then the loop multiplies it by every number from 1 to n.
For 5:
1 × 1 = 1
1 × 2 = 2
2 × 3 = 6
6 × 4 = 24
24 × 5 = 120
Concepts Covered
forloop- Multiplication
- Accumulator
if-else- Factorial
Q7. Count Numbers Divisible by 3
Problem Statement
Write a C program to print and count all numbers between 1 and 50 that are divisible by 3.
C Program
#include <stdio.h>
int main()
{
int i;
int count = 0;
for (i = 1; i <= 50; i++)
{
if (i % 3 == 0)
{
printf("%d\n", i);
count++;
}
}
printf("Total numbers = %d", count);
return 0;
}
Sample Output
3
6
9
12
15
18
21
24
27
30
33
36
39
42
45
48
Total numbers = 16
Explanation
The condition:
i % 3 == 0
checks whether i is divisible by 3.
Every time the condition is true:
count++;
increases the counter by 1.
Concepts Covered
forloopif- Modulus
- Counter variable
- Divisibility
Q8. Find the Largest Number Among N Numbers
Problem Statement
Write a C program that asks the user how many numbers they want to enter and then finds the largest number using a for loop.
C Program
#include <stdio.h>
int main()
{
int n;
int i;
int number;
int largest;
printf("How many numbers do you want to enter? ");
scanf("%d", &n);
if (n <= 0)
{
printf("Please enter a positive count.");
return 0;
}
printf("Enter number 1: ");
scanf("%d", &largest);
for (i = 2; i <= n; i++)
{
printf("Enter number %d: ", i);
scanf("%d", &number);
if (number > largest)
{
largest = number;
}
}
printf("Largest number = %d", largest);
return 0;
}
Sample Output
How many numbers do you want to enter? 5
Enter number 1: 25
Enter number 2: 78
Enter number 3: 12
Enter number 4: 90
Enter number 5: 45
Largest number = 90
Explanation
The first number is stored as the initial largest value.
Then each new number is compared:
if (number > largest)
{
largest = number;
}
If the new number is larger, it becomes the new largest number.
Concepts Covered
forloop- Comparison
- Counter
- Finding maximum
- User input
Q9. Print a Star Pattern
Problem Statement
Write a C program to print the following pattern using a for loop:
*
**
***
****
*****
C Program
#include <stdio.h>
int main()
{
int i;
for (i = 1; i <= 5; i++)
{
int j;
for (j = 1; j <= i; j++)
{
printf("*");
}
printf("\n");
}
return 0;
}
Sample Output
*
**
***
****
*****
Explanation
This example uses a for loop inside another for loop.
The outer loop controls the number of rows.
The inner loop controls the number of stars.
For example, when:
i = 3
the inner loop runs three times and prints:
***
Concepts Covered
forloop- Nested
forloop - Pattern printing
- Inner loop
- Outer loop
Q10. Print Prime Numbers from 1 to N
Problem Statement
Write a C program that accepts a number N and prints all prime numbers from 2 to N.
C Program
#include <stdio.h>
int main()
{
int n;
int i, j;
int isPrime;
printf("Enter the limit: ");
scanf("%d", &n);
printf("Prime numbers: ");
for (i = 2; i <= n; i++)
{
isPrime = 1;
for (j = 2; j * j <= i; j++)
{
if (i % j == 0)
{
isPrime = 0;
break;
}
}
if (isPrime == 1)
{
printf("%d ", i);
}
}
return 0;
}
Sample Output
Enter the limit: 30
Prime numbers: 2 3 5 7 11 13 17 19 23 29
Explanation
The outer loop checks every number from 2 to n.
For each number, the inner loop checks whether it can be divided evenly by another number.
For example, for 7:
7 % 2 != 0
No divisor is found, so 7 is prime.
For 12:
12 % 2 == 0
A divisor is found, so 12 is not prime.
The inner loop only checks up to:
j * j <= i
because a composite number must have at least one factor less than or equal to its square root.
Concepts Covered
forloop- Nested
forloop ifbreak- Modulus operator
- Prime number logic
Key Takeaways
- A
forloop repeats a block of code. - A typical
forloop contains initialization, condition, and update. - The condition is checked before each iteration.
i++increases the counter by one.i--decreases the counter by one.i += 2increases the value by two.- A
forloop can be combined withifstatements for filtering values. - A
forloop can be placed inside anotherforloop to create nested loops. forloops are commonly used for tables, counting, sums, factorials, searching, and patterns.breakcan be used to stop a loop before its normal condition becomes false.- Be careful with the update expression; forgetting to update the loop variable can create an unintended infinite loop.
FAQs
1. What is a for loop in C?
A for loop repeatedly executes a block of code while its condition remains true.
Example:
for (i = 1; i <= 5; i++)
{
printf("%d ", i);
}
2. What are the three parts of a for loop?
A typical for loop contains:
for (initialization; condition; update)
For example:
for (i = 1; i <= 10; i++)
Here:
i = 1is initialization.i <= 10is the condition.i++is the update.
3. Can a for loop run backward?
Yes.
for (i = 10; i >= 1; i--)
{
printf("%d ", i);
}
This prints numbers from 10 down to 1.
4. Can we use if inside a for loop?
Yes. This is very common.
for (i = 1; i <= 20; i++)
{
if (i % 2 == 0)
{
printf("%d ", i);
}
}
This prints even numbers from 1 to 20.
5. Can we use a for loop inside another for loop?
Yes. This is called a nested for loop.
Nested loops are commonly used for patterns, matrices, tables, and other problems involving rows and columns.
6. What happens if the for loop condition is false at the beginning?
The loop body does not execute even once.
For example:
for (i = 10; i <= 5; i++)
{
printf("%d", i);
}
Since 10 <= 5 is false initially, the loop is skipped.
7. Can we leave a part of the for loop empty?
Yes. The three expressions are optional, although the semicolons must remain.
For example:
int i = 1;
for (; i <= 5; i++)
{
printf("%d ", i);
}
Here, initialization is done before the loop.
Written by Shubhranshu Shekhar, who has trained 20000+ students in coding.
