C Programming • C Basics, Operators, Loops
C Programming / do-while — Exercises

do-while — Exercises

Practical 2 C Basics, Operators, Loops

Write a C program to display numbers from 1 to 10 using a do-while loop.

Practical / Solution

do-while Loop — Exercise 1

Problem Statement

Write a C program to display numbers from 1 to 10 using a do-while loop.

Learning Outcomes

  • Understand the basic working of a do-while loop.
  • Use a counter variable with a loop.
  • Understand that the loop body executes before the condition is checked.

Hint

Initialize the counter with 1, print it, increment it, and continue while the counter is less than or equal to 10.

Theory

A do-while loop executes its body first and checks the condition afterwards. Therefore, the loop body executes at least once.

Program

#include <stdio.h> int main() { int i = 1; // Display numbers from 1 to 10 do { printf("%d ", i); i++; } while (i <= 10); return 0; }

Expected Output

1 2 3 4 5 6 7 8 9 10

Note

Even if the condition is false initially, the body of a do-while loop executes once.

do-while Loop — Exercise 2

Problem Statement

Write a C program to calculate the sum of the first N natural numbers using a do-while loop.

Learning Outcomes

  • Use a do-while loop for repeated addition.
  • Understand counter-controlled repetition.
  • Update the sum during every iteration.

Hint

Start with sum = 0 and i = 1. Add i to the sum and increment i.

Theory

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.

Program

#include <stdio.h> int main() { int n, i = 1, sum = 0; // Read the limit printf("Enter N: "); scanf("%d", &n); // Calculate the sum if (n >= 1) { do { sum += i; i++; } while (i <= n); } printf("Sum = %d\n", sum); return 0; }

Expected Output

Enter N: 10

Sum = 55

Note

The counter starts at 1 and continues until it reaches N.

do-while Loop — Exercise 3

Problem Statement

Write a C program to calculate the factorial of a number using a do-while loop.

Learning Outcomes

  • Perform repeated multiplication using a loop.
  • Understand factorial calculation.
  • Use a loop variable to control repetition.

Hint

Initialize fact = 1 and multiply it by each number from 1 to N.

Theory

The factorial of N is the product of all positive integers from 1 to N. It is represented as N!.

Program

#include <stdio.h> int main() { int n, i = 1; long long fact = 1; printf("Enter a number: "); scanf("%d", &n); // Calculate factorial if (n >= 1) { do { fact *= i; i++; } while (i <= n); } printf("Factorial = %lld\n", fact); return 0; }

Expected Output

Enter a number: 5

Factorial = 120

Note

The factorial of 0 is defined as 1. The condition is checked only after the loop body executes.

do-while Loop — Exercise 4

Problem Statement

Write a C program to print the multiplication table of a given number using a do-while loop.

Learning Outcomes

  • Use a loop for repeated multiplication.
  • Generate structured output.
  • Understand counter updates.

Hint

Start the counter from 1 and continue up to 10.

Theory

A multiplication table repeatedly multiplies a fixed number by consecutive integers. A do-while loop can be used to perform these repeated operations.

Program

#include <stdio.h> int main() { int num, i = 1; printf("Enter a number: "); scanf("%d", &num); // Print multiplication table do { printf("%d x %d = %d\n", num, i, num * i); i++; } while (i <= 10); return 0; }

Expected Output

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

Note

The loop prints one complete multiplication step before checking the condition.

do-while Loop — Exercise 5

Problem Statement

Write a C program to calculate the sum of digits of a given number using a do-while loop.

Learning Outcomes

  • Extract digits using the modulus operator.
  • Perform repeated addition.
  • Understand digit-processing with a loop.

Hint

Extract the last digit using % 10, add it to sum, and remove it using /= 10.

Theory

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.

Program

#include <stdio.h> int main() { int num, digit, sum = 0; printf("Enter a number: "); scanf("%d", &num); // Process each digit do { digit = num % 10; sum += digit; num /= 10; } while (num != 0); printf("Sum of digits = %d\n", sum); return 0; }

Expected Output

Enter a number: 12345

Sum of digits = 15

Note

Unlike while, this loop processes at least one digit, which is especially visible when the input is 0.

do-while Loop — Exercise 6

Problem Statement

Write a C program to reverse a given number using a do-while loop.

Learning Outcomes

  • Extract digits from an integer.
  • Build a reversed number.
  • Use an exit-controlled loop for digit processing.

Hint

Extract the last digit using % 10, add it to the reversed number, and remove the last digit using /= 10.

Theory

Reversing a number involves processing its digits from right to left and constructing a new number from those digits.

Program

#include <stdio.h> int main() { int num, digit, reverse = 0; printf("Enter a number: "); scanf("%d", &num); // Reverse the number do { digit = num % 10; reverse = reverse * 10 + digit; num /= 10; } while (num != 0); printf("Reversed number = %d\n", reverse); return 0; }

Expected Output

Enter a number: 12345

Reversed number = 54321

Note

The loop body executes before the condition is checked, making do-while an exit-controlled loop.

do-while Loop — Exercise 7

Problem Statement

Write a C program to check whether a given number is even or odd using a do-while loop.

Learning Outcomes

  • Use the modulus operator in a conditional check.
  • Understand the role of a loop in repeated validation.
  • Apply if-else inside a do-while loop.

Hint

Take a number, check num % 2, display the result, and ask whether the user wants to check another number.

Theory

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.

Program

#include <stdio.h> int main() { int num, choice; do { // Read the number printf("Enter a number: "); scanf("%d", &num); // Check even or odd if (num % 2 == 0) printf("%d is Even.\n", num); else printf("%d is Odd.\n", num); // Ask whether to continue printf("Enter 1 to continue, 0 to stop: "); scanf("%d", &choice); } while (choice == 1); return 0; }

Expected Output

Enter a number: 12

12 is Even.

Enter 1 to continue, 0 to stop: 0

Note

The program demonstrates a practical reason for using do-while: the user must get at least one chance to perform the operation.

do-while Loop — Exercise 8

Problem Statement

Write a menu-driven C program using do-while to perform addition, subtraction, multiplication and division.

Learning Outcomes

  • Build a simple menu-driven program.
  • Combine do-while and switch-case.
  • Handle invalid choices and division by zero.

Hint

Display the menu at least once, read the choice, perform the selected operation, and repeat until the user chooses Exit.

Theory

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.

Program

#include <stdio.h> int main() { int choice; float a, b; do { // Display menu printf("\n1. Addition\n"); printf("2. Subtraction\n"); printf("3. Multiplication\n"); printf("4. Division\n"); printf("5. Exit\n"); printf("Enter your choice: "); scanf("%d", &choice); if (choice >= 1 && choice <= 4) { printf("Enter two numbers: "); scanf("%f %f", &a, &b); } switch (choice) { case 1: printf("Result = %.2f\n", a + b); break; case 2: printf("Result = %.2f\n", a - b); break; case 3: printf("Result = %.2f\n", a * b); break; case 4: if (b != 0) printf("Result = %.2f\n", a / b); else printf("Error: Division by zero\n"); break; case 5: printf("Exiting program.\n"); break; default: printf("Invalid choice.\n"); } } while (choice != 5); return 0; }

Expected Output

1. Addition
2. Subtraction
3. Multiplication
4. Division
5. Exit

Enter your choice: 1

Enter two numbers: 10 20

Result = 30.00

Note

Always ensure that the menu's Exit option and the loop's termination condition use the same value.

do-while Loop — Exercise 9

Problem Statement

Write a C program to find the largest of a series of numbers entered by the user using a do-while loop.

Learning Outcomes

  • Compare values repeatedly.
  • Maintain the largest value during execution.
  • Use a do-while loop for repeated input.

Hint

Read the first number as the initial largest value. Then read additional numbers and update the largest value whenever a bigger number is found.

Theory

A running maximum stores the largest value found so far. Every new input is compared with this value and replaces it when necessary.

Program

#include <stdio.h> int main() { int n, i = 1, num, largest; printf("How many numbers? "); scanf("%d", &n); printf("Enter number 1: "); scanf("%d", &largest); i = 2; // Compare remaining numbers if (n > 1) { do { printf("Enter number %d: ", i); scanf("%d", &num); if (num > largest) largest = num; i++; } while (i <= n); } printf("Largest number = %d\n", largest); return 0; }

Expected Output

How many numbers? 4

Enter number 1: 12

Enter number 2: 25

Enter number 3: 18

Enter number 4: 30

Largest number = 30

Note

The first input is stored separately so that it can be used as the initial value of largest.

do-while Loop — Exercise 10

Problem Statement

Write a C program that repeatedly accepts a positive number and calculates its square. The program should continue until the user chooses to stop.

Learning Outcomes

  • Understand repeated user interaction.
  • Use do-while for menu-like repetition.
  • Combine input, calculation and a continuation condition.

Hint

Read a number, calculate its square, then ask the user whether another calculation is required.

Theory

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.

Program

#include <stdio.h> int main() { int num, choice; do { // Read the number printf("Enter a number: "); scanf("%d", &num); // Display the square printf("Square = %d\n", num * num); // Ask whether to continue printf("Enter 1 to continue, 0 to stop: "); scanf("%d", &choice); } while (choice == 1); printf("Program ended.\n"); return 0; }

Expected Output

Enter a number: 7

Square = 49

Enter 1 to continue, 0 to stop: 0

Program ended.

Note

This exercise demonstrates the practical use of an exit-controlled loop where the user decides whether another iteration is required.