Java Problems .. Array

 


import java.util.Scanner;

// 1 Write program add two matrix of 2*3
class Problems{
public static void probOne() {
// 1 2 3 2 3 1 4 6 5 4 5 6
int[][] MatOne = {
{1,2,3}, // index 0 length = 3 0 1 2
{4,6,5}, // index 1 length = 3 0 2 2
};
int[][] MatTwo = {
{2,3,1}, // index 0 length = 3 0 1 2
{4,5,6}, // index 1 length = 3 0 1 2
};
int[][] MatResults ={
{0,0,0}, // index 0 length = 3 0 1 2
{0,0,0} // index 1 length = 3 0 1 2
};
for(int i=0;i<MatOne.length;i++){ // 2times run
for (int j =0;j<MatOne[i].length;j++){ // 3times run
MatResults[i][j] = MatOne[i][j] + MatTwo[i][j]; // formula
System.out.print(MatResults[i][j]+" ");
}
System.out.println();
}
}
// 2 Create five float array and calculate there sum
public static void probTwo() {
float[] Marks = {10f,20f,30f,40f,50f};
float sum =0;
for (float element:Marks) {
sum = sum + element;
}
System.out.println("Sum :"+sum);
System.out.println("Average :"+sum/Marks.length);
}
// 3 Find present String in array
public static void probThree() {
String[] Class = {"ranjit","raj","rohit","vish","anna"};
Scanner Scan = new Scanner(System.in);
String getInput = Scan.next() ;
for (String element:Class) {
if (element.equals(getInput)){
System.out.println(getInput+" is a "+"Present");
}
else {
System.out.println("Not Present");
}
break;
}
}
// 4 Reverse array using for loop swapping Dokyachi **chi
public static void probFour(){
int[] Arr = {1,2,3,4,5,6};
int L = Arr.length;
int n = Math.floorDiv(L,2); //2
int temp;

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

temp = Arr[i];
Arr[i] = Arr[L-1-i];
Arr[L-1-i] = temp;

}
for (int element:Arr) {
System.out.println(element);
}
}
// 5 find maximum element in the array
public static void probFive(){
int[] arr ={1,3,5,6,4,2};
int Len = arr.length; // ele > max = max = 0
int max =0; // 1 > 0 = true max = 1
for (int ele:arr){ // 3 > 1 = true max = 3
if (ele>max){ // 5 > 3 = true max = 5
max =ele; // 6 > 5 = true max = 6
} // 4 > 6 = false max = 6
} // 2 > 0 = false max = 6
System.out.println(max);
}
// 6 find array sorted or not
public static void probSix(){
boolean issorted = true;
int[] arr ={1,2,8,4,5,6}; // 1 > 2 = false
int Len = arr.length; // 2 > 8 = false
for (int i =0;i<Len-1;i++){ // 8 > 4 = true break swap bolean=fales
if (arr[i]>arr[i+1]){ // 4 > 5 = false
issorted = false; // 5 > 6 = false
break;
}
}
if (issorted){
System.out.println("Array is a sorted ");
}
else {
System.out.println("Array is not sorted");
}
}
}
public class SolveProb {
public static void main(String[] args) {
//Problems.probOne();
//Problems.probTwo();
//Problems.probThree();
//Problems.probFour();
//Problems.probFive();
//Problems.probSix();

}
}

Ranjit Patil

Ranjit is a passionate engineer specializing in AI/ML, Java, and Spring Boot. He loves talking about technology and is endlessly curious about the creation of the universe.

Post a Comment

comment here

Previous Post Next Post