1.
Write a program to accept a number and print reverse of
the number and sum of digits of that number in java
programming.
import java.util.Scanner;
public class NumberOperations {
public static void main(String[] args) {
// Create Scanner object for input
Scanner scanner = new Scanner(System.in);
// Ask the user to enter a number
System.out.print("Enter a number: ");
int num = scanner.nextInt();
int originalNum = num; // Store the original number
int reverse = 0;
int sum = 0;
// Make num positive if it's negative for digit processing
int temp = Math.abs(num);
while (temp > 0) {
int digit = temp % 10;
reverse = reverse * 10 + digit;
sum += digit;
temp /= 10;
}
// Print results
System.out.println("Reverse of " + originalNum + " is " +
reverse);
System.out.println("Sum of digits of " + originalNum + "
is " + sum);
// Close scanner
scanner.close();
}
}
2. Write a program to accept array of 10 no. and remove the
occurrence of the given no from the array in java
programming.
import java.util.Scanner;
public class RemoveNumberFromArray {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int[] originalArray = new int[10];
int[] newArray = new int[10]; // Max same size
int count = 0;
// Input 10 numbers into the array
System.out.println("Enter 10 numbers:");
for (int i = 0; i < 10; i++) {
originalArray[i] = scanner.nextInt();
}
// Input the number to be removed
System.out.print("Enter number to remove: ");
int numToRemove = scanner.nextInt();
// Copy elements except the one to remove
for (int i = 0; i < 10; i++) {
if (originalArray[i] != numToRemove) {
newArray[count] = originalArray[i];
count++;
}
}
// Display the result
if (count == 0) {
System.out.println("All elements were removed. No
elements to display.");
} else {
System.out.println("Array after removing " +
numToRemove + ":");
for (int i = 0; i < count; i++) {
System.out.print(newArray[i] + " ");
}
System.out.println();
}
scanner.close();
}
}
3. Write a program to accept a number on the command line
and check if it's an Armstrong number or not
public class ArmstrongCheck {
public static void main(String[] args) {
// Check if argument is provided
if (args.length != 1) {
System.out.println("Please provide a number as a
command line argument.");
return;
}
int num = Integer.parseInt(args[0]);
int originalNum = num;
int sum = 0;
int digits = String.valueOf(num).length();
while (num > 0) {
int digit = num % 10;
sum += Math.pow(digit, digits);
num /= 10;
}
if (sum == originalNum) {
System.out.println(originalNum + " is an Armstrong
number.");
} else {
System.out.println(originalNum + " is not an
Armstrong number.");
}
}
}
4.Write a program to create an applet to accept a string in a
textbox and change all the letters in d given string to
Uppercase at the click of 'show' button in java programming.
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
/*
<applet code="UppercaseApplet" width=400 height=200>
</applet>
*/
public class UppercaseApplet extends Applet implements
ActionListener {
TextField inputField;
Button showButton;
String result = "";
public void init() {
// Create components
inputField = new TextField(20);
showButton = new Button("Show");
// Add components
add(new Label("Enter String:"));
add(inputField);
add(showButton);
// Register event
showButton.addActionListener(this);
}
public void actionPerformed(ActionEvent e) {
String text = inputField.getText();
result = text.toUpperCase();
repaint(); // Call paint() method
}
public void paint(Graphics g) {
g.drawString("Uppercase String: " + result, 20, 100);
}
}
[Textbox: Enter String here] [Show Button]
User enters: "hello world"
Clicks Show
Output: Uppercase String: HELLO WORLD
5. Write a program to accept a no. and print factorial of that
no.
import java.util.Scanner;
public class FactorialCalculator {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// Accept number from user
System.out.print("Enter a number: ");
int num = scanner.nextInt();
long factorial = 1;
// Check for valid input
if (num < 0) {
System.out.println("Factorial is not defined for
negative numbers.");
} else {
// Calculate factorial
for (int i = 1; i <= num; i++) {
factorial *= i;
}
// Display result
System.out.println("Factorial of " + num + " is " +
factorial);
}
scanner.close();
}
}
6. Write a program to print the sum of all the odd number
between 1-100.
public class SumOfOddNumbers {
public static void main(String[] args) {
int sum = 0;
// Loop through odd numbers from 1 to 100
for (int i = 1; i <= 100; i += 2) {
sum += i;
}
// Display the result
System.out.println("Sum of all odd numbers between 1
and 100 is: " + sum);
}
}
7. Write a java program to display alternate character from a
given string.
import java.util.Scanner;
public class AlternateCharacters {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// Input string from user
System.out.print("Enter a string: ");
String input = scanner.nextLine();
System.out.print("Alternate characters: ");
for (int i = 0; i < input.length(); i += 2) {
System.out.print(input.charAt(i));
}
System.out.println(); // for a clean new line
scanner.close();
}
}
8.Write a java program to accept a number from a user, if it is
zero then throw user defined Exception Number is Zero'". If it
is non-numeric then generate an error ”Number is Invalid”
otherwise check whether it is palindrome or not.
import java.util.Scanner;
// User-defined exception
class NumberIsZeroException extends Exception {
public NumberIsZeroException(String message) {
super(message);
}
}
public class PalindromeCheck {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a number: ");
try {
String input = scanner.nextLine();
// Try parsing input to integer
int number = Integer.parseInt(input);
// Check for zero
if (number == 0) {
throw new NumberIsZeroException("Number is
Zero");
}
// Check for palindrome
int original = number;
int reverse = 0;
while (number > 0) {
int digit = number % 10;
reverse = reverse * 10 + digit;
number /= 10;
}
// Display result
if (original == reverse) {
System.out.println(original + " is a Palindrome.");
} else {
System.out.println(original + " is not a Palindrome.");
}
} catch (NumberIsZeroException e) {
System.out.println(e.getMessage());
} catch (NumberFormatException e) {
System.out.println("Number is Invalid");
}
scanner.close();
}
}
9.Write a Java program Create abstract class shape. Drive
three classes sphere. Cone and cylinder from it.
Calculate Area and volume of all(use method overriding)
import java.util.Scanner;
// Abstract class Shape
abstract class Shape {
abstract void calculateArea();
abstract void calculateVolume();
}
// Sphere class
class Sphere extends Shape {
double radius;
Sphere(double radius) {
this.radius = radius;
}
@Override
void calculateArea() {
double area = 4 * Math.PI * radius * radius;
System.out.println("Sphere Surface Area: " + area);
}
@Override
void calculateVolume() {
double volume = (4.0 / 3.0) * Math.PI * radius * radius
* radius;
System.out.println("Sphere Volume: " + volume);
}
}
// Cone class
class Cone extends Shape {
double radius, height;
Cone(double radius, double height) {
this.radius = radius;
this.height = height;
}
@Override
void calculateArea() {
double slantHeight = Math.sqrt(radius * radius +
height * height);
double area = Math.PI * radius * (radius +
slantHeight);
System.out.println("Cone Surface Area: " + area);
}
@Override
void calculateVolume() {
double volume = (1.0 / 3.0) * Math.PI * radius * radius
* height;
System.out.println("Cone Volume: " + volume);
}
}
// Cylinder class
class Cylinder extends Shape {
double radius, height;
Cylinder(double radius, double height) {
this.radius = radius;
this.height = height;
}
@Override
void calculateArea() {
double area = 2 * Math.PI * radius * (radius + height);
System.out.println("Cylinder Surface Area: " + area);
}
@Override
void calculateVolume() {
double volume = Math.PI * radius * radius * height;
System.out.println("Cylinder Volume: " + volume);
}
}
// Main class
public class ShapeTest {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// Sphere
System.out.print("Enter radius of Sphere: ");
double r1 = scanner.nextDouble();
Shape sphere = new Sphere(r1);
sphere.calculateArea();
sphere.calculateVolume();
// Cone
System.out.print("\nEnter radius and height of Cone:
");
double r2 = scanner.nextDouble();
double h2 = scanner.nextDouble();
Shape cone = new Cone(r2, h2);
cone.calculateArea();
cone.calculateVolume();
// Cylinder
System.out.print("\nEnter radius and height of
Cylinder: ");
double r3 = scanner.nextDouble();
double h3 = scanner.nextDouble();
Shape cylinder = new Cylinder(r3, h3);
cylinder.calculateArea();
cylinder.calculateVolume();
scanner.close();
}
}
10. Write a program in Java to display following pattern
55555
4444
333
22
1
public class NumberPattern {
public static void main(String[] args) {
// Loop from 5 to 1
for (int i = 5; i >= 1; i--) {
// Print the number i, i times
for (int j = 1; j <= i; j++) {
System.out.print(i);
}
// Move to the next line
System.out.println();
}
}
}
11 Write Java program to convert the temperature in
Centigrade to Fahrenheit.
import java.util.Scanner;
public class CelsiusToFahrenheit {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// Input temperature in Celsius
System.out.print("Enter temperature in Celsius: ");
double celsius = scanner.nextDouble();
// Convert to Fahrenheit
double fahrenheit = (celsius * 9 / 5) + 32;
// Display result
System.out.println("Temperature in Fahrenheit: " +
fahrenheit);
scanner.close();
}
}
12.Write a Java program to create an interface Shape with
the get Area() method. Create three classes Rectangle,
Circle, and Triangle that implement the Shape interface.
Implement the get Area() method for each of the three
classes.
import java.util.Scanner;
// Interface Shape
interface Shape {
double getArea();
}
// Rectangle class
class Rectangle implements Shape {
double length, width;
Rectangle(double length, double width) {
this.length = length;
this.width = width;
}
public double getArea() {
return length * width;
}
}
// Circle class
class Circle implements Shape {
double radius;
Circle(double radius) {
this.radius = radius;
}
public double getArea() {
return Math.PI * radius * radius;
}
}
// Triangle class
class Triangle implements Shape {
double base, height;
Triangle(double base, double height) {
this.base = base;
this.height = height;
}
public double getArea() {
return 0.5 * base * height;
}
}
// Main class
public class ShapeInterfaceDemo {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// Rectangle
System.out.print("Enter length and width of Rectangle:
");
double length = scanner.nextDouble();
double width = scanner.nextDouble();
Shape rectangle = new Rectangle(length, width);
System.out.println("Area of Rectangle: " +
rectangle.getArea());
// Circle
System.out.print("\nEnter radius of Circle: ");
double radius = scanner.nextDouble();
Shape circle = new Circle(radius);
System.out.println("Area of Circle: " + circle.getArea());
// Triangle
System.out.print("\nEnter base and height of Triangle:
");
double base = scanner.nextDouble();
double height = scanner.nextDouble();
Shape triangle = new Triangle(base, height);
System.out.println("Area of Triangle: " +
triangle.getArea());
scanner.close();
}
}