- The do-while loop is a type of loop where the code block is executed at least once, and then the condition is checked.
- If the condition is true, the loop will continue to execute the code block.
- If the condition is false, the loop will terminate and the program will proceed to the next statement after the loop.
Syntax of a do-while loop in C:
C
do {
// code to be executed
} while (condition);
Example:
C
#include <stdio.h>
int main() {
int count = 0;
do {
printf("Count: %d\n", count);
count++;
} while (count < 5);
return 0;
}
Explanation:
- In this example, the variable count is initially set to 0.
- The code block inside the do-while loop is executed first, which prints the current value of count.
- Then, the count variable is incremented by 1.
- The condition count < 5 is checked, and since it is true, the loop continues to execute.
- This process repeats until count becomes 5, at which point the condition becomes false, and the loop terminates.
Output:
Output
Count: 0
Count: 1
Count: 2
Count: 3
Count: 4
Practice problems on Do While loop in C:
Problem 1: Sum of Digits
Write a program that calculates the sum of the digits of a given positive integer.
C
#include <stdio.h>
int main() {
int num, digit, sum = 0;
printf("Enter a positive integer: ");
scanf("%d", &num);
do {
digit = num % 10;
sum += digit;
num /= 10;
} while (num > 0);
printf("Sum of digits: %d\n", sum);
return 0;
}
Explanation:
- The program takes input from the user for a positive integer num.
- It initializes variables digit and sum to 0. The do-while loop executes the code block at least once.
- In each iteration, the last digit of num is obtained using the modulo operator (%) and stored in digit.
- The digit is added to the sum.
- The last digit of num is removed by dividing it by 10.
- The loop continues until num becomes 0.
- Finally, the sum of the digits is printed.
Output:
Output
Enter a positive integer: 25
Sum of digits: 7
Problem 2: Guessing Game
Write a program that generates a random number between 1 and 100. The user needs to guess the number, and the program provides feedback on whether the guess is too high or too low until the correct number is guessed.
C
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main() {
int number, guess, attempts = 0;
srand(time(0));
number = rand() % 100 + 1;
do {
printf("Guess the number (1-100): ");
scanf("%d", &guess);
attempts++;
if (guess > number) {
printf("Too high\n");
} else if (guess < number) {
printf("Too low\n");
} else {
printf("Congratulations! You guessed it right in %d attempts.\n", attempts);
}
} while (guess != number);
return 0;
}
Explanation:
- The program generates a random number between 1 and 100 using the rand() function with the help of time() for seed initialization.
- The variable guess stores the user’s input for the guessed number.
- The attempts variable keeps track of the number of attempts made.
- Inside the do-while loop, the user is prompted to enter a guess.
- Depending on whether the guess is too high or too low, the program provides appropriate feedback.
- If the guess is correct, the loop terminates, and the number of attempts is displayed.
Output:
Output
Guess the number (1-100): 50
Too high
Guess the number (1-100): 35
Too low
Guess the number (1-100): 38
Too low
Guess the number (1-100): 41
Too low
Guess the number (1-100): 46
Too low
Guess the number (1-100): 48
Congratulations! You guessed it right in 6 attempts.