Question 01)) Write a Java program that checks whether a given number is even or odd and prints the result..
Ans:
package HelloWorld;
import java.util.Scanner;
public class LearningJava {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int a = scan.nextInt();
if (a % 2 == 0){
System.out.print("Even");
}
else {
System.out.print("Odd");
}
}
}
Let me Explain: Yahan humne Scanner ka istemal kiya hai jisse user se input liya ja sake. Fir humne ek integer variable a mein input liya. Uske baad if-else statement ka istemal kiya hai. Yadi a ko 2 se divide karke remainder 0 aata hai, tab woh even hai, aur agar remainder kuch aur hota hai, tab woh odd hai.
Jab aap koi number enter karenge, code us number ko check karega ki woh even hai ya odd, aur uska result print karega.
This is Question of Modulo %
![]() |
Even Number |
Ans:
package HelloWorld;
import java.util.Scanner;
public class LearningJava {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int age = scan.nextInt();
if (age >= 18) {
System.out.print("You are Eligible For Vote");
}
else {
System.out.print("You are not Eligible for Vote");
}
}
}
Question 03) Develop a program that takes two integers as input and checks if the first number is divisible by the second number without leaving a remainder.
package HelloWorld;
import java.util.Scanner;
public class LearningJava {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int first = scan.nextInt();
int second = scan.nextInt();
if (second !=0 && first % second == 0) {
System.out.print(first + " " + "is divisible by" + " " + second + " " + "Without living a reminder");
}
else {
System.out.print(first +" " + "is not divisbile by" + " " + second + " " + "without living a reminder");
}
}
}
package HelloWorld;
import java.util.Scanner;
public class LearningJava {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.print("Enter Your Age");
int age = scan.nextInt();
if (age >= 60) {
System.out.print("Congratulatios! you are eligible for senior citizen benefits");
}
else {
System.out.print("Oho! you are not eligible for senior citizen benefits");
}
}
}
Comments
Post a Comment