C Program to Print Numbers Except Multiples of n

Introduction

In this article, I am writing a program in c to print numbers from m1 to m2 except for multiples of n. Let’s see the expected output first and then it will be easy for you to understand the aim of this program.

Also read: C Program to Display Numbers From 1 to n Except 6 and 9

Expected Output

Enter the range for printing numbers
1
10
Enter the number to exclude its multiples
4
1 2 3 5 6 7 9 10

Here, we have to ask the user to enter the range of numbers. The range means the numbers between the first number and the last number. In the above output, we have provided the range 1 and 10 that means we have to print the numbers between 1 and 10.

But there is a twist. The main goal of this program is to write a program in c to print numbers except multiples of n. So, we have to ask the user the number which we do not have to include in the given list. Not only this number but we have to exclude its multiples also.

Also Read: Interview Questions On C

In the above output, user enter the number 4. So, we have to exclude 4 and 8. Why 4 and 8 only? Because we have to print number in the range 1 and 10. Suppose our range is 10 to 100, then we will have to print the numbers from 10 to 100 except the multiples of 4.

I am writing here same program using and without using continue statement. When continue statement is encountered then control will transfer to the beginning of the loop.

I hope you got the logic of this program. Now, we will see the actual program.

C Program to Print Numbers Except Multiples of n using continue statement

#include <stdio.h>
#include <stdlib.h>
int main()
{
    int m1,m2,n,i;
    printf("Enter the range for printing numbers\n");
    scanf("%d%d",&m1,&m2);
    printf("Enter the number to exclude its multiples\n");
    scanf("%d",&n);
    for(i=m1;i<=m2;i++)
    {
        if((i%n)==0)
        {
            continue;
        }
        printf("%d ",i);
    }
    return 0;
}

The above program will display the number within a given range except for multiples of n. In this program, m1 will store initial value and m2 will store the final value. The variable n is the number that we have to exclude from this series with its multiples.

Also read: C Program to Print Multiples of 5 using do while loop

See the following important part of this program.

    for(i=m1;i<=m2;i++)
    {
        if((i%n)==0)
        {
            continue;
        }
        printf("%d ",i);
    }

You can observe the above code. This is the for loop which will repeat the number of statements until the value of i is less than or equal to the value of m2. In this loop, I have written if statement in which we are checking that the value of i is divisible by the number n or not. If it is divisible, then the body of is will be executed and the continue statement will transfer the control to the beginning of the loop without printing that number.

Also Read: Perfect Number in C Programming using All Loops

We can write the same program without using continue statement in c programming. Just see the following program and you will get my point.

C Program to Print Numbers Except Multiples of n without using continue statement

#include <stdio.h>
#include <stdlib.h>
int main()
{
    int m1,m2,n,i;
    printf("Enter the range for printing numbers\n");
    scanf("%d%d",&m1,&m2);
    printf("Enter the number to exclude its multiples\n");
    scanf("%d",&n);
    for(i=m1;i<=m2;i++)
    {
        if((i%n)!=0)
        {
            printf("%d ",i);
        }
    }
    return 0;
}

This program will also generate the same output. Only the difference is in the if statement. The if statement in the first program has equal to operator and in the second program has not equal to operator.

Also read: Factorial Program in C Programming

For the Hindi language user, you can watch my video on same topic.

I hope, you have understood the program. If you have any difficulties regarding this program or any other programs, you can contact me.

Thank you.

Also Read: C Program To Print All The Numbers In a Given Range

FAQ

Print 1 to 10 except 5 in C Programming

Using continue statement
#include <stdio.h>
#include <stdlib.h>
int main()
{
int i;
for(i=1; i<=10; i++)
{
if(i==5)
{
continue;
}
printf(“%d “,i);
}
return 0;
}

Output:
1 2 3 4 6 7 8 9 10

Without using the continue statement
#include <stdio.h>
#include <stdlib.h>
int main()
{
int i;
for(i=1; i<=10; i++)
{
if(i!=5)
{
printf(“%d “,i);
}
}
return 0;
}

Leave a Comment