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

continue — Exercises

Practical 2 C Basics, Operators, Loops

Write a C program to display numbers from 1 to 10, but skip the number 5 using the continue statement.

Practical / Solution

Exercise 1: Skip Number 5

Problem Statement

Write a C program to display numbers from 1 to 10, but skip the number 5 using the continue statement.

Learning Outcomes

Understand how continue skips the remaining statements of the current iteration.

Hint

When i == 5, use continue.

Theory

The continue statement skips the remaining statements of the current loop iteration and moves to the next iteration. :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++)
    {
        // Skip number 5
        if (i == 5)
        {
            continue;
        }

        printf("%d ", i);
    }

    return 0;
}

Expected Output

1 2 3 4 6 7 8 9 10

Note

Only the iteration for 5 is skipped; the loop continues normally with 6.

Exercise 2: Display Only Odd Numbers

Problem Statement

Write a C program to display odd numbers from 1 to 20 using the continue statement.

Learning Outcomes

Use continue to skip unwanted values during loop execution.

Hint

When a number is even, use continue to skip its printing.

Theory

Continue is useful when certain values should be ignored while the remaining iterations continue normally.

Program

#include <stdio.h>

int main()
{
    int i;

    // Check numbers from 1 to 20
    for (i = 1; i <= 20; i++)
    {
        // Skip even numbers
        if (i % 2 == 0)
        {
            continue;
        }

        printf("%d ", i);
    }

    return 0;
}

Expected Output

1 3 5 7 9 11 13 15 17 19

Note

Whenever an even number is encountered, continue skips its printing.

Exercise 3: Skip Negative Numbers

Problem Statement

Write a C program to read 10 numbers and calculate the sum of only positive numbers. Negative numbers should be skipped using continue.

Learning Outcomes

Apply continue to filter unwanted input values.

Hint

If the entered number is negative, use continue before adding it to the sum.

Theory

Continue allows a program to ignore a particular input or case without terminating the entire loop.

Program

#include <stdio.h>

int main()
{
    int i, num, sum = 0;

    // Read 10 numbers
    for (i = 1; i <= 10; i++)
    {
        printf("Enter number %d: ", i);
        scanf("%d", &num);

        // Ignore negative numbers
        if (num < 0)
        {
            continue;
        }

        sum = sum + num;
    }

    printf("Sum of positive numbers = %d", sum);

    return 0;
}

Expected Output

Enter number 1: 10
Enter number 2: -5
Enter number 3: 20
Enter number 4: -2
Enter number 5: 15
...
Sum of positive numbers = 45

Note

Negative values are ignored and are not included in the sum.

Exercise 4: Skip Multiples of 3

Problem Statement

Write a C program to display numbers from 1 to 30 except the numbers divisible by 3.

Learning Outcomes

Use continue with a divisibility condition.

Hint

Check i % 3 == 0 and skip that iteration.

Theory

The continue statement can be combined with conditional statements to ignore values satisfying a particular condition.

Program

#include <stdio.h>

int main()
{
    int i;

    for (i = 1; i <= 30; i++)
    {
        // Skip multiples of 3
        if (i % 3 == 0)
        {
            continue;
        }

        printf("%d ", i);
    }

    return 0;
}

Expected Output

1 2 4 5 7 8 10 11 13 14 16 17 19 20 22 23 25 26 28 29

Note

Every multiple of 3 is skipped without stopping the loop.

Exercise 5: Skip Zero Values

Problem Statement

Write a C program to read 8 numbers and calculate their sum, but ignore all zero values using continue.

Learning Outcomes

Understand how continue can be used to ignore special input values.

Hint

When the input is zero, use continue before performing the addition.

Theory

Continue is appropriate when an iteration should be ignored but the loop itself must continue.

Program

#include <stdio.h>

int main()
{
    int i, num, sum = 0;

    for (i = 1; i <= 8; i++)
    {
        printf("Enter number %d: ", i);
        scanf("%d", &num);

        // Ignore zero values
        if (num == 0)
        {
            continue;
        }

        sum = sum + num;
    }

    printf("Sum excluding zeros = %d", sum);

    return 0;
}

Expected Output

Enter number 1: 10
Enter number 2: 0
Enter number 3: 20
Enter number 4: 5
Enter number 5: 0
...
Sum excluding zeros = 35

Note

Zero values are ignored and do not affect the sum.

Exercise 6: Print Numbers Not Divisible by 5

Problem Statement

Write a C program to display numbers from 1 to 50 that are not divisible by 5.

Learning Outcomes

Use continue to skip values based on a mathematical condition.

Hint

Use the modulus operator and skip numbers for which i % 5 == 0.

Theory

Continue moves execution directly to the next iteration, making it useful for filtering values during a loop.

Program

#include <stdio.h>

int main()
{
    int i;

    for (i = 1; i <= 50; i++)
    {
        // Skip numbers divisible by 5
        if (i % 5 == 0)
        {
            continue;
        }

        printf("%d ", i);
    }

    return 0;
}

Expected Output

1 2 3 4 6 7 8 9 11 12 13 14 16 17 18 19 ... 49

Note

The loop continues through all numbers, but multiples of 5 are not printed.

Exercise 7: Accept Only Positive Marks

Problem Statement

Write a C program to read marks of 10 students. Ignore invalid negative marks using continue and calculate the total of valid marks.

Learning Outcomes

Apply continue to basic input validation.

Hint

When a mark is negative, skip the current iteration.

Theory

Continue can be used to skip invalid data while allowing the program to process the remaining inputs.

Program

#include <stdio.h>

int main()
{
    int i, marks, total = 0;

    for (i = 1; i <= 10; i++)
    {
        printf("Enter marks for student %d: ", i);
        scanf("%d", &marks);

        // Ignore invalid negative marks
        if (marks < 0)
        {
            continue;
        }

        total = total + marks;
    }

    printf("Total of valid marks = %d", total);

    return 0;
}

Expected Output

Enter marks for student 1: 70
Enter marks for student 2: 85
Enter marks for student 3: -1
Enter marks for student 4: 60
...
Total of valid marks = 215

Note

A negative mark is treated as invalid and is skipped.

Exercise 8: Skip Numbers Between 10 and 20

Problem Statement

Write a C program to display numbers from 1 to 30, but skip all numbers from 10 to 20.

Learning Outcomes

Use continue with a range-based condition.

Hint

Use an if condition that checks whether i lies between 10 and 20.

Theory

Continue can skip multiple consecutive values when the condition represents a range.

Program

#include <stdio.h>

int main()
{
    int i;

    for (i = 1; i <= 30; i++)
    {
        // Skip values from 10 to 20
        if (i >= 10 && i <= 20)
        {
            continue;
        }

        printf("%d ", i);
    }

    return 0;
}

Expected Output

1 2 3 4 5 6 7 8 9 21 22 23 24 25 26 27 28 29 30

Note

All iterations corresponding to numbers 10 through 20 are skipped.

Exercise 9: Display Valid Numbers from User Input

Problem Statement

Write a C program to read 10 numbers and display only numbers greater than or equal to 10. Values below 10 should be skipped using continue.

Learning Outcomes

Use continue for conditional filtering of user input.

Hint

If the entered number is less than 10, use continue.

Theory

Continue is useful in data-processing programs when certain values need to be ignored without stopping the entire process.

Program

#include <stdio.h>

int main()
{
    int i, num;

    for (i = 1; i <= 10; i++)
    {
        printf("Enter number %d: ", i);
        scanf("%d", &num);

        // Skip numbers below 10
        if (num < 10)
        {
            continue;
        }

        printf("Valid number: %d\n", num);
    }

    return 0;
}

Expected Output

Enter number 1: 5
Enter number 2: 25
Valid number: 25
Enter number 3: 8
Enter number 4: 40
Valid number: 40

Note

Only values satisfying the required condition are processed further.

Exercise 10: Skip Multiples of 2 and 3

Problem Statement

Write a C program to display numbers from 1 to 50, skipping numbers divisible by either 2 or 3.

Learning Outcomes

Combine logical conditions with continue to filter multiple categories of values.

Hint

Skip the current iteration when the number is divisible by 2 or by 3.

Theory

Continue can be used with compound logical conditions to ignore values that belong to one or more unwanted categories.

Program

#include <stdio.h>

int main()
{
    int i;

    for (i = 1; i <= 50; i++)
    {
        // Skip numbers divisible by 2 or 3
        if (i % 2 == 0 || i % 3 == 0)
        {
            continue;
        }

        printf("%d ", i);
    }

    return 0;
}

Expected Output

1 5 7 11 13 17 19 23 25 29 31 35 37 41 43 47 49

Note

Only numbers that are neither divisible by 2 nor divisible by 3 are displayed.