Java
Java
Code:-
Input:-
1234
Output:-
2.2 WAP to count the number of characters in a given string, to reverse the string
and check whether it is palindrome or not?
Code:-
import java.util.Scanner;
public class exp2 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a string: ");
String input = sc.nextLine();
if (input.equals(reversed)) {
System.out.println("It's a palindrome.");
} else {
System.out.println("Not a palindrome.");
}
}
}
Input:-
NAMAN
Output:-
Total characters: 5
Reversed: NAMAN
It's a palindrome.
Code:-
import java.util.Scanner;
Input:-
1234
Output:-
Smallest: 1
Largest: 4
2.4 Java Program to Find the Sum and Product of Elements in a Row/Column of a
Matrix
Code:-
import java.util.Scanner;
sum += matrix[row][j];
product *= matrix[row][j];
}
Input:-
123456789
Output:-
Row Sum: 15
2.5 WAP to create class Number with only one private instance variable as a double
primitive type, include the following methods isZero(), isPositive(), isNegative( ),
isOdd( ), isEven( ), isPrime(), isAmstrong() in this class and all above methods
should return boolean primitive type like for isPositive() should return “Positive =
True”
Code:-
import java.util.Scanner;
public class Number {
private double num;
Console:-
Enter element:
isZero = false
isPositive = true
isNegative = false
isEven = false
isOdd = true
isPrime = true
isAmstrong = true
2.6 WAP to insert 3 numbers from the keyboard and find a greater number among 3
numbers.
Code:-
import java.util.Scanner;
public class Greatest {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter elements:");
int n = sc.nextInt();
int n1 = sc.nextInt();
int n2 = sc.nextInt();
if(n > n1) {
if(n > n2) {
System.out.println(n + " is greatest");
}
else {
System.out.println(n2 + " is greatest");
}
}
else{
if(n1 > n2) {
System.out.println(n1 + " is greatest");
}
else {
System.out.println(n2 + " is greatest");
}
}
}
}
Console:-
Enter elements:
3 is greatest
Code:-
ThisKeyword(int x) {
this.x = x;
}
void display() {
System.out.println("Value: " + this.x);
}
Console:-
Value: 10
static {
count = 100;
StaticDemo.displayCount();
Console:-
Count = 100
2.9 WAP to create a class named Shape and create three subclasses Circle,
Triangle and Square, each class has two-member functions named draw () and
erase (). Implement this concept using polymorphism.
Code:-
class Shape {
void draw() { System.out.println("Drawing shape"); }
void erase() { System.out.println("Erasing shape"); }
}
Console:-
Drawing circle
Erasing circle
Drawing triangle
Erasing triangle
Drawing square
Erasing square
Code:-
class Employee {
double salary;
double calculateSalary() {
return salary;
double calculateSalary() {
double calculateSalary() {
Console:-
2.11 WAP to count the total number of odd numbers between 1-100, and display the
sum of them.
Code:-
Cosole:-