Program: Check if a number is positive
java
import [Link];
public class SimpleIfExample {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter a number: ");
int number = [Link]();
// Simple if statement
if (number > 0) {
[Link]("The number is positive.");
}
}
}
simple if task for a student that checks whether the student’s
marks are above the passing grade (assuming a passing grade of 50).
public class StudentPassFail {
public static void main(String[] args) {
// Hard-coded marks
int marks = 65;
// Simple if statement to check if the student passed
if (marks >= 50) {
[Link]("Congratulations! You passed.");
}
}
}
import [Link];
public class VotingEligibility {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter your age: ");
int age = [Link]();
if (age >= 18) {
[Link]("You are eligible to vote.");
} else {
[Link]("You are not eligible to vote.");
}
}
}
If you want to divide a given number by 3 and check if it's divisible
by 3 using an if-else statement, here's how you can write the code.
The task will involve dividing the number by 3 and checking the result,
then displaying whether the number is divisible by 3 or not.
import [Link];
public class AgeCategory {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter your age: ");
int age = [Link]();
// Using if-else if to categorize the person based on their age
if (age >= 65) {
[Link]("You are a Senior Citizen.");
} else if (age >= 18) {
[Link]("You are an Adult.");
} else if (age >= 13) {
[Link]("You are a Teenager.");
} else if (age >= 3) {
[Link]("You are a Child.");
} else {
[Link]("You are an Infant.");
}
}
}
} else if (year > currentYear) {
import [Link]; [Link](year + " is in the future.");
} else {
public class LeapYearCheck { [Link](year + " is the current year.");
public static void main(String[] args) { }
Scanner sc = new Scanner([Link]); } else {
[Link](year + " is not a leap year.");
[Link]("Enter a year: "); }
int year = [Link](); }
// First, check if the year is a leap year
if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))
{
[Link](year + " is a leap year.");
// Nested if to check if the year is in the past or future
int currentYear = 2025; // You can dynamically get the
current year using [Link] or LocalDate in real-world
applications
if (year < currentYear) {
[Link](year + " is in the past.");
}
case 4:
import [Link]; [Link]("Thursday");
break;
public class SwitchExample { case 5:
public static void main(String[] args) { [Link]("Friday");
Scanner sc = new Scanner([Link]); break;
case 6:
[Link]("Enter a number between 1 and 7: "); [Link]("Saturday");
int day = [Link](); break;
case 7:
switch (day) { [Link]("Sunday");
case 1: break;
[Link]("Monday"); default:
break; [Link]("Invalid number. Please enter a
case 2: number between 1 and 7.");
[Link]("Tuesday"); }
break; }
case 3: }
[Link]("Wednesday");
break;
•for loop
•while loop
•do-while loop
For each
The for loop is typically used when you know the number of iterations in advance. It consists of three main parts:
•Initialization: Typically used to initialize the loop counter.
•Condition: The loop continues as long as this condition evaluates to true.
•Update: This part updates the counter after each iteration.
Syntax:
java
for (initialization; condition; update) { // Code to be executed }
public class ForLoopExample {
public static void main(String[] args) {
// Prints numbers from 1 to 5
for (int i = 1; i <= 5; i++) {
[Link](i);
}
}
}
Calculating the Sum of Numbers from 1 to 10
Multiplication Table Using for Loop
Calculating Factorial Using for Loop
Countdown Using for Loop
Sum of Even Numbers Using for Loop
public class ForLoopSumExample {
public static void main(String[] args) {
int sum = 0;
// Using a for loop to calculate the sum of numbers from 1
to 10
for (int i = 1; i <= 10; i++) {
sum += i; // Adds i to sum on each iteration
}
// Prints the final sum
[Link]("The sum of numbers from 1 to 10 is: "
+ sum);
}
}
import [Link];
public class MultiplicationTable {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
// Ask user for the number
[Link]("Enter a number: ");
int number = [Link]();
// Print multiplication table of the given number using a for
loop
for (int i = 1; i <= 10; i++) {
[Link](number + " x " + i + " = " + (number *
i));
}
}
}
import [Link];
public class FactorialExample {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
// Ask user for a number
[Link]("Enter a number: ");
int num = [Link]();
// Initialize the factorial result as 1
int factorial = 1;
// Using a for loop to calculate the factorial
for (int i = 1; i <= num; i++) {
factorial *= i; // Multiply factorial by i
}
// Print the result
[Link]("The factorial of " + num + " is: " +
factorial);
}
}
import [Link];
public class CountdownExample {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
// Ask user for a number
[Link]("Enter a number to start countdown: ");
int startNumber = [Link]();
// Using a for loop to count down from the entered
number to 1
for (int i = startNumber; i >= 1; i--) {
[Link](i);
}
// Print a message after the countdown is complete
[Link]("Countdown complete!");
}
}
import [Link];
public class SumEvenNumbersExample {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
// Ask the user for a number
[Link]("Enter a number: ");
int num = [Link]();
// Variable to store the sum of even numbers
int sum = 0;
// Using a for loop to sum even numbers from 1 to num
for (int i = 1; i <= num; i++) {
if (i % 2 == 0) { // Check if i is even
sum += i; // Add the even number to sum
}
}
// Print the result
[Link]("The sum of even numbers from 1 to "
+ num + " is: " + sum);
}
}
import [Link];
public class ArmstrongNumberChecker {
while (condition) {
public static void main(String[] args) {
// code to run
Scanner scanner = new Scanner([Link]);
}
// Input from user
[Link]("Enter a number: ");
if (result == num) { int num = [Link]();
[Link](num + " is an Armstrong number.");
} else { int originalNum = num; // Save the original number
[Link](num + " is not an Armstrong int result = 0;
number.");
} // Check each digit
while (originalNum != 0) {
// Close the scanner int remainder = originalNum % 10; // Get the last digit
[Link](); result += remainder * remainder * remainder; // Add
} the cube of the digit to the result
} originalNum /= 10; // Remove the last digit
}
// Check if the sum of cubes is equal to the original
number
do {
// Loop body
} while (condition);
do {
int remainder = originalNum % 10; // Get the last digit
result += remainder * remainder * remainder; // Add
the cube of the digit to the result
originalNum /= 10; // Remove the last digit
} while (originalNum != 0); // Continue until all digits are
processed
public class ForEachExample {
public static void main(String[] args) {
int[] numbers = {1, 2, 3, 4, 5};
// For-each loop
for (int num : numbers) {
[Link](num);
}
}
}