Program to check weather the no is odd or even in C and java please help
Answers
To understand this example, you should have the knowledge of the following C programming topics:
C Programming Operators
C if...else Statement
An even number is an integer that is exactly divisible by 2. For example: 0, 8, -24
An odd number is an integer that is not exactly divisible by 2. For example: 1, 7, -11, 15
Program to Check Even or Odd
#include <stdio.h>
int main() {
int num;
printf("Enter an integer: ");
scanf("%d", &num);
// true if num is perfectly divisible by 2
if(num % 2 == 0)
printf("%d is even.", num);
else
printf("%d is odd.", num);
return 0;
}
Output
Enter an integer: -7
-7 is odd.
In the program, the integer entered by the user is stored in the variable num.
Then, whether num is perfectly divisible by 2 or not is checked using the modulus % operator.
If the number is perfectly divisible by 2, test expression number%2 == 0 evaluates to 1 (true). This means the number is even.
However, if the test expression evaluates to 0 (false), the number is odd.
➥ C Program to check weather the no is odd or even
#include<stdio.h>
int main(){
int i;
printf("Enter a Number:\n");
scanf("%d",&i);
if(i%2==0)
{
printf("The number %d is Even",i);
}
else
{
printf("The number %d is Odd",i);
}
}
EXAMPLE OUTPUT
Enter a number :
5
The number 5 is Odd
Note: Program has been run in the above attachment with output attached.
➥ JAVA Program to check weather the no is odd or even
import java.util.Scanner;
public class CheckOddEven {
public static void main(String[] args){
int n;
Scanner s = new Scanner(System.in);
System.out.print("Enter the number:");
n = s.nextInt();
if(n % 2 == 0)
{
System.out.println("The number "+n+" is Even ");
}
else
{
System.out.println("The number "+n+" is Odd ");
}
}
}
EXAMPLE OUTPUT
Enter the number: 6
The number 6 is Even
Note: Program has been run in the above attachment with output attached.