0% found this document useful (0 votes)
2 views

Java

The document contains multiple Java programming examples demonstrating the use of conditional statements (if-else, switch-case) and user input via Scanner. Examples include checking if a number is even or odd, determining voting eligibility, performing basic arithmetic operations, and calculating grades based on marks. Each example is structured with a main method and includes user prompts for interaction.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Java

The document contains multiple Java programming examples demonstrating the use of conditional statements (if-else, switch-case) and user input via Scanner. Examples include checking if a number is even or odd, determining voting eligibility, performing basic arithmetic operations, and calculating grades based on marks. Each example is structured with a main method and includes user prompts for interaction.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

1.

if-else Example – Even or Odd Checker


public class IfElseExample {
public static void main(String[]
args) {
int number = 10;

if (number % 2 == 0) {
System.out.println(number + "
is Even.");
} else {
System.out.println(number + "
is Odd.");
}
}
}

2. Scanner Example – Input from User


import java.util.Scanner;

public class ScannerExample {


public static void main(String[]
args) {
Scanner sc = new
Scanner(System.in);

System.out.print("Enter your
name: ");
String name = sc.nextLine();

System.out.println("Hello, " +
name + "!");
sc.close();
}
}

3. switch-case Example – Simple Calculator


import java.util.Scanner;

public class SwitchCaseExample {


public static void main(String[]
args) {
Scanner sc = new
Scanner(System.in);

System.out.println("Choose an
operation:");
System.out.println("1. Add");
System.out.println("2.
Subtract");

System.out.print("Enter your
choice (1 or 2): ");
int choice = sc.nextInt();

System.out.print("Enter first
number: ");
int a = sc.nextInt();

System.out.print("Enter second
number: ");
int b = sc.nextInt();

switch (choice) {
case 1:
System.out.println("Sum =
" + (a + b));
break;
case 2:

System.out.println("Difference = " + (a -
b));
break;
default:

System.out.println("Invalid choice.");
}

sc.close();
}
}

Example 1: Check if a number is posi7ve, nega7ve, or zero

public class PositiveNegativeZero {


public static void main(String[]
args) {
int number = -5;

if (number > 0) {
System.out.println("Positive
number");
} else if (number < 0) {
System.out.println("Negative
number");
} else {
System.out.println("Number is
zero");
}
}
}

Check if a person is eligible to vote

public class VotingEligibility {


public static void main(String[]
args) {
int age = 17;

if (age >= 18) {


System.out.println("You are
eligible to vote.");
} else {
System.out.println("You are
not eligible to vote.");
}
}
}

Example 1: Add two numbers

import java.util.Scanner;

public class AddTwoNumbers {


public static void main(String[]
args) {
Scanner sc = new
Scanner(System.in);

System.out.print("Enter first
number: ");
int a = sc.nextInt();

System.out.print("Enter second
number: ");
int b = sc.nextInt();

int sum = a + b;
System.out.println("Sum = " +
sum);

sc.close();
}
}

Example 2: Take student’s name and marks

import java.util.Scanner;

public class StudentDetails {


public static void main(String[]
args) {
Scanner sc = new
Scanner(System.in);

System.out.print("Enter your
name: ");
String name = sc.nextLine();
System.out.print("Enter your
marks: ");
int marks = sc.nextInt();

System.out.println(name + "
scored " + marks + " marks.");

sc.close();
}
}

Example 1: Days of the week

import java.util.Scanner;

public class DayOfWeek {


public static void main(String[]
args) {
Scanner sc = new
Scanner(System.in);

System.out.print("Enter a number
(1-7): ");
int day = sc.nextInt();

switch (day) {
case 1:
System.out.println("Monday"); break;
case 2:
System.out.println("Tuesday"); break;
case 3:
System.out.println("Wednesday"); break;
case 4:
System.out.println("Thursday"); break;
case 5:
System.out.println("Friday"); break;
case 6:
System.out.println("Saturday"); break;
case 7:
System.out.println("Sunday"); break;
default:
System.out.println("Invalid day
number.");
}

sc.close();
}
}

Example 2: Grade based on marks

import java.util.Scanner;

public class GradeCalculator {


public static void main(String[]
args) {
Scanner sc = new
Scanner(System.in);

System.out.print("Enter your
marks (0-100): ");
int marks = sc.nextInt();

switch (marks / 10) {


case 10:
case 9:
System.out.println("Grade: A"); break;
case 8:
System.out.println("Grade: B"); break;
case 7:
System.out.println("Grade: C"); break;
case 6:
System.out.println("Grade: D"); break;
default:
System.out.println("Grade: F");
}

sc.close();
}
}

You might also like