In this tutorial, we will discuss the C program to display all even and odd number from 1 to n
In this program, we are going to learn about how to find odd or even numbers from 1 to given number using the loops in the C language
What is Even or Odd
When the number is divided by 2 and the balance becomes zero and the above number is called as the even number – eg 2,4,6,8,10
and on the other sides when it is divided by 2 and balance becomes 1 they are called odd numbers or uneven numbers
In my previous post, I have explained the various ways to check whether the number is even or odd in C language
Once this program received the number it will check the given number either odd or even number of given range using loops.
After receiving the input from the user, it is stored in the variable of num and then program divides the value of num by 2 and displays the output.
Here, we can use modular operator
Example for C program to display all even number from 1 to n
In this program, we print all even numbers from 1 to n using for loop
Program 1
#include <stdio.h> #include <stdlib.h> int main() { int i,num; printf("Print all even number until\n"); scanf("%d",&num);//Reads input from user and store in variable num printf("Even number from 1 to %d are\n",num); for(i=1; i<=num; i++){//loop for iterates from 1 to maximum if(i%2==0) { printf("%d\n",i); } } return 0; }
When the above code is executed, it produces the following results
Print all even number until 50 Even number from 1 to 50 are 2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40 42 44 46 48 50
Program 2
In this program, we print all odd numbers from 1 to n using for loop
#include <stdio.h> #include <stdlib.h> int main() { int i,num; printf("Print all odd number until\n"); scanf("%d",&num);//Reads input from user and stored in variable num printf("Odd numbers from 1 to %d are\n",num); for(i=1; i<=num; i++){//loop for iterates from 1 to maximum if(i%2==1) { printf("%d\n",i); } } return 0; }
When the above code is executed, it produces the following results
Print all odd number until 50 Odd number from 1 to 50 are 1 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31 33 35 37 39 41 43 45 47 49
Program 3
In this program, we print all even number from 1 to n using the while loop
#include <stdio.h> #include <stdlib.h> int main() { int i,num; printf("Print all even number until \n"); scanf("%d",&num);//Reads input from user and stored in variable num printf("Even number from 1 to %d are\n",num); i=1; while(i<=num){//loop for iterates from 1 to maximum if(i%2==0) { printf("%d\n",i); } i++; } getch(); return 0; }
When the above code is executed, it produces the following results
Print all even number until 50 Even number from 1 to 50 are 2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40 42 44 46 48 50
Program 4
In this program, we print all odd number from 1 to n using the while loop
#include <stdio.h> #include <stdlib.h> int main() { int i,num; printf("Print all odd number until \n"); scanf("%d",&num);//Reads input from user and stored in variable num printf("Odd number from 1 to %d are\n",num); i=1; while(i<=num){//loop for iterates from 1 to maximum if(i%2==1) { printf("%d\n",i); } i++; } getch(); return 0; }
When the above code is executed, it produces the following results
Print all odd number until 50 Odd number from 1 to 50 are 1 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31 33 35 37 39 41 43 45 47 49
Program 5
In this program, we print all even number from 1 to n using the do-while loop
#include <stdio.h> #include <stdlib.h> int main() { int i,num; printf("Print all even number until \n"); scanf("%d",&num);//Reads input from user and stored in variable num printf("Even number from 1 to %d are\n",num); i=1; do{//loop for iterates from 1 to maximum if(i%2==0) { printf("%d\n",i); } i++; } while(i<=num); getch(); return 0; }
When the above code is executed, it produces the following results
Print all even number until 50 Even number from 1 to 50 are 2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40 42 44 46 48 50
Program 6
In this program, we print all odd number from 1 to n using the do-while loop
#include <stdio.h> #include <stdlib.h> int main() { int i,num; printf("Print all odd number until \n"); scanf("%d",&num);//Reads input from user and stored in variable num printf("Odd number from 1 to %d are\n",num); i=1; do{//loop for iterates from 1 to maximum if(i%2==1) { printf("%d\n",i); } i++; } while(i<=num); getch(); return 0; }
When the above code is executed, it produces the following results
Print all odd number until 50 Odd number from 1 to 50 are 1 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31 33 35 37 39 41 43 45 47 49
Suggested for you
Similar post
C program to find a number is even or odd using the function
C program to separate Odd and Even numbers from an array
C program to Electricity bill calculation using the function
C program to display all even and odd numbers from 1 to n
C program display odd and even numbers without if statements
C program to calculate the sum of odd and even numbers
C program to find whether a number is even or odd
Java program to find a number is even or odd using the method
Java program to separate Odd and Even numbers from an array
Java program to display all even and odd numbers from 1 to n
Java program display odd and even numbers without if statements
Java program to calculate the sum of odd and even numbers
Java program to find whether a number is even or odd
Python program check whether a number is odd or even
Python program to check a number is even or odd using the function
Python program to display even and odd numbers without if
Python program to display even and odd number in the given range
Separate odd and even numbers in a list to different two list
Python Program to find odd and even numbers from a list
How to find reverse number using method In this article, we will discuss the concept…
C# inverted full pyramid star pattern In this article, we will discuss the concept of…
C# Full Pyramid star pattern program In this article, we will discuss the concept of…
Program to count vowels, consonants, words, characters and space in Java In this article, we…
How to print multiplication table using Array in C++ language In this post, we will…
C Program to multiplication table using Array In this tutorial , we will discuss about…