NAME : SAMULA UDAYA SINDHU
Simple Calculator Application: Develop a basic calculator application that performs
addition, subtraction, multiplication, and division.
PROGRAM
import [Link];
public class Main
{
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
[Link]("Simple Calculator");
[Link]("✮-✮-✮-✮-✮-✮-✮-✮-✮");
[Link]("Enter the first number: ");
double num1 = [Link]();
[Link]("Enter the second number: ");
double num2 = [Link]();
[Link]("Choose an operation:");
[Link]("1. Addition (+)");
[Link]("2. Subtraction (-)");
[Link]("3. Multiplication (*)");
[Link]("4. Division (/)");
[Link]("Enter your choice (1-4): ");
int choice = [Link]();
double result = 0;
boolean valid = true;
switch (choice) {
case 1:
result = num1 + num2;
break;
case 2:
result = num1 - num2;
break;
case 3:
result = num1 * num2;
break;
case 4:
if (num2 != 0) {
result = num1 / num2;
} else {
[Link]("Error: Division by zero is not allowed.");
valid = false;
}
break;
default:
[Link]("Error: Invalid choice. Please enter a number between 1 and
4.");
valid = false;
break;
}
if (valid) {
[Link]("The result is: " + result);
}
[Link]();
}
}
OUTPUT
Palindrome Checker: Write a program to check if a given string is a palindrome (reads
the same forwards and backwards).
PROGRAM
import [Link];
public class Main
{
public static void main(String[] args)
{
Scanner scanner = new Scanner([Link]);
[Link]("Palindrome Checker");
[Link]("✮-✮-✮-✮-✮-✮-✮-✮-✮");
[Link]("Enter a string: ");
String input = [Link]();
String cleanedInput = [Link]("[^a-zA-Z0-9]", "").toLowerCase();
boolean isPalindrome = isPalindrome(cleanedInput);
if (isPalindrome) {
[Link]("The string is a palindrome.");
} else {
[Link]("The string is not a palindrome.");
}
[Link]();
}
private static boolean isPalindrome(String str) {
int left = 0;
int right = [Link]() - 1;
while (left < right) {
if ([Link](left) != [Link](right)) {
return false;
}
left++;
right--;
}
return true;
}
}
OUTPUT
Number Guessing Game: Create a simple number guessing game where the program
generates a random number and the user tries to guess it.
PROGRAM
import [Link];
import [Link];
public class Main
{
public static void main(String[] args) {
Random random = new Random();
Scanner scanner = new Scanner([Link]);
int num = [Link](100) + 1;
int guess = -1;
int count = 0;
[Link]("Number Guessing Game");
[Link]("✮-✮-✮-✮-✮-✮-✮-✮-✮");
[Link]("Guess the number (between 1 and 100): ");
while (guess != num) {
guess = [Link]();
count++;
if (guess > num) {
[Link]("Lower than this");
} else if (guess < num) {
[Link]("Greater than this");
} else {
[Link]("Finally......! You got it");
[Link]("You guessed it in " + count + " attempts.");
}
if (guess != num) {
[Link]("Guess again: ");
}
}
[Link]();
}
}
OUTPUT