Write a C program to reverse the digits of a given number using a while loop.
Write a C program to reverse the digits of a given number using
a while loop.
while loop.
Use % 10 to extract the last digit and
/= 10 to remove the last digit.
A while loop repeatedly executes a block of statements
while its condition remains true. Here, the loop continues until
all digits of the number have been processed.
Enter a number to reverse: 12345
Reversed number = 54321
The condition is checked before every iteration, so the loop stops when the number becomes zero.
Write a C program to count the number of digits in a given number
using a while loop.
while loop for repeated processing.Repeatedly divide the number by 10 and increase the counter by 1 until the number becomes zero.
Each integer division by 10 removes the last digit of a positive integer. The number of such divisions required to reach zero gives the total number of digits.
Enter a number: 12345
Number of digits = 5
This approach directly demonstrates how a loop can repeatedly process each digit of an integer.
Write a C program to calculate the sum of the digits of a given
number using a while loop.
Extract the last digit using % 10, add it to
sum, and remove the last digit using /= 10.
The modulus operator returns the last digit of an integer when it is divided by 10. By repeatedly extracting and adding these digits, the sum of all digits can be calculated.
Enter a number: 12345
Sum of digits = 15
The same digit-extraction pattern is useful in many number-based programming problems.
Write a C program to check whether a given number is a palindrome
using a while loop.
Store the original number, create its reverse using
% 10 and /= 10, then compare both values.
A palindrome number reads the same from left to right and right to left. The program checks this by creating the reverse of the number.
Enter a number: 121
121 is a Palindrome.
A copy of the original number is required because the working loop changes the value of the input number.
Write a C program to check whether a given number is an Armstrong
number using a while loop.
Extract each digit using % 10, add its cube to
sum, and remove the digit using /= 10.
A three-digit Armstrong number is a number whose value is equal to the sum of the cubes of its digits. For example, 153 = 1³ + 5³ + 3³.
Enter a number: 153
153 is an Armstrong number.
This exercise is intended for the basic three-digit Armstrong number concept taught at this level.
Write a C program to check whether a given number is prime using
a while loop.
Check divisibility from 1 up to the given number using the modulus operator and count the number of divisors.
A prime number has exactly two positive divisors: 1 and itself.
The program uses a while loop to test possible divisors.
Enter a number: 17
17 is a Prime number.
This version emphasizes the basic idea of checking divisors, making it suitable for first-year beginners.
Write a C program to calculate the factorial of a number using
a while loop.
Initialize fact = 1 and multiply it by each number
from 1 to n.
The factorial of a non-negative integer N is the product of all
positive integers from 1 to N. It is written as
N!.
Enter a number: 5
Factorial = 120
The factorial of 0 is also defined as 1. For large values, the factorial may exceed the range of the selected data type.
Write a C program to generate the Fibonacci series up to N terms
using a while loop.
Start with 0 and 1. Each next term is obtained by adding the previous two terms.
In the Fibonacci series, each term is the sum of the two previous terms. The sequence starts with 0 and 1.
Enter number of terms: 7
0 1 1 2 3 5 8
The loop is controlled by the number of terms rather than by the value of a number being reduced.
Write a C program to calculate the sum of the first N natural
numbers using a while loop.
Start with sum = 0 and add each number from 1 to N.
Natural numbers begin with 1. Their sum can be calculated by repeatedly adding each number until N is reached.
Enter N: 10
Sum = 55
This exercise shows a simple counter-controlled while
loop and is useful for understanding initialization, condition,
body, and update.
Write a C program to print the multiplication table of a given
number using a while loop.
while loop.Start the counter from 1 and continue until it reaches 10.
A multiplication table can be generated by repeatedly multiplying
a fixed number by consecutive integers. A while loop
avoids writing the same statement ten times.
Enter a number: 5
5 x 1 = 5
5 x 2 = 10
5 x 3 = 15
5 x 4 = 20
5 x 5 = 25
5 x 6 = 30
5 x 7 = 35
5 x 8 = 40
5 x 9 = 45
5 x 10 = 50
The counter is initialized before the loop and updated at the
end of each iteration. This is a basic example of a
counter-controlled while loop.