Write a program to check whether the given two string are equal or not ?
import java.util.Scanner;
public class StringEqualityCheck {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// Input two strings
System.out.print("Enter the first string: ");
String str1 = scanner.nextLine();
System.out.print("Enter the second string: ");
String str2 = scanner.nextLine();
// Using equals() method
boolean areEqualUsingEquals = checkEqualityUsingEquals(str1, str2);
// Using == operator
boolean areEqualUsingOperator = checkEqualityUsingOperator(str1, str2);
// Display the results
System.out.println("Using equals() method: The strings are " + (areEqualUsingEquals ? "equal" : "not equal"));
System.out.println("Using == operator: The strings are " + (areEqualUsingOperator ? "equal" : "not equal"));
// Function to check equality using equals() method
private static boolean checkEqualityUsingEquals(String str1, String str2) {
return str1.equals(str2);
// Function to check equality using == operator
private static boolean checkEqualityUsingOperator(String str1, String str2) {
return str1 == str2;
}
Output:-
Enter the first string: Hello
Enter the second string: Hello
Using equals() method: The strings are equal
Using == operator: The strings are equal
Calculate the sum of digit in the given number ?
import java.util.Scanner;
public class SumOfDigits {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// Input an integer from the user
System.out.print("Enter an integer: ");
int number = scanner.nextInt();
// Calculate the sum of digits
int sum = calculateSumOfDigits(number);
// Display the result
System.out.println("Sum of digits: " + sum);
// Function to calculate the sum of digits
private static int calculateSumOfDigits(int num) {
int sum = 0;
// Ensure the number is non-negative
num = Math.abs(num);
// Iterate through each digit and add to the sum
while (num > 0) {
sum += num % 10; // Add the last digit to the sum
num /= 10; // Remove the last digit
return sum;
Output:-
Enter an integer: 12345
Sum of digits: 15
Write a program to create program in java to create group class and the attributes of class that is room no . room
type and room area and ac machine in this class the method are set data and display data
import java.util.Scanner;
public class Room {
private int roomNo;
private String roomType;
private double roomArea;
private boolean acMachine;
// Method to set data for the Room
public void setData(int roomNo, String roomType, double roomArea, boolean acMachine) {
this.roomNo = roomNo;
this.roomType = roomType;
this.roomArea = roomArea;
this.acMachine = acMachine;
// Method to display data for the Room
public void displayData() {
System.out.println("Room No: " + roomNo);
System.out.println("Room Type: " + roomType);
System.out.println("Room Area: " + roomArea + " square meters");
System.out.println("AC Machine: " + (acMachine ? "Yes" : "No"));
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// Create a Room object
Room myRoom = new Room();
// Input data for the Room
System.out.print("Enter Room No: ");
int roomNo = scanner.nextInt();
scanner.nextLine(); // Consume the newline character
System.out.print("Enter Room Type: ");
String roomType = scanner.nextLine();
System.out.print("Enter Room Area (in square meters): ");
double roomArea = scanner.nextDouble();
System.out.print("Does the room have an AC machine? (true/false): ");
boolean acMachine = scanner.nextBoolean();
// Set data for the Room
myRoom.setData(roomNo, roomType, roomArea, acMachine);
// Display data for the Room
System.out.println("\nRoom Details:");
myRoom.displayData();
// Close the scanner
scanner.close();
}
}
Output:-
Enter Room No: 101
Enter Room Type: Deluxe
Enter Room Area (in square meters): 25.5
Does the room have an AC machine? (true/false): true
Room Details:
Room No: 101
Room Type: Deluxe
Room Area: 25.5 square meters
AC Machine: Yes
Palindrome number in the given range .
import java.util.Scanner;
public class PalindromeNumbersInRange {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// Input range
System.out.print("Enter the starting number of the range: ");
int start = scanner.nextInt();
System.out.print("Enter the ending number of the range: ");
int end = scanner.nextInt();
// Display palindrome numbers in the given range
System.out.println("Palindrome numbers in the range [" + start + ", " + end + "]:");
displayPalindromesInRange(start, end);
// Close the scanner
scanner.close();
}
// Function to check if a number is palindrome
private static boolean isPalindrome(int num) {
int originalNum = num;
int reversedNum = 0;
while (num > 0) {
int digit = num % 10;
reversedNum = reversedNum * 10 + digit;
num /= 10;
return originalNum == reversedNum;
// Function to display palindrome numbers in a given range
private static void displayPalindromesInRange(int start, int end) {
for (int i = start; i <= end; i++) {
if (isPalindrome(i)) {
System.out.print(i + " ");
System.out.println(); // Move to the next line after displaying the palindromes
Output:-
Enter the starting number of the range: 100
Enter the ending number of the range: 200
Palindrome numbers in the range [100, 200]:
101 111 121 131 141 151 161 171 181 191
Prime number in the given range .
import java.util.Scanner;
public class PrimeNumbersInRange {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// Input range
System.out.print("Enter the starting number of the range: ");
int start = scanner.nextInt();
System.out.print("Enter the ending number of the range: ");
int end = scanner.nextInt();
// Display prime numbers in the given range
System.out.println("Prime numbers in the range [" + start + ", " + end + "]:");
displayPrimesInRange(start, end);
// Close the scanner
scanner.close();
// Function to check if a number is prime
private static boolean isPrime(int num) {
if (num <= 1) {
return false;
for (int i = 2; i <= Math.sqrt(num); i++) {
if (num % i == 0) {
return false;
return true;
// Function to display prime numbers in a given range
private static void displayPrimesInRange(int start, int end) {
for (int i = start; i <= end; i++) {
if (isPrime(i)) {
System.out.print(i + " ");
System.out.println(); // Move to the next line after displaying the primes
Output:-
Enter the starting number of the range: 10
Enter the ending number of the range: 50
Prime numbers in the range [10, 50]:
11 13 17 19 23 29 31 37 41 43 47
Write a program in java to find odd and even number in the given range.
import java.util.Scanner;
public class OddEvenNumbersInRange {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// Input range
System.out.print("Enter the starting number of the range: ");
int start = scanner.nextInt();
System.out.print("Enter the ending number of the range: ");
int end = scanner.nextInt();
// Display odd and even numbers in the given range
System.out.println("Odd numbers in the range [" + start + ", " + end + "]:");
displayOddNumbersInRange(start, end);
System.out.println("\nEven numbers in the range [" + start + ", " + end + "]:");
displayEvenNumbersInRange(start, end);
// Close the scanner
scanner.close();
// Function to check if a number is odd
private static boolean isOdd(int num) {
return num % 2 != 0;
// Function to display odd numbers in a given range
private static void displayOddNumbersInRange(int start, int end) {
for (int i = start; i <= end; i++) {
if (isOdd(i)) {
System.out.print(i + " ");
System.out.println(); // Move to the next line after displaying the odds
// Function to check if a number is even
private static boolean isEven(int num) {
return num % 2 == 0;
// Function to display even numbers in a given range
private static void displayEvenNumbersInRange(int start, int end) {
for (int i = start; i <= end; i++) {
if (isEven(i)) {
System.out.print(i + " ");
System.out.println(); // Move to the next line after displaying the evens
}
Output:-
Enter the starting number of the range: 5
Enter the ending number of the range: 15
Odd numbers in the range [5, 15]:
5 7 9 11 13 15
Even numbers in the range [5, 15]:
6 8 10 12 14