Write a C program to display numbers from 1 to 10, but terminate the loop when the number becomes 6.
Write a C program to display numbers from 1 to 10, but terminate the loop when the number becomes 6.
Understand how the break statement immediately terminates a loop.
Use a for loop and place break inside an if condition when i == 6.
The break statement terminates the nearest loop immediately and transfers control to the statement following the loop. :contentReference[oaicite:0]{index=0}
#include <stdio.h>
int main()
{
int i;
// Display numbers from 1 to 10
for (i = 1; i <= 10; i++)
{
// Stop the loop when i becomes 6
if (i == 6)
{
break;
}
printf("%d ", i);
}
return 0;
}
1 2 3 4 5
When i becomes 6, break terminates the loop completely.
Write a C program to search for a given number in a series of numbers. Stop searching as soon as the number is found.
Use break to terminate a search operation as soon as the required value is located.
Compare each input value with the search value and use break when they match.
In a sequential search, values are checked one by one. Once the required value is found, further checking is unnecessary, so break can terminate the loop.
#include <stdio.h>
int main()
{
int n, i, num, search;
int found = 0;
printf("Enter number of elements: ");
scanf("%d", &n);
printf("Enter number to search: ");
scanf("%d", &search);
// Read and search values
for (i = 1; i <= n; i++)
{
printf("Enter number %d: ", i);
scanf("%d", &num);
if (num == search)
{
found = 1;
break;
}
}
if (found)
{
printf("Number found.");
}
else
{
printf("Number not found.");
}
return 0;
}
Enter number of elements: 5 Enter number to search: 30 Enter number 1: 10 Enter number 2: 25 Enter number 3: 30 Number found.
The loop stops immediately after finding the required number.
Write a C program that continuously accepts numbers and stops when the user enters zero.
Understand how break can be used to terminate a loop based on user input.
Use an infinite for loop and terminate it when the input becomes zero.
A loop can intentionally continue indefinitely and use break as an explicit termination mechanism. :contentReference[oaicite:1]{index=1}
#include <stdio.h>
int main()
{
int num;
// Continue until the user enters zero
for (;;)
{
printf("Enter a number (0 to stop): ");
scanf("%d", &num);
if (num == 0)
{
break;
}
printf("You entered: %d\n", num);
}
printf("Loop terminated.");
return 0;
}
Enter a number (0 to stop): 10 You entered: 10 Enter a number (0 to stop): 25 You entered: 25 Enter a number (0 to stop): 0 Loop terminated.
The for (;;) creates an infinite loop and break provides the termination condition.
Write a C program to accept numbers and calculate their sum. Stop the input process when a negative number is entered.
Use break to stop input processing based on a condition.
Keep adding positive values and use break whenever the entered number is negative.
The break statement is useful when input processing must terminate before a loop's normal condition is reached.
#include <stdio.h>
int main()
{
int num, sum = 0;
// Accept numbers until a negative value is entered
for (;;)
{
printf("Enter a number: ");
scanf("%d", &num);
if (num < 0)
{
break;
}
sum = sum + num;
}
printf("Sum = %d", sum);
return 0;
}
Enter a number: 10 Enter a number: 20 Enter a number: 15 Enter a number: -1 Sum = 45
The negative number acts as a stopping signal and is not added to the sum.
Write a C program to check numbers from 1 to 100 and display the first number divisible by 7 using break.
Use break to terminate a loop after the first successful condition.
Check i % 7 == 0 and use break when the condition becomes true.
Break is useful when only the first matching value is required and processing of later values is unnecessary.
#include <stdio.h>
int main()
{
int i;
// Search from 1 to 100
for (i = 1; i <= 100; i++)
{
if (i % 7 == 0)
{
printf("First number divisible by 7 = %d", i);
break;
}
}
return 0;
}
First number divisible by 7 = 7
The loop stops at the first value satisfying the divisibility condition.
Write a C program that gives the user up to 3 attempts to enter a password. Stop the loop immediately when the correct password is entered.
Apply break in a practical input-validation problem.
Compare the entered password with a predefined password and use break when they match.
Break can terminate a loop early when a successful condition is achieved.
#include <stdio.h>
int main()
{
int password, i;
int correct = 1234;
// Allow maximum three attempts
for (i = 1; i <= 3; i++)
{
printf("Enter password: ");
scanf("%d", &password);
if (password == correct)
{
printf("Login successful.");
break;
}
printf("Incorrect password.\n");
}
if (i > 3)
{
printf("Account locked.");
}
return 0;
}
Enter password: 1234 Login successful.
Once the correct password is entered, break prevents unnecessary remaining attempts.
Write a C program to display numbers from 1 to 50 and stop the loop at the first multiple of 10.
Understand early termination using a conditional break.
Check whether the current number is divisible by 10.
A conditional break is executed only when a particular condition becomes true.
#include <stdio.h>
int main()
{
int i;
// Display values until the first multiple of 10
for (i = 1; i <= 50; i++)
{
printf("%d ", i);
if (i % 10 == 0)
{
break;
}
}
return 0;
}
1 2 3 4 5 6 7 8 9 10
The loop terminates when the first multiple of 10 is reached.
Write a menu-driven C program that repeatedly displays options for addition and subtraction. Use break when the user selects the exit option.
Understand the use of break for terminating menu-driven loops.
Use an infinite loop containing a switch statement and terminate it with break when option 3 is selected.
Menu-driven applications often use an infinite loop and break to terminate execution when the user selects an exit option. :contentReference[oaicite:2]{index=2}
#include <stdio.h>
int main()
{
int choice, a, b;
// Display menu until Exit is selected
for (;;)
{
printf("\n1. Addition\n");
printf("2. Subtraction\n");
printf("3. Exit\n");
printf("Enter choice: ");
scanf("%d", &choice);
if (choice == 3)
{
break;
}
if (choice == 1 || choice == 2)
{
printf("Enter two numbers: ");
scanf("%d %d", &a, &b);
if (choice == 1)
{
printf("Result = %d\n", a + b);
}
else
{
printf("Result = %d\n", a - b);
}
}
else
{
printf("Invalid choice.\n");
}
}
printf("Program terminated.");
return 0;
}
1. Addition 2. Subtraction 3. Exit Enter choice: 1 Enter two numbers: 20 10 Result = 30 1. Addition 2. Subtraction 3. Exit Enter choice: 3 Program terminated.
The exit option uses break to terminate the infinite loop.
Write a C program to read numbers from the user and stop as soon as a number greater than 100 is entered.
Use break with user-controlled input and conditional checking.
Continue accepting numbers inside a loop and use break when the entered number is greater than 100.
Break is useful when the loop should terminate immediately after a special input condition is satisfied.
#include <stdio.h>
int main()
{
int num;
// Continue until a number greater than 100 is entered
for (;;)
{
printf("Enter a number: ");
scanf("%d", &num);
if (num > 100)
{
printf("Number greater than 100 found: %d", num);
break;
}
}
return 0;
}
Enter a number: 25 Enter a number: 60 Enter a number: 90 Enter a number: 150 Number greater than 100 found: 150
The loop continues until the required condition becomes true.
Write a C program to search for the first prime number between 10 and 50 and stop the search using break.
Apply break in a nested loop and understand early termination during searching.
For each number, check whether it has a divisor. Use break when a divisor is found. Stop the outer loop when a prime number is identified.
Nested loops can be used when one repeated operation must be performed inside another. Break can terminate the inner loop once a divisor is found and can also terminate the outer search after the first prime is found.
#include <stdio.h>
int main()
{
int i, j, isPrime;
// Search numbers from 10 to 50
for (i = 10; i <= 50; i++)
{
isPrime = 1;
// Check whether i has a divisor
for (j = 2; j * j <= i; j++)
{
if (i % j == 0)
{
isPrime = 0;
break;
}
}
// Stop after finding the first prime
if (isPrime)
{
printf("First prime number = %d", i);
break;
}
}
return 0;
}
First prime number = 11
This example demonstrates break in both the inner divisibility check and the outer search loop.