Write a C program to display numbers from 1 to 10 using a do-while loop.
Write a C program to display numbers from 1 to 10 using a
do-while loop.
do-while loop.Initialize the counter with 1, print it, increment it, and continue while the counter is less than or equal to 10.
A do-while loop executes its body first and checks
the condition afterwards. Therefore, the loop body executes
at least once.
1 2 3 4 5 6 7 8 9 10
Even if the condition is false initially, the body of a
do-while loop executes once.
Write a C program to calculate the sum of the first N natural
numbers using a do-while loop.
do-while loop for repeated addition.
Start with sum = 0 and i = 1. Add
i to the sum and increment i.
The sum of the first N natural numbers is obtained by adding
each number from 1 through N. The do-while loop
performs the calculation at least once.
Enter N: 10
Sum = 55
The counter starts at 1 and continues until it reaches N.
Write a C program to calculate the factorial of a number using
a do-while loop.
Initialize fact = 1 and multiply it by each number
from 1 to N.
The factorial of N is the product of all positive integers from
1 to N. It is represented as N!.
Enter a number: 5
Factorial = 120
The factorial of 0 is defined as 1. The condition is checked only after the loop body executes.
Write a C program to print the multiplication table of a given
number using a do-while loop.
Start the counter from 1 and continue up to 10.
A multiplication table repeatedly multiplies a fixed number by
consecutive integers. A do-while loop can be used
to perform these repeated operations.
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 loop prints one complete multiplication step before checking the condition.
Write a C program to calculate the sum of digits of a given
number using a do-while loop.
Extract the last digit using % 10, add it to
sum, and remove it using /= 10.
The modulus operator returns the last digit when an integer is divided by 10. This process can be repeated until all digits have been processed.
Enter a number: 12345
Sum of digits = 15
Unlike while, this loop processes at least one digit,
which is especially visible when the input is 0.
Write a C program to reverse a given number using a
do-while loop.
Extract the last digit using % 10, add it to the
reversed number, and remove the last digit using /= 10.
Reversing a number involves processing its digits from right to left and constructing a new number from those digits.
Enter a number: 12345
Reversed number = 54321
The loop body executes before the condition is checked, making
do-while an exit-controlled loop.
Write a C program to check whether a given number is even or odd
using a do-while loop.
if-else inside a do-while loop.
Take a number, check num % 2, display the result,
and ask whether the user wants to check another number.
A do-while loop is useful when an operation must be
performed at least once. Here, the user is given the opportunity
to check a number before the continuation condition is evaluated.
Enter a number: 12
12 is Even.
Enter 1 to continue, 0 to stop: 0
The program demonstrates a practical reason for using
do-while: the user must get at least one chance
to perform the operation.
Write a menu-driven C program using do-while to
perform addition, subtraction, multiplication and division.
do-while and switch-case.Display the menu at least once, read the choice, perform the selected operation, and repeat until the user chooses Exit.
A do-while loop is well suited for menu-driven
programs because the menu must be displayed before the program
can ask whether the user wants to continue.
1. Addition
2. Subtraction
3. Multiplication
4. Division
5. Exit
Enter your choice: 1
Enter two numbers: 10 20
Result = 30.00
Always ensure that the menu's Exit option and the loop's termination condition use the same value.
Write a C program to find the largest of a series of numbers
entered by the user using a do-while loop.
do-while loop for repeated input.Read the first number as the initial largest value. Then read additional numbers and update the largest value whenever a bigger number is found.
A running maximum stores the largest value found so far. Every new input is compared with this value and replaces it when necessary.
How many numbers? 4
Enter number 1: 12
Enter number 2: 25
Enter number 3: 18
Enter number 4: 30
Largest number = 30
The first input is stored separately so that it can be used as
the initial value of largest.
Write a C program that repeatedly accepts a positive number and calculates its square. The program should continue until the user chooses to stop.
do-while for menu-like repetition.Read a number, calculate its square, then ask the user whether another calculation is required.
The do-while loop is useful when a task must be
performed at least once and the decision to continue is made
after the task is completed.
Enter a number: 7
Square = 49
Enter 1 to continue, 0 to stop: 0
Program ended.
This exercise demonstrates the practical use of an exit-controlled loop where the user decides whether another iteration is required.