0% found this document useful (0 votes)
2 views5 pages

Java_LAB_RECORD

The document contains multiple Java programs that perform various tasks such as checking if a string is a palindrome, counting character frequency, reversing a string, finding the second smallest element in an array, multiplying matrices, transposing a matrix, and checking if a number is prime. Each program uses the Scanner class for input and includes basic control structures like loops and conditionals. The programs demonstrate fundamental programming concepts in Java.

Uploaded by

WITCHER
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
2 views5 pages

Java_LAB_RECORD

The document contains multiple Java programs that perform various tasks such as checking if a string is a palindrome, counting character frequency, reversing a string, finding the second smallest element in an array, multiplying matrices, transposing a matrix, and checking if a number is prime. Each program uses the Scanner class for input and includes basic control structures like loops and conditionals. The programs demonstrate fundamental programming concepts in Java.

Uploaded by

WITCHER
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 5

import java.util.

*;
class pr2{
public static void main(String[] args){
Scanner my_scanner = new Scanner(System.in);
System.out.println("Enter the string");
String str = my_scanner.nextLine();
int l=0;int r = str.length()-1;
int flag=0;
while(l<r){
if(str.charAt(l) != str.charAt(r)){
flag=1;
break;
}
r--;
l++;
}
if(flag==0)
System.out.println("PALINDROME");
else if(flag==1)
System.out.println("NOt PALINDROME");
}
}

import java.util.*;
class pr3{
public static void main(String[] args){
Scanner my_scanner = new Scanner(System.in);
System.out.println("Enter the string");
String str = my_scanner.nextLine();
System.out.println("Enter the character");
char ch = my_scanner.next().charAt(0);
int count =0;
for(int i=0;i<str.length();i++){
if(str.charAt(i) == ch)
count++;
}
System.out.println("Frequency is : "+ count);
}
}
import java.util.*;
class pr4{
public static void main(String[] args){
Scanner my_scanner = new Scanner(System.in);
System.out.println("Enter the string");
String str = my_scanner.nextLine();
String rev="";
for(int i=str.length()-1;i>=0;i--){
rev =rev+ str.charAt(i); //Adding reverse in the beginning and after
}
System.out.println("Reversed string: " + rev);
}
}

import java.util.*;
public class pr5{
public static void main(String[] args){
System.out.println("ENter the number of elemts in array");
Scanner my_scanner = new Scanner(System.in);
int n = my_scanner.nextInt();
int arr[] = new int[n];
System.out.println("ENter the elements");
for(int i=0;i<n;i++){
arr[i] =my_scanner.nextInt();
}
int min = arr[0];
int second_min =2147483647;
for(int i=0;i<arr.length;i++){
if(arr[i] < min){
second_min= min;
min = arr[i];
}
else if(arr[i]>min && arr[i]<second_min){
second_min = arr[i];
}
}
System.out.println("Second smallest element is: " + second_min);
}
}
import java.util.*;
class pr6{
public static void main(String[] args){
Scanner my_scanner = new Scanner(System.in);
System.out.println("Enter the dimensions of array A and B");
int r1 = my_scanner.nextInt();
int c1 = my_scanner.nextInt();
int r2 = my_scanner.nextInt();
int c2 = my_scanner.nextInt();
if(c1 != r2){
System.out.println("Multiplication not feasible");
return;
}
else{
int[][] A =new int[r1][c1];
int[][] B= new int[r2][c2];
System.out.println("ENteer elemeents in A");
for(int i=0;i<r1;i++){
for(int j=0;j<c1;j++){
A[i][j] = my_scanner.nextInt();
}
}
System.out.println("Enter elemets in B");
for(int i=0;i<r1;i++){
for(int j=0;j<c1;j++){
B[i][j] = my_scanner.nextInt();
}
}
int[][] c = new int [r1][c2];
for(int i=0;i<r1;i++){
for(int j=0;j<c2;j++){
c[i][j] =0;
for(int k=0;k<c1;k++){
c[i][j] += A[i][k] * B[k][j];
}
}
}
System.out.println("THE product is ");

for(int i=0;i<r1;i++){
for(int j=0;j<c2;j++){
System.out.print(c[i][j]+" ");
}
System.out.println("");
}
}
}
}

import java.util.*;
class pr7{
public static void main(String[] args){
Scanner my_scanner = new Scanner(System.in);
System.out.println("Enter the dimensions of array ");
int r1 = my_scanner.nextInt();
int c1 = my_scanner.nextInt();
int[][] A =new int[r1][c1];
System.out.println("Enter elements ");
for(int i=0;i<r1;i++){
for(int j=0;j<c1;j++){
A[i][j] = my_scanner.nextInt();
}
}
System.out.println("Transpose is: ");
for(int i=0;i<c1;i++ ){
for(int j=0;j<r1;j++){
System.out.print(A[j][i]+ " ");
}
System.out.println("\n");
}
}
}
import java.util.*;
class pr1{
public static void main(String[] args){
int flag=0;
Scanner my_scanner = new Scanner(System.in);
System.out.println("Enter the number: ");

int num = my_scanner.nextInt();

for(int i=2;i<num/2;i++){
if(num%i ==0){
flag =1;
break;
}
}
if(num ==0 || num ==1){
System.out.println("NOt a prime number");
}
else if(flag ==0)
System.out.println("Prime number");
else if(flag ==1)
System.out.println("Not a Prime number");
}
}

You might also like