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

Task 1

good

Uploaded by

sindhusamula
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views

Task 1

good

Uploaded by

sindhusamula
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

NAME : SAMULA UDAYA SINDHU

Simple Calculator Application: Develop a basic calculator application that performs


addition, subtraction, multiplication, and division.
PROGRAM
import java.util.Scanner;
public class Main
{
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Simple Calculator");
System.out.println("✮-✮-✮-✮-✮-✮-✮-✮-✮");
System.out.print("Enter the first number: ");
double num1 = scanner.nextDouble();
System.out.print("Enter the second number: ");
double num2 = scanner.nextDouble();
System.out.println("Choose an operation:");
System.out.println("1. Addition (+)");
System.out.println("2. Subtraction (-)");
System.out.println("3. Multiplication (*)");
System.out.println("4. Division (/)");
System.out.print("Enter your choice (1-4): ");
int choice = scanner.nextInt();
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 {
System.out.println("Error: Division by zero is not allowed.");
valid = false;
}
break;
default:
System.out.println("Error: Invalid choice. Please enter a number between 1 and
4.");
valid = false;
break;
}
if (valid) {
System.out.println("The result is: " + result);
}
scanner.close();
}
}
OUTPUT

Palindrome Checker: Write a program to check if a given string is a palindrome (reads


the same forwards and backwards).
PROGRAM
import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
Scanner scanner = new Scanner(System.in);
System.out.println("Palindrome Checker");
System.out.println("✮-✮-✮-✮-✮-✮-✮-✮-✮");
System.out.print("Enter a string: ");
String input = scanner.nextLine();
String cleanedInput = input.replaceAll("[^a-zA-Z0-9]", "").toLowerCase();
boolean isPalindrome = isPalindrome(cleanedInput);
if (isPalindrome) {
System.out.println("The string is a palindrome.");
} else {
System.out.println("The string is not a palindrome.");
}
scanner.close();
}
private static boolean isPalindrome(String str) {
int left = 0;
int right = str.length() - 1;
while (left < right) {
if (str.charAt(left) != str.charAt(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 java.util.Random;
import java.util.Scanner;
public class Main
{
public static void main(String[] args) {
Random random = new Random();
Scanner scanner = new Scanner(System.in);
int num = random.nextInt(100) + 1;
int guess = -1;
int count = 0;
System.out.println("Number Guessing Game");
System.out.println("✮-✮-✮-✮-✮-✮-✮-✮-✮");
System.out.print("Guess the number (between 1 and 100): ");
while (guess != num) {
guess = scanner.nextInt();
count++;
if (guess > num) {
System.out.println("Lower than this");
} else if (guess < num) {
System.out.println("Greater than this");
} else {
System.out.println("Finally......! You got it");
System.out.println("You guessed it in " + count + " attempts.");
}
if (guess != num) {
System.out.print("Guess again: ");
}
}
scanner.close();
}
}
OUTPUT

You might also like