You are on page 1of 9

O B J ECT - O R I E N T E D P R O G R AMMING

INPUT FROM USER

JAVA
EXERCISE 1: SIMPLE INPUT
Write a Java program that uses the Scanner class to take user input for their name
and then prints a greeting using their name.

import java.util.Scanner;

public class Greeting {


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

System.out.print("Enter your name: ");


String name = scanner.nextLine();

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


scanner.close();
}
}
Exercise 2: Calculate the Sum
Write a Java program that takes two numbers as input and calculates their sum.

import java.util.Scanner;

public class SumCalculator {


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

System.out.print("Enter the first number: ");


int num1 = scanner.nextInt();

System.out.print("Enter the second number: ");


int num2 = scanner.nextInt();

int sum = num1 + num2;


System.out.println("Sum: " + sum);
scanner.close();
}
}
Exercise 3: Temperature Converter
Write a program that converts a temperature in Celsius to Fahrenheit. Use the
formula: F = (C × 9/5) + 32.

import java.util.Scanner;

public class TemperatureConverter {


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

System.out.print("Enter temperature in Celsius: ");


double celsius = scanner.nextDouble();

double fahrenheit = (celsius * 9/5) + 32;


System.out.println("Temperature in Fahrenheit: " + fahrenheit);
scanner.close();
}
}
O B J ECT - O R I E N T E D P R O G R AMMING

IF AND ELSE CONDITION

JAVA
Exercise 4: Even or Odd
Write a Java program that takes an integer as input and prints whether it is even or odd.

import java.util.Scanner;

public class EvenOdd {


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

System.out.print("Enter an integer: ");


int number = scanner.nextInt();

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

scanner.close();
}
}
Exercise 5: Positive, Negative, or Zero
Write a Java program that takes an integer as input and prints whether it is positive, negative, or zero.

import java.util.Scanner;

public class PositiveNegativeZero {


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

System.out.print("Enter an integer: ");


int number = scanner.nextInt();

if (number > 0) {
System.out.println(number + " is positive.");
} else if (number < 0) {
System.out.println(number + " is negative.");
} else {
System.out.println(number + " is zero.");
}

scanner.close();
}
}
Exercise 6: Largest of Three Numbers (1)
Write a Java program that takes three integers as input and prints the largest among them.

import java.util.Scanner;

public class LargestOfThree {


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

System.out.print("Enter the first integer: ");


int num1 = scanner.nextInt();

System.out.print("Enter the second integer: ");


int num2 = scanner.nextInt();

System.out.print("Enter the third integer: ");


int num3 = scanner.nextInt();
Exercise 6: Largest of Three Numbers (2)
Write a Java program that takes three integers as input and prints the largest among them.

if (num1 >= num2 && num1 >= num3) {


System.out.println(num1 + " is the largest.");

} else if (num2 >= num1 && num2 >= num3) {


System.out.println(num2 + " is the largest.");

} else {
System.out.println(num3 + " is the largest.");
}

scanner.close();
}
}

You might also like