1.Accept a number and print if it is an even or an odd number (using if else).
Answers
Explanation:
a=int(input("Enter your number"))
if a%2==0:
print(a "is an even integer")
elif a%2==1:
print(a " is an odd integer")
else:
print("invalid input")
The following program has been written using Python.
num = int(input("Enter an integer: "))
if num%2 == 0:
print("The given integer is an even number.")
else:
print("The given integer is an odd number.")
The following program has been written using Java.
import java.util.Scanner;
public class Main
{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter an integer: ");
int num = sc.nextInt();
if (num%2 == 0) {
System.out.println("The given integer is an even number.");
}
else {
System.out.println("The given integer is an odd number.");
}
}
}
The following program has been written using C.
#include <stdio.h>
int main()
{
int num;
printf("Enter an integer: ");
scanf("%d", &num);
if(num%2 == 0)
printf("The given integer is an even number.");
else
printf("The given integer is an odd number.");
return 0;
}
The following program has been written using C++.
#include <iostream>
using namespace std;
int main() {
int num;
cout << "Enter an integer: ";
cin >> num;
if (num%2 == 0)
cout << "The given integer is an even number.";
else
cout << "The given integer is an even number.";
return 0;
}
The following program has been written using QBasic.
CLS
INPUT "Enter a Number: ", num
IF num MOD 2 = 0 THEN
PRINT "The given integer is an even number."
ELSE
PRINT "The given integer is an odd number."
END IF
END
Logic:
- Take user input and store it in integer type value. Read: num.
- Check: if (num%2 == 0) is true, display num is an even number.
- If (num%2 == 0) is false, display num is an even number.
I have added a .txt file from where you can get all the codes.