Introduction
The while and do-while loops are used when a C program needs to repeat a block of code based on a condition. A while loop checks the condition before executing the loop body, while a do-while loop executes the body at least once before checking the condition. In this chapter, you will practice both loops with 10 easy-to-understand examples covering counting, sums, digits, menus, validation, and practical number problems. while and do-while Loops in C practice questions with solutions to help you understand the concepts.
Q1. Print Numbers from 1 to 10 Using while
Problem Statement
Write a C program to print numbers from 1 to 10 using a while loop.
C Program
#include <stdio.h>
int main()
{
int i = 1;
while (i <= 10)
{
printf("%d\n", i);
i++;
}
return 0;
}
Sample Output
1
2
3
4
5
6
7
8
9
10
Explanation
The variable starts with:
i = 1;
The while loop checks:
i <= 10
If the condition is true, the loop body executes.
After printing the number, i++ increases its value by 1.
The loop stops when i becomes 11.
Concepts Covered
whileloop- Initialization
- Condition
- Increment
- Counter variable
Q2. Print Numbers from 10 to 1
Problem Statement
Write a C program to print numbers from 10 down to 1 using a while loop.
C Program
#include <stdio.h>
int main()
{
int i = 10;
while (i >= 1)
{
printf("%d\n", i);
i--;
}
return 0;
}
Sample Output
10
9
8
7
6
5
4
3
2
1
Explanation
This time, the counter starts at 10.
After every iteration:
i--;
decreases the value by 1.
The loop continues while:
i >= 1
Concepts Covered
while- Decrement operator
- Reverse counting
- Loop condition
Q3. 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 numbers from 1 to N using a while loop.
For example:
N = 5
1 + 2 + 3 + 4 + 5 = 15
C Program
#include <stdio.h>
int main()
{
int n;
int i = 1;
int sum = 0;
printf("Enter a number: ");
scanf("%d", &n);
while (i <= n)
{
sum = sum + i;
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
The final result is 15.
Concepts Covered
whileloop- Accumulator
- Addition
- User input
Q4. Count the Digits of a Number
Problem Statement
Write a C program to count how many digits are present in an integer using a while loop.
C Program
#include <stdio.h>
int main()
{
int number;
int count = 0;
printf("Enter a number: ");
scanf("%d", &number);
if (number == 0)
{
count = 1;
}
else
{
if (number < 0)
{
number = -number;
}
while (number != 0)
{
number = number / 10;
count++;
}
}
printf("Number of digits = %d", count);
return 0;
}
Sample Output
Enter a number: 5832
Number of digits = 4
Explanation
Every time we divide an integer by 10, its last digit is removed.
For example:
5832 / 10 = 583
583 / 10 = 58
58 / 10 = 5
5 / 10 = 0
The loop runs four times, so the number has four digits.
Concepts Covered
while- Integer division
- Digit counting
- Negative numbers
if-else
Q5. Find the Sum of Digits
Problem Statement
Write a C program to find the sum of all digits of a number.
For example:
5832
5 + 8 + 3 + 2 = 18
C Program
#include <stdio.h>
int main()
{
int number;
int digit;
int sum = 0;
printf("Enter a number: ");
scanf("%d", &number);
if (number < 0)
{
number = -number;
}
while (number != 0)
{
digit = number % 10;
sum = sum + digit;
number = number / 10;
}
printf("Sum of digits = %d", sum);
return 0;
}
Sample Output
Enter a number: 5832
Sum of digits = 18
Explanation
The % operator extracts the last digit:
digit = number % 10;
For 5832:
5832 % 10 = 2
583 % 10 = 3
58 % 10 = 8
5 % 10 = 5
The digits are then added together.
Concepts Covered
while- Modulus operator
- Integer division
- Digit extraction
- Accumulator
Q6. Reverse a Number
Problem Statement
Write a C program to reverse a number using a while loop.
For example:
12345 → 54321
C Program
#include <stdio.h>
int main()
{
int number;
int digit;
int reverse = 0;
printf("Enter a number: ");
scanf("%d", &number);
while (number != 0)
{
digit = number % 10;
reverse = reverse * 10 + digit;
number = number / 10;
}
printf("Reversed number = %d", reverse);
return 0;
}
Sample Output
Enter a number: 12345
Reversed number = 54321
Explanation
For 12345:
Last digit = 5
reverse = 0 × 10 + 5 = 5
Last digit = 4
reverse = 5 × 10 + 4 = 54
Last digit = 3
reverse = 54 × 10 + 3 = 543
The same process continues until the original number becomes 0.
Concepts Covered
while%operator/operator- Number reversal
- Digit extraction
Q7. Check Whether a Number Is Palindrome
Problem Statement
Write a C program to check whether a number is a palindrome.
A palindrome reads the same forward and backward.
Examples:
121 → Palindrome
1331 → Palindrome
123 → Not Palindrome
C Program
#include <stdio.h>
int main()
{
int number;
int original;
int digit;
int reverse = 0;
printf("Enter a number: ");
scanf("%d", &number);
original = number;
while (number != 0)
{
digit = number % 10;
reverse = reverse * 10 + digit;
number = number / 10;
}
if (original == reverse)
{
printf("The number is a palindrome.");
}
else
{
printf("The number is not a palindrome.");
}
return 0;
}
Sample Output
Enter a number: 1221
The number is a palindrome.
Explanation
First, the original number is saved:
original = number;
Then the number is reversed using the while loop.
Finally:
if (original == reverse)
checks whether the original and reversed numbers are equal.
Concepts Covered
while- Number reversal
if-else- Palindrome logic
Q8. Print Multiplication Table Using while
Problem Statement
Write a C program that accepts a number and prints its multiplication table from 1 to 10 using a while loop.
C Program
#include <stdio.h>
int main()
{
int number;
int i = 1;
printf("Enter a number: ");
scanf("%d", &number);
while (i <= 10)
{
printf("%d x %d = %d\n", number, i, number * i);
i++;
}
return 0;
}
Sample Output
Enter a number: 8
8 x 1 = 8
8 x 2 = 16
8 x 3 = 24
8 x 4 = 32
8 x 5 = 40
8 x 6 = 48
8 x 7 = 56
8 x 8 = 64
8 x 9 = 72
8 x 10 = 80
Explanation
The counter starts at 1 and continues until 10.
The multiplication is performed using:
number * i
After every iteration:
i++;
moves the counter to the next value.
Concepts Covered
while- Counter
- Multiplication
- User input
Q9. Create a Simple Menu Using do-while
Problem Statement
Create a simple menu using a do-while loop.
The menu should repeatedly display:
1. Say Hello
2. Say Welcome
3. Exit
The program should continue until the user selects 3.
C Program
#include <stdio.h>
int main()
{
int choice;
do
{
printf("\n===== MENU =====\n");
printf("1. Say Hello\n");
printf("2. Say Welcome\n");
printf("3. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice)
{
case 1:
printf("Hello!");
break;
case 2:
printf("Welcome!");
break;
case 3:
printf("Program ended.");
break;
default:
printf("Invalid choice.");
}
} while (choice != 3);
return 0;
}
Sample Output
===== MENU =====
1. Say Hello
2. Say Welcome
3. Exit
Enter your choice: 1
Hello!
===== MENU =====
1. Say Hello
2. Say Welcome
3. Exit
Enter your choice: 2
Welcome!
===== MENU =====
1. Say Hello
2. Say Welcome
3. Exit
Enter your choice: 3
Program ended.
Explanation
A do-while loop executes its body first and checks the condition afterward.
Here:
do
{
// menu
}
while (choice != 3);
The menu appears at least once.
The loop continues while the user does not select 3.
Concepts Covered
do-whileswitch- Menu-driven program
break- Loop condition
Q10. Keep Asking for a Positive Number Using do-while
Problem Statement
Write a C program that repeatedly asks the user to enter a positive number. The program should stop only when the user enters a positive value.
C Program
#include <stdio.h>
int main()
{
int number;
do
{
printf("Enter a positive number: ");
scanf("%d", &number);
if (number <= 0)
{
printf("Invalid input. Try again.\n");
}
} while (number <= 0);
printf("You entered: %d", number);
return 0;
}
Sample Output
Enter a positive number: -5
Invalid input. Try again.
Enter a positive number: 0
Invalid input. Try again.
Enter a positive number: 25
You entered: 25
Explanation
The do-while loop is useful here because the program needs to ask for input at least once.
The condition:
number <= 0
means the loop continues when the input is invalid.
When the user enters 25:
25 <= 0 → False
so the loop stops.
Concepts Covered
do-while- Input validation
if- Repeated input
- Loop condition
Key Takeaways
whileis an entry-controlled loop because it checks the condition before execution.do-whileis an exit-controlled loop because it checks the condition after execution.- A
whileloop may execute zero times. - A
do-whileloop executes at least once. - Always make sure the loop variable or condition changes when necessary.
whileloops are useful for counting, digit processing, searching, and condition-based repetition.do-whileloops are particularly useful for menus and input validation.- The
do-whilestatement ends with a semicolon afterwhile (condition);. % 10can be used to extract the last digit of an integer.- Integer division by
10can remove the last digit. whileanddo-whileloops can be combined withif,switch, and other C concepts.- Nested loops can also be created using
whileordo-while.
FAQs
1. What is a while loop in C?
A while loop repeatedly executes a block of code as long as its condition remains true.
while (condition)
{
// code
}
2. What is a do-while loop in C?
A do-while loop executes its body first and then checks the condition.
do
{
// code
}
while (condition);
3. What is the main difference between while and do-while?
The main difference is when the condition is checked.
while→ condition is checked first.do-while→ condition is checked after the body executes.
Therefore, a do-while loop always executes its body at least once.
4. Can a while loop execute zero times?
Yes.
If its condition is false before the first iteration, the loop body will not execute.
int i = 10;
while (i < 5)
{
printf("Hello");
}
The body does not execute.
5. Can a do-while loop execute zero times?
No. The body executes once before the condition is checked.
do
{
printf("Hello");
}
while (0);
Hello is still printed once.
6. When should I use while instead of do-while?
Use while when the program should check a condition before doing anything.
Use do-while when the operation should happen at least once, such as displaying a menu or asking for input.
7. Why does a do-while loop have a semicolon after while?
The semicolon is part of the syntax:
do
{
// code
}
while (condition);
It marks the end of the do-while statement.
Written by Shubhranshu Shekhar, who has trained 20000+ students in coding.
