Write a program to check whether a number is even or odd
Answers
Answer:
Explanation:
#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.
Answer:
Here comes the required program.
1. In Java.
import java.util.*;
public class Number {
public static void main(String args[]) {
System.out.print("Enter a number: ");
int n=(new Scanner(System.in)).nextInt();
if(n%2==0)
System.out.println("Even.");
else
System.out.println("Odd.");
}
}
2. In Python.
n=int(input("Enter a number: "))
if n%2==0:
print("Even.")
else:
print("Odd.")
3. In C
#include <stdio.h>
int main() {
int n;
printf("Enter a number: ");
scanf("%d",&n);
if(n%2==0)
printf("Even.");
else
printf("Odd.");
return 0;
}
4. In C++
#include <iostream>
using namespace std;
int main() {
int n;
cout<<"Enter a number: ";
cin>>n;
if(n%2==0)
cout<<"Even.";
else
cout<<"Odd.";
return 0;
}
To check if the number is even or not, we will check if the remainder obtained after dividing the number by 2 is 0 or not. If the remainder is zero, then the number is even else not.
I have added a .txt file from where you can get all the cσdes.
•••♪