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 check whether a number is even or odd
C program to check a number is even or odd using function
C program to separate Odd and Even numbers from an array
PHP Star Triangle pattern program In this tutorial, we will discuss about PHP Star Triangle…
PHP Full Pyramid pattern program In this tutorial, we will discuss about PHP Full Pyramid…
5 methods to add two numbers in Java In this tutorial, we will discuss the…
Python full Pyramid star pattern program In this tutorial, we will discuss the concept of…
Write a function or method to convert C into F -Entered by the user In…
How to write a function or method to convert Celsius into Fahrenheit In this tutorial,…