Write a C program to display numbers from 1 to 10, but skip the number 5 using the continue statement.
Write a C program to display numbers from 1 to 10, but skip the number 5 using the continue statement.
Understand how continue skips the remaining statements of the current iteration.
When i == 5, use continue.
The continue statement skips the remaining statements of the current loop iteration and moves to the next iteration. :contentReference[oaicite:0]{index=0}
#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;
}
1 2 3 4 6 7 8 9 10
Only the iteration for 5 is skipped; the loop continues normally with 6.
Write a C program to display odd numbers from 1 to 20 using the continue statement.
Use continue to skip unwanted values during loop execution.
When a number is even, use continue to skip its printing.
Continue is useful when certain values should be ignored while the remaining iterations continue normally.
#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;
}
1 3 5 7 9 11 13 15 17 19
Whenever an even number is encountered, continue skips its printing.
Write a C program to read 10 numbers and calculate the sum of only positive numbers. Negative numbers should be skipped using continue.
Apply continue to filter unwanted input values.
If the entered number is negative, use continue before adding it to the sum.
Continue allows a program to ignore a particular input or case without terminating the entire loop.
#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;
}
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
Negative values are ignored and are not included in the sum.
Write a C program to display numbers from 1 to 30 except the numbers divisible by 3.
Use continue with a divisibility condition.
Check i % 3 == 0 and skip that iteration.
The continue statement can be combined with conditional statements to ignore values satisfying a particular condition.
#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;
}
1 2 4 5 7 8 10 11 13 14 16 17 19 20 22 23 25 26 28 29
Every multiple of 3 is skipped without stopping the loop.
Write a C program to read 8 numbers and calculate their sum, but ignore all zero values using continue.
Understand how continue can be used to ignore special input values.
When the input is zero, use continue before performing the addition.
Continue is appropriate when an iteration should be ignored but the loop itself must continue.
#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;
}
Enter number 1: 10 Enter number 2: 0 Enter number 3: 20 Enter number 4: 5 Enter number 5: 0 ... Sum excluding zeros = 35
Zero values are ignored and do not affect the sum.
Write a C program to display numbers from 1 to 50 that are not divisible by 5.
Use continue to skip values based on a mathematical condition.
Use the modulus operator and skip numbers for which i % 5 == 0.
Continue moves execution directly to the next iteration, making it useful for filtering values during a loop.
#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;
}
1 2 3 4 6 7 8 9 11 12 13 14 16 17 18 19 ... 49
The loop continues through all numbers, but multiples of 5 are not printed.
Write a C program to read marks of 10 students. Ignore invalid negative marks using continue and calculate the total of valid marks.
Apply continue to basic input validation.
When a mark is negative, skip the current iteration.
Continue can be used to skip invalid data while allowing the program to process the remaining inputs.
#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;
}
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
A negative mark is treated as invalid and is skipped.
Write a C program to display numbers from 1 to 30, but skip all numbers from 10 to 20.
Use continue with a range-based condition.
Use an if condition that checks whether i lies between 10 and 20.
Continue can skip multiple consecutive values when the condition represents a range.
#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;
}
1 2 3 4 5 6 7 8 9 21 22 23 24 25 26 27 28 29 30
All iterations corresponding to numbers 10 through 20 are skipped.
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.
Use continue for conditional filtering of user input.
If the entered number is less than 10, use continue.
Continue is useful in data-processing programs when certain values need to be ignored without stopping the entire process.
#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;
}
Enter number 1: 5 Enter number 2: 25 Valid number: 25 Enter number 3: 8 Enter number 4: 40 Valid number: 40
Only values satisfying the required condition are processed further.
Write a C program to display numbers from 1 to 50, skipping numbers divisible by either 2 or 3.
Combine logical conditions with continue to filter multiple categories of values.
Skip the current iteration when the number is divisible by 2 or by 3.
Continue can be used with compound logical conditions to ignore values that belong to one or more unwanted categories.
#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;
}
1 5 7 11 13 17 19 23 25 29 31 35 37 41 43 47 49
Only numbers that are neither divisible by 2 nor divisible by 3 are displayed.