Computer Java Practical
Computer Java Practical
Input:
import java.util.Scanner;
System.out.println("The largest natural number among " + n + " natural numbers is: " + largest);
}
return largest;
}
}
Output:
Enter the value of n: 7
The largest natural number among 7 natural numbers is: 7
Input:
import java.util.Scanner;
if (number <= 1) {
isPrime = false;
} else {
for (int i = 2; i <= Math.sqrt(number); i++) {
if (number % i == 0) {
isPrime = false;
break;
}
}
}
if (isPrime) {
System.out.println(number + " is a prime number.");
} else {
System.out.println(number + " is not a prime number.");
}
}
}
Output:
Enter a number to check if it's prime: 17
17 is a prime number.
Input:
import java.util.Scanner;
do {
System.out.println("Menu:");
System.out.println("1. Display Fibonacci Series");
System.out.println("2. Exit");
System.out.print("Enter your choice: ");
choice = scanner.nextInt();
switch (choice) {
case 1:
System.out.print("Enter the number of terms: ");
int terms = scanner.nextInt();
displayFibonacciSeries(terms);
break;
case 2:
System.out.println("Exiting...");
break;
default:
System.out.println("Invalid choice. Please enter 1 or 2.");
}
} while (choice != 2);
}
Output:
Menu:
1. Display Fibonacci Series
2. Exit
Enter your choice: 1
Enter the number of terms in Fibonacci series: 10
Fibonacci Series:
0 1 1 2 3 5 8 13 21 34
Menu:
1. Display Fibonacci Series
2. Exit
Enter your choice: 2
Exiting program...
B. input:
import java.util.Scanner;
do {
System.out.println("Menu:");
System.out.println("1. Compute factorial of a number");
System.out.println("2. Exit");
System.out.print("Enter your choice: ");
choice = scanner.nextInt();
switch(choice) {
case 1:
System.out.print("Enter a number to compute factorial: ");
int number = scanner.nextInt();
long factorial = computeFactorial(number);
System.out.println("Factorial of " + number + " is: " + factorial);
break;
case 2:
System.out.println("Exiting program. Goodbye!");
break;
default:
System.out.println("Invalid choice. Please enter 1 or 2.");
}
} while(choice != 2);
scanner.close();
}
long factorial = 1;
for (int i = 2; i <= n; i++) {
factorial *= i;
}
return factorial;
}
}
}
Output:
Menu:
1. Compute factorial of a number
2. Exit
Enter your choice: 1
Enter a number to compute factorial: 5
Factorial of 5 is: 120
Menu:
1. Compute factorial of a number
2. Exit
Enter your choice: 2
Exiting program. Goodbye!
C. Input:
import java.util.Scanner;
while (true) {
System.out.println("Menu:");
System.out.println("1. Check if a number is odd or even");
System.out.println("2. Exit");
System.out.print("Enter your choice: ");
int choice = scanner.nextInt();
switch (choice) {
case 1:
System.out.print("Enter a number to check: ");
int number = scanner.nextInt();
if (number % 2 == 0) {
System.out.println(number + " is even.");
} else {
System.out.println(number + " is odd.");
}
break;
case 2:
System.out.println("Exiting program...");
return;
default:
System.out.println("Invalid choice! Please enter 1 or 2.");
}
}
}
}
Output:
Menu:
1. Check if a number is odd or even
2. Exit
Enter your choice: 1
Enter a number to check: 15
15 is odd.
Menu:
1. Check if a number is odd or even
2. Exit
Enter your choice: 2
Exiting program...
D. input:
import java.util.Scanner;
switch (choice) {
case 1:
System.out.print("Enter a string to check if it's a palindrome: ");
scanner.nextLine(); // Clear the input buffer
String input = scanner.nextLine();
if (isPalindrome(input)) {
System.out.println("The string '" + input + "' is a palindrome.");
} else {
System.out.println("The string '" + input + "' is not a palindrome.");
}
break;
case 2:
System.out.println("Exiting program...");
break;
default:
System.out.println("Invalid choice. Please enter 1 or 2.");
break;
}
} while (choice != 2);
}
Output:
Menu:
1. Check if a string is a palindrome
2. Exit
Enter your choice: 1
Enter a string to check if it's a palindrome: racecar
The string 'racecar' is a palindrome.
Menu:
1. Check if a string is a palindrome
2. Exit
Enter your choice: 1
Enter a string to check if it's a palindrome: hello
The string 'hello' is not a palindrome.
Menu:
1. Check if a string is a palindrome
2. Exit
Enter your choice: 2
Exiting program...
Q4.
WAP to print the sum and product of digits of an Integer and reverse the Integer.
Input:
import java.util.Scanner;
Output:
Enter an integer: 1234
Sum of digits: 10
Product of digits: 24
Reversed number: 4321
Q5. Write a program to create an array of 10 integers. Accept values from the user in that
array.Input another number from the user and find out how many numbers are equal to
thenumber passed, how many are greater and how many are less than the number passed.
Input:
import java.util.Scanner;
// Accept values from the user and store them in the array
System.out.println("Enter 10 integers:");
for (int i = 0; i < numbers.length; i++) {
System.out.print("Enter value " + (i + 1) + ": ");
numbers[i] = scanner.nextInt();
}
// Count how many numbers are equal, greater, and less than the input number
int equalCount = 0;
int greaterCount = 0;
int lessCount = 0;
for (int num : numbers) {
if (num == inputNumber) {
equalCount++;
} else if (num > inputNumber) {
greaterCount++;
} else {
lessCount++;
}
}
Output:
Enter 10 integers:
Enter value 1: 5
Enter value 2: 3
Enter value 3: 8
Enter value 4: 5
Enter value 5: 7
Enter value 6: 2
Enter value 7: 9
Enter value 8: 5
Enter value 9: 3
Enter value 10: 1
Enter another number: 5
Number of numbers equal to 5: 3
Number of numbers greater than 5: 4
Number of numbers less than 5: 3
Q6. Write a program that will prompt the user for a list of 5 prices. Compute the average of
the prices and find out all the prices that are higher than the calculated average.
Input:
import java.util.Scanner;
Q7. Write a program in java to input N numbers in an array and print out the Armstrong
numbers from the set.
Input:
import java.util.Scanner;
Output:
Enter the number of elements in the array: 5
Enter the elements of the array:
Enter element 1: 153
Enter element 2: 370
Enter element 3: 947
Enter element 4: 371
Enter element 5: 820
Armstrong numbers in the array:
153
370
371
Input:
import java.util.Scanner;
return result;
}
Output:
Enter the number of rows for both matrices: 2
Enter the number of columns for both matrices: 2
b. input:
import java.util.Scanner;
// Summation of matrices
int[][] sumMatrix = sumMatrices(matrix1, matrix2);
Output:
Result of matrix summation:
10 10 10
10 10 10
10 10 10
c. input:
import java.util.Scanner;
Output:
Enter the number of rows in the matrix: 3
Enter the number of columns in the matrix: 2
Enter the elements of the matrix:
Enter element at position (1, 1): 1
Enter element at position (1, 2): 2
Enter element at position (2, 1): 3
Enter element at position (2, 2): 4
Enter element at position (3, 1): 5
Enter element at position (3, 2): 6
d. input:
import java.util.Scanner;
System.out.println("\nDifference of matrices:");
printMatrix(difference);
System.out.println("\nProduct of matrices:");
printMatrix(product);
}
return sum;
}
return difference;
}
return product;
}
Q9. Write a java program that computes the area of a circle, rectangle and a Cylinder
usingfunction overloading.
Input:
public class AreaCalculator {
public static void main(String[] args) {
// Calculate area of a circle with radius 5
double circleArea = calculateArea(5.0);
System.out.println("Area of the circle with radius 5: " + circleArea);
Q10. Write a Java for the implementation of Multiple inheritance using interfaces to
calculate the area of a rectangle and triangle.
Input:
interface Rectangle {
double calculateArea(double length, double width);
}
interface Triangle {
double calculateArea(double base, double height);
}
@Override
public double calculateArea(double base, double height) {
return 0.5 * base * height;
}
}
Q11. Write a java program to create a frame window in an Applet. Display your name,
address and qualification in the frame window.
Input:
import java.applet.Applet;
import java.awt.*;
Q12. Write a java program to draw a line between two coordinates in a window.
Input:
import javax.swing.*;
import java.awt.*;
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawLine(x1, y1, x2, y2);
}
frame.setVisible(true);
}
}
Output:
A window titled "Line Drawing" will appear, displaying a line drawn between the coordinates (50, 50) and (200, 150).
Q13. Write a java program to display the following graphics in an applet window.a.
Rectangles
b. Circles
c. Ellipses
d. Arcs
e. Polygons
a. input:
import java.applet.Applet;
import java.awt.*;
c. input:
import java.awt.*;
import java.applet.*;
d. input:
import java.awt.*;
import java.applet.*;
e. input:
import java.awt.*;
import java.applet.*;
g.setColor(Color.blue);
g.drawPolygon(xPoints, yPoints, numPoints); // Polygon 1
g.setColor(Color.red);
g.drawPolygon(xPoints2, yPoints2, numPoints2); // Polygon 2
}
}
Output:
two polygons displayed in an applet window, one in blue and one in red, as described in the program. Each polygon is drawn with different
coordinates.
Q14. Write a program that reads two integer numbers for the variables a and b. If any other
character except number (0-9) is entered then the error is caught by Number Format
Exception object. After that ex. Get Message() prints the information about the error
occurring causes.
Input:
import java.util.Scanner;
try {
System.out.print("Enter the value of a: ");
int a = Integer.parseInt(scanner.nextLine());