In this tutorial, we discuss a concept of C program to display even and odd number in the given range
What is odd or even
when you devide a number by two and if the balance is zero, it is an even number
when you divided number by two and if the balance is one, it is an odd number
Example of even number 2,4,6,8,…..
Example of odd number 1,3,5,7,…..
Here we will use a modular operator to find and display odd or even numbers in the given range.
This program allows the user to enter two different digits and then, the program will display odd numbers and even numbers between entered digits using for loop
Program 1
#include <stdio.h> #include <stdlib.h> int main() { int num1, num2,r,i; printf("Enter the first number for the range: "); scanf("%d",&num1); //received the number for num1 printf("Enter the second number for the range: "); scanf("%d",&num2); //received the number for num2 printf("\nDisplay the even numbers between %d and %d are: ",num1,num2); for(i=num1; i<=num2; i++){ r=i%2; if(r==0) printf("\n %d",i); } printf("\n\nDisplay the odd numbers between %d and %d are: ",num1,num2); for(i=num1; i<=num2; i++){ r=i%2; if(r==1) printf("\n %d",i); } getch(); return 0; }
When the above code is executed, it produces the following results
Enter the first number for the range: 20 Enter the second number for the range: 40 Display the even numbers between 20 and 40 are: 20 22 24 26 28 30 32 34 36 38 40 Display the odd numbers between 20 and 40 are: 21 23 25 27 29 31 33 35 37 39
Program 2
This program allows the user to enter two different digits and then, the program will display odd numbers and even numbers between entered digits using while loop
#include <stdio.h> #include <stdlib.h> int main() { int num1,num2,r,i; printf("Enter the first number for the range: "); scanf("%d",&num1); //read first number to num1 printf("Enter the second number for the range: "); scanf("%d",&num2); //read second number to num2 printf("\nDisplay even number between %d and %d are:",num1,num2); i=num1; while(i<=num2){ r=i%2; if(r==0) printf("\n%d",i); i++; } printf("\n\nDisplay odd number between %d and %d are",num1,num2); i=num1; while(i<=num2){ r=i%2; if(r==1) printf("\n%d",i); i++; } getch(); return 0; }
When the above code is executed, it produces the following results
Enter the first number for the range: 23 Enter the second number for the range: 35 Display even number between 23 and 35 are 24 26 28 30 34 Display odd number between 23 and 35 are 23 25 27 29 31 33 35
Program 3
This program allows the user to enter two different digits and then, the program will display odd numbers and even numbers between entered digits using do-while loop
#include <stdio.h> #include <stdlib.h> int main() { int num1,num2,r,i; printf("Enter the first number for the range: "); scanf("%d",&num1); //read first number to num1 printf("Enter the second number for the range: "); scanf("%d",&num2); //read second number to num2 printf("\nDisplay even number between %d and %d are",num1,num2); i=num1; do{ r=i%2; if(r==0) printf("\n%d",i); i++; }while(i<=num2); printf("\n\nDisplay odd number between %d and %d are",num1,num2); i=num1; do{ r=i%2; if(r==1) printf("\n%d",i); i++; }while(i<=num2); getch(); return 0; }
When the above code is executed, it produces the following results
Enter the first number for the range: 45 Enter the second number for the range: 60 Display even number between 45 and 60 are: 46 48 50 52 54 56 58 60 Display odd number between 45 and 60 are: 45 47 49 51 53 55 57 59
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
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 display all even and odd numbers from 1 to n
C++ program calculate Average of odd and even numbers in an array
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
Java program to calculate sum of odd and even numbers
C++ program to calculate sum of odd and even numbers
Python program to calculate sum of odd and even numbers
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…