Write a program using a class called MyClass that consist of a main method and asks the user to input an integer and the method returns whether it’s even or odd
Answers
Answer:
import java.util.Scanner;
class MyClass
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.print("Input a number: ");
int a = sc.nextInt();
if(a%2 == 0)
{
System.out.print("Even");
else
{
System.out.print("Odd");
}
}
}
Answer:
The code in C++, C and java is
Explanation:
1. C++
#include <iostream>
using namespace std;
bool isEven(int n) { return (n % 2 == 0); }
int main()
{
int n = 101;
isEven(n) ? cout << "Even" : cout << "Odd";
return 0;
}
OR
#include <iostream>
using namespace std;
bool isEven(int n)
{
return (!(n & 1));
}
int main()
{
int n = 101;
isEven(n)? cout << "Even" :
cout << "Odd";
return 0;
}
2. C
#include <stdio.h>
#include <math.h>
int main(){
int n = 101;
if (n%2==0){
printf("Even");
}
else{
printf("Odd");
}
return 0;
}
3. Java
import java.util.Scanner;
class MyClass
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.print("Input a number: ");
int a = sc.nextInt();
if(a%2 == 0)
{
System.out.print("Even");
else
{
System.out.print("Odd");
}
}
}
#SPJ2