Day 7 - 21 August, 2024

Program 23: WAP to check if a given String is a Palindrome

import java.util.*; class P23 { public static void main(String[] args) { String str, reversed = ""; Scanner sc = new Scanner(System.in); System.out.print("Enter your sentence : "); str = sc.nextLine(); for (int i = str.length() - 1; i >= 0; --i) { reversed += str.charAt(i); } if(reversed.equalsIgnoreCase(str)) { System.out.print("Entered String is Palindrome."); } else { System.out.print("Entered String is not a Palindrome."); } } } Copy Code
Expected Output:
Input:
Enter your sentence: madam

Output:
Entered String is Palindrome.

Program 24: WAP to find the sum of numbers using user-defined methods

import java.util.*; class P24 { static int sum(int a, int b) { return (a + b); } public static void main(String[] args) { int a, b, r; Scanner sc = new Scanner(System.in); System.out.print("Enter the value of a : "); a = sc.nextInt(); System.out.print("Enter the value of b : "); b = sc.nextInt(); r = sum(a, b); System.out.print("Sum of a and b : " + r); } } Copy Code
Expected Output:
Input:
Enter the value of a: 10
Enter the value of b: 20

Output:
Sum of a and b: 30

Program 25: WAP to find the area of a rectangle using a method

import java.util.*; class P25 { public static void main(String[] args) { int x, y, a; Scanner sc = new Scanner(System.in); System.out.print("Enter the length of Rectangle : "); x = sc.nextInt(); System.out.print("Enter the breadth of Rectangle : "); y = sc.nextInt(); P25 p = new P25(); a = p.area(x, y); System.out.print("Here is the area of Rectangle : " + a); } public int area(int l, int b) { return (l * b); } } Copy Code
Expected Output:
Input:
Enter the length of Rectangle: 5
Enter the breadth of Rectangle: 10

Output:
Here is the area of Rectangle: 50