C program to check odd or even
Advertisements
C program to check odd or even
Even numbers are those which are divisible by 2, and which numbers are not divisible 2 is called odd numbers.
But in term of programming for find even number we check the remainder of number is zero or not when number divided by zero, If remainder is equal to zero that means number is divisible by 2. To find remainder of any number you need to use modulo (%) operator in C language, which return remainder as a result.
C program to check odd or even
#include<stdio.h> #include<conio.h> void main() { int no; clrscr(); printf("Enter any number: "); scanf("%d",&no); if(no%2==0) { printf("Even num"); } else { printf("Odd num"); } getch(); }
Output
Enter any num: 5 Odd num
C program to check odd or even using ternary or conditional operator
Check give number is Even or Odd
#include<stdio.h> #include<conio.h> void main() { int no; clrscr(); printf("Enter any num : "); scanf("%d",&no); (no%2==0) ? printf("Even number") : printf("Odd number"); getch(); }
Output
Enter any num : 6 Even number
C program to check odd or even using bitwise operator
C program to check odd or even
#include<stdio.h> #include<conio.h> void main() { int num; clrscr(); printf("Enter any num : "); scanf("%d",&num); if(num & 1) { printf("%d is odd",num); } else { printf("%d is even",num); } getch(); }
Output
Enter any num: 20 20 is even
Program to print even number upto 100
program to print even number upto 100
#include<stdio.h> #include<conio.h> void main() { int n=2; clrscr(); while(n<=100) { printf(" %d",n); n=n+2; } getch(); }
Output
2 4 6 8 10 12....100
Google Advertisment