Question
C Program to Print Numbers Until a Multiple of 7 is Found Using the break Statement
Solution
β Verified
In this program, we take input numbers from the user one by one and print them. The loop continues until the user enters a number that is a multiple of 7. As soon as a multiple of 7 is found, the break statement is used to immediately stop the loop.
This is useful when you want to exit a loop based on a specific condition (in this case, the number being divisible by 7), even though the loop could still keep running.
#include <stdio.h>
int main() {
int num;
// Infinite loop to keep asking for numbers
while (1) {
printf("Enter a number: ");
scanf("%d", &num);
// Check if the number is a multiple of 7
if (num % 7 == 0) {
printf("Multiple of 7 found. Stopping the loop.\n");
break; // Exit the loop
} else {
printf("%d is not multiple of 7 \n\n", num);
}
}
return 0;
}
Output -Β
Enter a number: 23
23 is not multiple of 7
Enter a number: 89
89 is not multiple of 7
Enter a number: 78
78 is not multiple of 7
Enter a number: 14
Multiple of 7 found. Stopping the loop.Β
Click here to download practice questions on
Break Statement in CMore Questions on Break Statement in C
Question 1
C program to calculate the sum of Numbers Until a Negative Value is Entered using break statement.
View solution