# C Program to Display Numbers From 1 to n Except 6 and 9

## Overview

How to write a c program to display numbers from 1 to n except 6 and 9? This is a very simple c program. For doing this program, you must know what is the loop and continue statement in c programming?

Actually, we can do this program using a continue statement and without using a continue statement. Before that, I am just writing a program in c to display numbers from 1 to n including 6 and 9.

### C Program to Display Numbers From 1 to n Including 6 and 9

Here, we can use any loop. It may be for, while or do-while loop.

``````#include <stdio.h>
#include <stdlib.h>
int main()
{
int i, n;
printf("Enter the value of n: ");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
printf("%d ",i);
}
return 0;
}``````

#### Output

``````Enter the value of n: 10
1 2 3 4 5 6 7 8 9 10``````

As you can see the output of the above program. We have displayed all the numbers from 1 to n. We have read the value of n from the user. But this is not the actual program that we want to write.

We have to skip 6 and 9 both if the list is greater than the number 6 and 9. This is confirm that we have to use loop. In c programming, there are three type of loops and they are for loop, while loop and do-while loop.

Also Read: The while loop in C Programming

We know, we can repeat any number of statements using loops. So, here we are also repeating the numbers starting from 1 to n. Therefore, we are initializing the value of i=1. The variable i is the loop variable. Next, we need to print the values of the variable i up to n. That is why, we reading the value of n from the user. Next, we need to increment the value of i by 1. This is also important step in the loop statement.

Now, come to our title. We have to print the list without printing 6 and 9. In this article, I am using two approaches.

1. Without using continue statement.
2. Using continue statement.

Let’s see both approaches.

### C Program to Display Numbers From 1 to n Except 6 and 9 without using continue statement.

In this program, we will use if-else statement. In if statement, we will simply check if the value of i is not 6 and 9, then print the values of i, otherwise, no need to print anything.

See the program.

``````#include <stdio.h>
#include <stdlib.h>
int main()
{
int i, n;
printf("Enter the value of n: ");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
if( (i!=6) && (i!=9) )
{
printf("%d ",i);
}
}
return 0;
}``````

### Output

``````Enter the value of n: 10
1 2 3 4 5 7 8 10``````

We have written if statement in the for loop. In this if statement, we are testing the value of i. Suppose the value of i is 1, then the condition becomes true. Because we are checking here that the value of i must not be equal to 6 or 9. Therefore, body of this if statement will be executed and 1 will be printed. In this way, the values of i i.e. 2, 3, 4 and 5 will be displayed.

Now, the next value of i is 6. In the if statement, we have a condition if( (i!=6) && (i!=9) ) i.e. the body of if statement will be executed only when the value of i must not be equal to 6 or 9. But now, this condition becomes false. Why? Because the value of i is 6. Therefore, this time 6 will not be printed. Similar thing is happened with 9 also.

I hope you understood this logic. Now, we can write the same program using continue statement.

### C Program to Display Numbers From 1 to n Except 6 and 9 using continue statement.

``````#include <stdio.h>
#include <stdlib.h>
int main()
{
int i, n;
printf("Enter the value of n: ");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
if( (i==6) || (i==9) )
{
continue;
}
printf("%d ",i);
}
return 0;
}``````

The whole program is the same but there is only one difference. You can observe that difference in the if statement.

We have changed the if the condition from “not equal to” to the “equal to”. In the last program, we were executing the body of if statement when the value of i is not equal to 6 and 9. But, this time the body of if statement will be executed when the value of i is equal to 6 and 9. In the body of if statement, we have written continue statement. Whenever the continue statement is encountered, then the control will transfer to the beginning of the loop. That means it will not execute further statements. That means the printf() statement will not be executed.

If you know the language Hindi, then I have made a video on break and continue statement. You can watch it here.

## FAQs

We can do this program using two ways:
1. Using continue statement
2. Without continue statement
1. Using continue statement
int main()
{
int i;
for(i = 1; i<=10; i++)
{
if(i==5)
{
continue; // control will be transferred to the beginning of the loop
}
printf(“%d “, i);
}
return 0;
}

2. Without using the continue statement
int main()
{
int i;
for(i = 1; i<=10; i++)
{
if(i != 5)
{
printf(“%d “, i); // when i is not equal to 5
}
}
return 0;
}

Output:
1 2 3 4 6 7 8 9 10