Easy
Add two numbers
Write a programme that adds two numbers and prints the result.
Show answer:
int a = 5;
int b = 7;
int sum = a + b;
System.out.println("Sum: " + sum); // prints 12
Get remainder
Write a programme that prints the remainder value after dividing 13 by 3;
Show answer:
System.out.println(13 % 3); // prints 1
Comparison of two numbers
Write a programme that compares two numbers and prints whether the first number is greater, less, or equal to the second.
Show answer:
int a = 5;
int b = 7;
if (a > b) {
System.out.println(a + " is greater than " + b);
} else if (a < b) {
System.out.println(a + " is less than " + b); // prints 5 is less than 7
} else {
System.out.println(a + " is equal to " + b);
}
Are all conditions true?
Write a programme that checks if both conditions are true: age >= 18 and isStudent == true.
Show answer:
int age = 20;
boolean isStudent = true;
if (age >= 18 && isStudent) {
System.out.println("Eligible for student discount.");
} else {
System.out.println("Not eligible for student discount.");
}
Medium
Check if a Number is Divisible by 3
Write a programme that checks if a number is divisible by 3 and prints the result.
Show answer:
int number = 9;
if (number % 3 == 0) {
System.out.println(number + " is divisible by 3"); // prints 9 is divisble by 3
} else {
System.out.println(number + " is not divisible by 3");
}
Calculate Discount Based on Spending
Write a programme that calculates a discount based on the amount spent.
- If spending > 200, 20% discount.
- If spending > 100, 10% discount.
- If spending > 50, 5% discount.
- Otherwise, no discount.
Show answer:
int spend = 150;
double discount = 0;
if (spend > 200) {
discount = 0.20;
} else if (spend > 100) {
discount = 0.10;
} else if (spend > 50) {
discount = 0.05;
}
System.out.println("Discount: " + discount * 100 + "%"); // prints 10.0%
Check if a Number is Positive, Negative, or Zero
Write a programme that checks whether a number is positive, negative, or zero.
Show answer:
int number = -5;
if (number > 0) {
System.out.println("Positive"); // prints positive
} else if (number < 0) {
System.out.println("Negative");
} else {
System.out.println("Zero");
}
Hard
Calculate Grade Based on Marks
Write a programme that calculates a grade based on marks:
- 90 and above: “A”
- 80 to 89: “B”
- 70 to 79: “C”
- 60 to 69: “D”
- Below 60: “F”
Show answer:
int marks = 85;
if (marks >= 90) {
System.out.println("A");
} else if (marks >= 80) {
System.out.println("B"); // prints B
} else if (marks >= 70) {
System.out.println("C");
} else if (marks >= 60) {
System.out.println("D");
} else {
System.out.println("F");
}
Calculate Discount Using Logical Operators
Write a programme to calculate a discount:
- 20% if age > 18 and is a student.
- 10% if age <= 18 and is a student.
- 5% if age > 18 and not a student.
- 0% otherwise.
Show answer:
int age = 20;
boolean isStudent = true;
double discount = 0;
if (age > 18 && isStudent) {
discount = 0.20;
} else if (age <= 18 && isStudent) {
discount = 0.10;
} else if (age > 18 && !isStudent) {
discount = 0.05;
}
System.out.println("Discount: " + discount * 100 + "%"); // prints 20.0%
Leave a Reply