Loops are one of the most powerful features of C programming. They allow a program to execute the same block of code repeatedly without writing it multiple times. This makes programs shorter, faster, and easier to maintain.
C provides three types of loops:
- for loop
- while loop
- do…while loop
Loops are widely used in real-world applications such as generating reports, processing arrays, validating user input, creating patterns, and solving mathematical problems. In this chapter, you’ll practice beginner-friendly loop programs with complete solutions, sample outputs, explanations, and concepts covered. C Loops practice questions with solutions help to understand the concepts.
1. C Program to Print Numbers from 1 to 10 Using a for Loop
Problem Statement
Write a C program to print numbers from 1 to 10 using a for loop.
C Solution
#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 initializes i with 1, checks the condition i <= 10, prints the value, and increments i by 1 after every iteration.
Concepts Covered
- for Loop
- Loop Initialization
- Loop Condition
- Loop Increment
2. C Program to Print Numbers from 10 to 1 Using a for Loop
Problem Statement
Write a C program to print numbers from 10 to 1 in reverse order.
C Solution
#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
The loop starts from 10 and decreases the value by 1 until it reaches 1.
Concepts Covered
- Reverse Loop
- Decrement Operator (
--) - for Loop
3. C Program to Print Even Numbers from 1 to 20
Problem Statement
Write a C program to print all even numbers between 1 and 20.
C Solution
#include <stdio.h>
int main()
{
int i;
for(i = 2; i <= 20; i += 2)
{
printf("%d\n", i);
}
return 0;
}
Sample Output
2
4
6
8
10
12
14
16
18
20
Explanation
The loop starts at 2 and increases the value by 2 after every iteration, ensuring only even numbers are printed.
Concepts Covered
- for Loop
- Increment by 2
- Even Numbers
- Loop Control
4. C Program to Print Odd Numbers from 1 to 20
Problem Statement
Write a C program to print all odd numbers between 1 and 20 using a for loop.
C Solution
#include <stdio.h>
int main()
{
int i;
for(i = 1; i <= 20; i += 2)
{
printf("%d\n", i);
}
return 0;
}
Sample Output
1
3
5
7
9
11
13
15
17
19
Explanation
The loop starts with 1 and increments the value by 2 after every iteration. This ensures that only odd numbers are printed.
Concepts Covered
- for Loop
- Odd Numbers
- Loop Increment
- Arithmetic Progression
5. C Program to Calculate the Sum of Natural Numbers
Problem Statement
Write a C program to calculate the sum of the first N natural numbers entered by the user.
C Solution
#include <stdio.h>
int main()
{
int n, i, sum = 0;
printf("Enter a positive number: ");
scanf("%d", &n);
for(i = 1; i <= n; i++)
{
sum = sum + i;
}
printf("Sum of first %d natural numbers = %d", n, sum);
return 0;
}
Sample Output
Enter a positive number: 10
Sum of first 10 natural numbers = 55
Explanation
The loop starts from 1 and continues until N. During each iteration, the current value of i is added to the variable sum.
For example:
1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10 = 55
Concepts Covered
- for Loop
- Accumulator Variable
- User Input
- Natural Numbers
6. C Program to Calculate the Factorial of a Number
Problem Statement
Write a C program to calculate the factorial of a given positive integer using a for loop.
C Solution
#include <stdio.h>
int main()
{
int number, i;
long long factorial = 1;
printf("Enter a positive number: ");
scanf("%d", &number);
for(i = 1; i <= number; i++)
{
factorial = factorial * i;
}
printf("Factorial of %d = %lld", number, factorial);
return 0;
}
Sample Output
Enter a positive number: 5
Factorial of 5 = 120
Explanation
The factorial of a number is the product of all positive integers from 1 to that number.
Example:
5! = 5 × 4 × 3 × 2 × 1 = 120
The loop multiplies each value from 1 to N and stores the result in the factorial variable.
Concepts Covered
- for Loop
- Multiplication
- Factorial Logic
- Accumulator Variable
7. C Program to Print the Multiplication Table of a Number
Problem Statement
Write a C program to print the multiplication table of a given number.
C Solution
#include <stdio.h>
int main()
{
int number, 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 executes 10 times, multiplying the entered number by values from 1 to 10.
Concepts Covered
- for Loop
- Multiplication
- Loop Counter
8. C Program to Count the Number of Digits in an Integer
Problem Statement
Write a C program to count the total number of digits in a given integer.
C Solution
#include <stdio.h>
int main()
{
int number, count = 0;
printf("Enter a number: ");
scanf("%d", &number);
while(number != 0)
{
number = number / 10;
count++;
}
printf("Total Digits = %d", count);
return 0;
}
Sample Output
Enter a number: 987654
Total Digits = 6
Explanation
Each time the number is divided by 10, its last digit is removed. The loop continues until the number becomes 0, while the counter keeps track of the total digits.
Concepts Covered
- while Loop
- Integer Division
- Counting Digits
9. C Program to Reverse a Number
Problem Statement
Write a C program to reverse a given integer.
C Solution
#include <stdio.h>
int main()
{
int number, reverse = 0, remainder;
printf("Enter a number: ");
scanf("%d", &number);
while(number != 0)
{
remainder = number % 10;
reverse = reverse * 10 + remainder;
number = number / 10;
}
printf("Reversed Number = %d", reverse);
return 0;
}
Sample Output
Enter a number: 12345
Reversed Number = 54321
Explanation
The program extracts the last digit using %, appends it to the reversed number, and removes the last digit using / 10.
Concepts Covered
- while Loop
- Modulus Operator
- Integer Division
- Reverse Number Logic
10. C Program to Check Whether a Number is a Palindrome
Problem Statement
Write a C program to determine whether a number is a palindrome.
C Solution
#include <stdio.h>
int main()
{
int number, original, reverse = 0, remainder;
printf("Enter a number: ");
scanf("%d", &number);
original = number;
while(number != 0)
{
remainder = number % 10;
reverse = reverse * 10 + remainder;
number = number / 10;
}
if(original == reverse)
{
printf("Palindrome Number");
}
else
{
printf("Not a Palindrome Number");
}
return 0;
}
Sample Output
Enter a number: 121
Palindrome Number
Explanation
A palindrome number remains the same when its digits are reversed.
Examples:
- 121
- 1331
- 444
The program reverses the number and compares it with the original value.
Concepts Covered
- while Loop
- Number Reversal
- Conditional Statements
- Palindrome Logic
11. C Program to Print the Fibonacci Series
Problem Statement
Write a C program to print the first N terms of the Fibonacci series.
C Solution
#include <stdio.h>
int main()
{
int n, first = 0, second = 1, next, i;
printf("Enter number of terms: ");
scanf("%d", &n);
printf("Fibonacci Series:\n");
for(i = 1; i <= n; i++)
{
printf("%d ", first);
next = first + second;
first = second;
second = next;
}
return 0;
}
Sample Output
Enter number of terms: 8
Fibonacci Series:
0 1 1 2 3 5 8 13
Explanation
The Fibonacci series starts with 0 and 1. Every next number is the sum of the previous two numbers.
Formula:
Next = Previous + Current
Concepts Covered
- for Loop
- Fibonacci Logic
- Variable Swapping
- Arithmetic Operations
12. C Program to Check Whether a Number is an Armstrong Number
Problem Statement
Write a C program to determine whether a given three-digit number is an Armstrong number.
C Solution
#include <stdio.h>
int main()
{
int number, original, remainder;
int sum = 0;
printf("Enter a number: ");
scanf("%d", &number);
original = number;
while(number != 0)
{
remainder = number % 10;
sum = sum + (remainder * remainder * remainder);
number = number / 10;
}
if(sum == original)
{
printf("Armstrong Number");
}
else
{
printf("Not an Armstrong Number");
}
return 0;
}
Sample Output
Enter a number: 153
Armstrong Number
Explanation
For a three-digit Armstrong number:
153 = 1³ + 5³ + 3³
153 = 1 + 125 + 27
153 = 153
Since both values are equal, it is an Armstrong number.
Concepts Covered
- while Loop
- Modulus Operator
- Number Processing
- Armstrong Number Logic
13. C Program to Print the Sum of Even Numbers Between 1 and N
Problem Statement
Write a C program to calculate the sum of all even numbers between 1 and N.
C Solution
#include <stdio.h>
int main()
{
int n, i, sum = 0;
printf("Enter a number: ");
scanf("%d", &n);
for(i = 2; i <= n; i += 2)
{
sum = sum + i;
}
printf("Sum of Even Numbers = %d", sum);
return 0;
}
Sample Output
Enter a number: 10
Sum of Even Numbers = 30
Explanation
The program starts from 2 and increments by 2 in every iteration, ensuring only even numbers are added.
Example:
2 + 4 + 6 + 8 + 10 = 30
Concepts Covered
- for Loop
- Even Numbers
- Accumulator Variable
14. C Program to Print the Sum of Odd Numbers Between 1 and N
Problem Statement
Write a C program to calculate the sum of all odd numbers between 1 and N.
C Solution
#include <stdio.h>
int main()
{
int n, i, sum = 0;
printf("Enter a number: ");
scanf("%d", &n);
for(i = 1; i <= n; i += 2)
{
sum = sum + i;
}
printf("Sum of Odd Numbers = %d", sum);
return 0;
}
Sample Output
Enter a number: 10
Sum of Odd Numbers = 25
Explanation
The loop starts at 1 and increments by 2, so only odd numbers are added.
Example:
1 + 3 + 5 + 7 + 9 = 25
Concepts Covered
- for Loop
- Odd Numbers
- Summation Logic
15. C Program to Find the Power of a Number Using a Loop
Problem Statement
Write a C program to calculate the power of a number using a for loop.
C Solution
#include <stdio.h>
int main()
{
int base, exponent, i;
long long result = 1;
printf("Enter base: ");
scanf("%d", &base);
printf("Enter exponent: ");
scanf("%d", &exponent);
for(i = 1; i <= exponent; i++)
{
result = result * base;
}
printf("%d^%d = %lld", base, exponent, result);
return 0;
}
Sample Output
Enter base: 2
Enter exponent: 5
2^5 = 32
Explanation
The loop multiplies the base by itself repeatedly until the required exponent is reached.
Example:
2 × 2 × 2 × 2 × 2 = 32
Concepts Covered
- for Loop
- Exponent Calculation
- Repeated Multiplication
Chapter Summary
In this chapter, you learned how loops automate repetitive tasks in C programming. You practiced using for and while loops to print number sequences, calculate sums, generate multiplication tables, compute factorials, reverse numbers, check palindrome and Armstrong numbers, generate Fibonacci series, and calculate powers. These looping techniques are essential for solving mathematical problems, processing data, and building efficient C applications.
Key Takeaways
- Loops eliminate repetitive code.
forloops are ideal when the number of iterations is known.whileloops are useful when the stopping condition depends on user input or calculations.- Loops are commonly used with arithmetic and conditional statements.
- Accumulator variables help calculate sums and products.
- Number-processing problems often combine loops with
%and/. - Nested logic inside loops enables complex problem-solving.
- Loops improve code readability and efficiency.
- Loop control variables determine how many times a loop executes.
- Mastering loops is essential before learning arrays and functions.
Frequently Asked Questions (FAQs)
1. What is a loop in C?
A loop is a control structure that repeatedly executes a block of code until a specified condition becomes false.
2. What are the different types of loops in C?
C provides three types of loops:
forwhiledo...while
3. When should you use a for loop?
Use a for loop when the number of iterations is known in advance.
4. What is the purpose of a while loop?
A while loop executes as long as its condition remains true, making it suitable for condition-based repetition.
5. How is a factorial calculated?
The factorial of a positive integer is the product of all integers from 1 to that number.
Example:
5! = 5 × 4 × 3 × 2 × 1 = 120
6. What is a Fibonacci series?
A Fibonacci series is a sequence where each number is the sum of the previous two numbers, starting with 0 and 1.
7. What is an Armstrong number?
An Armstrong number is a number that is equal to the sum of the cubes of its digits (for three-digit numbers).
Example:
153 = 1³ + 5³ + 3³ = 153
8. Why are loops important in C programming?
Loops are essential because they automate repetitive tasks, reduce code duplication, improve efficiency, and are widely used in algorithms, data processing, pattern generation, arrays, searching, sorting, and many real-world software applications.
Written by Shubhranshu Shekhar, who has trained 20000+ students in coding.
