0% found this document useful (0 votes)
10 views

Java Lab Manual

The document contains 8 programs in Java to perform operations like finding the maximum of 3 numbers, checking if a number is prime, sorting an array, and adding two matrices. The programs take user input, perform calculations, and output results.

Uploaded by

Mohit Sharma
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

Java Lab Manual

The document contains 8 programs in Java to perform operations like finding the maximum of 3 numbers, checking if a number is prime, sorting an array, and adding two matrices. The programs take user input, perform calculations, and output results.

Uploaded by

Mohit Sharma
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

RSR RUNGTA COLLEGE OF ENGINEERING &

TECHNOLOGY

Object Oriented Programming


(with JAVA)
Laboratory
Subject code: BO22422(022)

Guided by Submitted by
Mr. Prashant Tamrakar Shristi Kumari
1. Write a program in JAVA to take 3 integers from user and find maximum
between them?

/* Author : Shristi Kumari


Date : 01/04/2024
*/
Import java.lang.*;
class Maximum{
public static void main(String[] args){
int a,b,c,d;
a=Integer.parseInt(args[0]);
b=Integer.parseInt(args[1]);
c=Integer.parseInt(args[2]);
if (a>=b && a>=c){
System.out.println("A is the greatest number
");
}
else if(b>=a && b>=c){
System.out.println("B is the greatest number
");
}
else{
System.out.println("C is the greatest number
");
}
}
}
2. Write a program in JAVA to take an integer from user and find it’s factorial?

/* Author : Shristi Kumari


Date : 01/04/2024
*/
class Factorial
{
public static void main(String args[])
{
int a,fact=1,num;

num = Integer.parseInt(args[0]);
for(a=1;a<=num;a++)
{
fact=fact*a;
}
System.out.println("Factorial of " + num + " is " + fact);
}
}
3. Write a program in JAVA to take an integer from user and check its
palindrome or not?
/* Author : Shristi Kumari
Date : 02/04/2024
*/
class Palindrome
{
public static void main(String args[])
{
int r,sum=0,temp;
int n;

n=Integer.parseInt(args[0]);

temp=n;
while(n>0)
{
r=n%10;
sum=(sum*10)+r;
n=n/10;
}
if(temp==sum)
System.out.println("palindrome number ");
else
System.out.println("not palindrome");
}
}
4. Write a program in JAVA to take an integer from user and check its
Armstrong or not?

/* Author : Shristi Kumari


Date : 02/04/2024
*/
public class Armstrong {
public static void main(String[] args) {
int n = Integer.parseInt(args[0]);
int r = isArmstrong(n);
if (r == 1) {
System.out.println(n + " is an Armstrong number.");
} else {
System.out.println(n + " is not an Armstrong number.");
}
}

public static int isArmstrong(int number) {


int o = number;
int a = 0;
int d = String.valueOf(number).length();

while (number != 0) {
int digit = number % 10;
a += Math.pow(digit, d);
number /= 10;
}

if (a == o) {
return 1;
} else {
return 0;
}
}
}
5. Write a program in JAVA to take an integer from user and check its Prime
no. or not?

/* Author : Shristi Kumari


Date : 03/04/2024
*/
class Prime{
public static void main(String args[]){
int i,m,flag=0;
int n;
m=n/2;
n=Integer.parseInt(args[0]);
if(n==0||n==1){
System.out.println(n+" is not a prime number");
}
else{
for(i=2;i<=m;i++)
{
if(n%i==0)
{
System.out.println(n+" is not a prime number");
flag=1;
break;
}
}
if(flag==0)
System.out.println(n+" is a prime number");
else
System.out.println(n+" is not a prime number");
}
}
}
6. Write a program in JAVA to take five element array and sort that array in
ascending order?

/* Author : Shristi Kumari


Date : 03/04/2024
*/
public class Sorting {
public static void main(String[] args) {

int[] arr = new int[5];

for (int i = 0; i < 5; i++) {


arr[i] = Integer.parseInt(args[i]);
}

for (int i = 0; i < 5 - 1; i++) {


for (int j = 0; j < 5 - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}

System.out.println("Sorted array in ascending order:");


for (int i = 0; i < 5; i++) {
System.out.print(arr[i] + " ");
}
}
}
7. Write a program in JAVA to take five integer arrays from user and check the
given element is present or not?
/* Author : Shristi Kumari
Date : 04/04/2024
*/
import java.util.Scanner;

public class ElementSearch {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);

int[][] arrays = new int[5][];


for (int i = 0; i < 5; i++) {
System.out.print("Enter the size of array " + (i + 1) + ": ");
int size = scanner.nextInt();
arrays[i] = new int[size];
System.out.println("Enter elements for array " + (i + 1) + ":");
for (int j = 0; j < size; j++) {
arrays[i][j] = scanner.nextInt();
}
}
System.out.print("Enter the element to search: ");
int elementToSearch = scanner.nextInt();
for (int i = 0; i < 5; i++) {
if (isElementPresent(arrays[i], elementToSearch)) {
System.out.println("Element " + elementToSearch + " is present in array " + (i + 1));
} else {
System.out.println("Element " + elementToSearch + " is not present in array " + (i + 1));
}
}
scanner.close();
}
public static boolean isElementPresent(int[] array, int element) {
for (int num : array) {
if (num == element) {
return true;
}
}
return false;
}
}
8. Write a program in JAVA to take 5 elements array and find sum of these
elements?

/* Author : Shristi Kumari


Date : 04/04/2024
*/
class ArraySum {
public static void main(String[] args) {

int[] arr = new int[5];

for (int i = 0; i < 5; i++) {


arr[i] = Integer.parseInt(args[i]);
}

int sum = 0;
for (int i = 0; i < 5; i++) {
sum += arr[i];
}

System.out.println("Sum of the elements in the array: " + sum);


}
}
9. Write a program in JAVA to take 2 matrix a & b and find the sum of 2
matrix a & b?

/* Author : Shristi Kumari public static int[][] sumOfMatrices(int[][] matrixA,


Date : 05/04/2024 int[][] matrixB) {
*/ int rows = matrixA.length;
import java.util.Scanner; int cols = matrixA[0].length;
int[][] sumMatrix = new int[rows][cols];
public class MatrixSum {
public static void main(String[] args) { for (int i = 0; i < rows; i++) {
Scanner scanner = new Scanner(System.in); for (int j = 0; j < cols; j++) {
sumMatrix[i][j] = matrixA[i][j] + matrixB[i][j];
System.out.println("Enter elements for matrix A (2x2):"); }
int[][] matrixA = inputMatrix(scanner, 2, 2); }
return sumMatrix;
System.out.println("Enter elements for matrix B (2x2):"); }
int[][] matrixB = inputMatrix(scanner, 2, 2);
public static void displayMatrix(int[][] matrix) {
int[][] sumMatrix = sumOfMatrices(matrixA, matrixB); int rows = matrix.length;
int cols = matrix[0].length;
System.out.println("Sum of matrices A and B:");
displayMatrix(sumMatrix); for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
scanner.close(); System.out.print(matrix[i][j] + " ");
} }
public static int[][] inputMatrix(Scanner scanner, int rows, int cols) { System.out.println();
int[][] matrix = new int[rows][cols]; }
for (int i = 0; i < rows; i++) { }
for (int j = 0; j < cols; j++) { }
matrix[i][j] = scanner.nextInt();
}
}
return matrix;
}
10.Write a program in JAVA to take 3X3 matrix and find sum of rows element in
given matrix?

/* Author : Shristi Kumari


Date : 03/04/2024
*/
import java.util.Scanner;

public class RowSum {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);

int[][] matrix = new int[3][3];


System.out.println("Enter elements for the 3x3 matrix:");
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
matrix[i][j] = scanner.nextInt();
}
}
int[] rowSums = new int[3];
for (int i = 0; i < 3; i++) {
int sum = 0;
for (int j = 0; j < 3; j++) {
sum += matrix[i][j];
}
rowSums[i] = sum;
}

System.out.println("Sum of elements in each row:");


for (int i = 0; i < 3; i++) {
System.out.println("Row " + (i + 1) + ": " + rowSums[i]);
}

scanner.close();
}
}

You might also like