0% found this document useful (0 votes)
16 views1 page

Core Java Exercises 1 To 2

The document contains two Java programs: the first is a simple 'Hello World' program that prints a greeting to the console. The second program is a basic calculator that allows users to input two numbers and choose an arithmetic operation to perform, displaying the result. It includes error handling for invalid operations and division by zero.

Uploaded by

hema22050.ec
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)
16 views1 page

Core Java Exercises 1 To 2

The document contains two Java programs: the first is a simple 'Hello World' program that prints a greeting to the console. The second program is a basic calculator that allows users to input two numbers and choose an arithmetic operation to perform, displaying the result. It includes error handling for invalid operations and division by zero.

Uploaded by

hema22050.ec
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/ 1

1.

Hello World Program


public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}

2. Simple Calculator
import java.util.Scanner;

public class Calculator {


public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter first number: ");
double a = sc.nextDouble();
System.out.print("Enter second number: ");
double b = sc.nextDouble();
System.out.print("Choose operation (+, -, *, /): ");
char op = sc.next().charAt(0);
double result = 0;
switch (op) {
case '+': result = a + b; break;
case '-': result = a - b; break;
case '*': result = a * b; break;
case '/': result = b != 0 ? a / b : Double.NaN; break;
default: System.out.println("Invalid operation."); return;
}
System.out.println("Result: " + result);
}
}

You might also like