C Programming • C Basics, Operators, Loops
C Programming / break — Exercises

break — Exercises

Practical 2 C Basics, Operators, Loops

Write a C program to display numbers from 1 to 10, but terminate the loop when the number becomes 6.

Practical / Solution

Exercise 1: Stop the Loop at 6

Problem Statement

Write a C program to display numbers from 1 to 10, but terminate the loop when the number becomes 6.

Learning Outcomes

Understand how the break statement immediately terminates a loop.

Hint

Use a for loop and place break inside an if condition when i == 6.

Theory

The break statement terminates the nearest loop immediately and transfers control to the statement following the loop. :contentReference[oaicite:0]{index=0}

Program

#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;
}

Expected Output

1 2 3 4 5

Note

When i becomes 6, break terminates the loop completely.

Exercise 2: Search for a Number

Problem Statement

Write a C program to search for a given number in a series of numbers. Stop searching as soon as the number is found.

Learning Outcomes

Use break to terminate a search operation as soon as the required value is located.

Hint

Compare each input value with the search value and use break when they match.

Theory

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.

Program

#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;
}

Expected Output

Enter number of elements: 5
Enter number to search: 30
Enter number 1: 10
Enter number 2: 25
Enter number 3: 30
Number found.

Note

The loop stops immediately after finding the required number.

Exercise 3: Stop When Zero is Entered

Problem Statement

Write a C program that continuously accepts numbers and stops when the user enters zero.

Learning Outcomes

Understand how break can be used to terminate a loop based on user input.

Hint

Use an infinite for loop and terminate it when the input becomes zero.

Theory

A loop can intentionally continue indefinitely and use break as an explicit termination mechanism. :contentReference[oaicite:1]{index=1}

Program

#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;
}

Expected Output

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.

Note

The for (;;) creates an infinite loop and break provides the termination condition.

Exercise 4: Stop When a Negative Number is Entered

Problem Statement

Write a C program to accept numbers and calculate their sum. Stop the input process when a negative number is entered.

Learning Outcomes

Use break to stop input processing based on a condition.

Hint

Keep adding positive values and use break whenever the entered number is negative.

Theory

The break statement is useful when input processing must terminate before a loop's normal condition is reached.

Program

#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;
}

Expected Output

Enter a number: 10
Enter a number: 20
Enter a number: 15
Enter a number: -1
Sum = 45

Note

The negative number acts as a stopping signal and is not added to the sum.

Exercise 5: Find First Number Divisible by 7

Problem Statement

Write a C program to check numbers from 1 to 100 and display the first number divisible by 7 using break.

Learning Outcomes

Use break to terminate a loop after the first successful condition.

Hint

Check i % 7 == 0 and use break when the condition becomes true.

Theory

Break is useful when only the first matching value is required and processing of later values is unnecessary.

Program

#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;
}

Expected Output

First number divisible by 7 = 7

Note

The loop stops at the first value satisfying the divisibility condition.

Exercise 6: Password Verification

Problem Statement

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.

Learning Outcomes

Apply break in a practical input-validation problem.

Hint

Compare the entered password with a predefined password and use break when they match.

Theory

Break can terminate a loop early when a successful condition is achieved.

Program

#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;
}

Expected Output

Enter password: 1234
Login successful.

Note

Once the correct password is entered, break prevents unnecessary remaining attempts.

Exercise 7: Stop at the First Multiple of 10

Problem Statement

Write a C program to display numbers from 1 to 50 and stop the loop at the first multiple of 10.

Learning Outcomes

Understand early termination using a conditional break.

Hint

Check whether the current number is divisible by 10.

Theory

A conditional break is executed only when a particular condition becomes true.

Program

#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;
}

Expected Output

1 2 3 4 5 6 7 8 9 10

Note

The loop terminates when the first multiple of 10 is reached.

Exercise 8: Menu-Driven Program with Exit Option

Problem Statement

Write a menu-driven C program that repeatedly displays options for addition and subtraction. Use break when the user selects the exit option.

Learning Outcomes

Understand the use of break for terminating menu-driven loops.

Hint

Use an infinite loop containing a switch statement and terminate it with break when option 3 is selected.

Theory

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}

Program

#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;
}

Expected Output

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.

Note

The exit option uses break to terminate the infinite loop.

Exercise 9: Find the First Number Greater Than 100

Problem Statement

Write a C program to read numbers from the user and stop as soon as a number greater than 100 is entered.

Learning Outcomes

Use break with user-controlled input and conditional checking.

Hint

Continue accepting numbers inside a loop and use break when the entered number is greater than 100.

Theory

Break is useful when the loop should terminate immediately after a special input condition is satisfied.

Program

#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;
}

Expected Output

Enter a number: 25
Enter a number: 60
Enter a number: 90
Enter a number: 150
Number greater than 100 found: 150

Note

The loop continues until the required condition becomes true.

Exercise 10: Find the First Prime Number in a Range

Problem Statement

Write a C program to search for the first prime number between 10 and 50 and stop the search using break.

Learning Outcomes

Apply break in a nested loop and understand early termination during searching.

Hint

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.

Theory

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.

Program

#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;
}

Expected Output

First prime number = 11

Note

This example demonstrates break in both the inner divisibility check and the outer search loop.