Program:-
public class PracticeSet6V29 {
public static void main(String[] args) {
//Q1.create an array of 5 float and calculate their sum.
System.out.println("Q1.");
float [] marks = {12.5f, 45.2f, 56.5f, 46.5f, 71.4f};
float sum = 0;
for(float element : marks){
sum = sum + element;
}
System.out.println("The sum of the array elements is: "+sum);
System.out.println();
//Q2.write a program to find out whether a given integer is present in an array or not.
System.out.println("Q2.");
float num = 45.2f;
boolean inanarray = false;
for(float element : marks){
if(num==element){
inanarray = true;
break;/* Here break is used because if the given element is present in the array then the comparison of next elements
will stop and the code become more useful. */
}
}
if(inanarray){
System.out.println("The element is present in an array");
}
else{
System.out.println("The element is not present in an array");
}
System.out.println();
//Q3. Calculate average marks from an array containing marks of all students in physics using for each loop.
System.out.println("Q3.");
int [] marks1 = {98, 89, 65, 75, 85};
int sum1 = 0 ;
for(int element : marks1){
sum1 = sum1 + element;
}
System.out.println("The average marks from an array containing marks is: "+ sum1/marks1.length);
System.out.println();
//Q4. Create a java program to add two matrices of size 2*3.
System.out.println("Q4.");
int [][] mat1 = {{1,2,3},
{4,5,6}};
int [][] mat2 = {{3,5,7},
{8,4,9}};
int [][] result = {{0,0,0},
{0,0,0}};
for(int i = 0;i<mat1.length; i++){
for(int j = 0; j<mat1[i].length;j++){
result[i][j] = mat1[i][j] + mat2[i][j];
System.out.print(result[i][j] + " ");
}
System.out.println();
}
System.out.println();
//Q5. write a java program to reverse an array.
System.out.println("Q5.");
int [] arr = {1, 2, 3, 4, 5, 6};
int l = arr.length;
int n = Math.floorDiv(l,2);
int temp;
for(int k = 0; k<n; k++){
temp = arr[k];
arr[k] = arr[l-k-1];
arr[l-k-1] = temp;
}
for(int element:arr){
System.out.print(element + " ");
}
System.out.println();
System.out.println();
//Q6.write a java program to find the maximum element in na array.
System.out.println("Q6.");
int max = 0;
int [] arr1 = {1, 2, 3, 9, 8, 7, 6};
for(int element: arr1){
if(element>max){
max = element;
}
}
System.out.println("The array is :");
for(int element : arr1){
System.out.print(element+" ");
}
System.out.println();
System.out.println("The maximum element from the list of elements in an array is: " + max);
System.out.println();
/*if the given array contains the negative elements then we have to use the:MIN_VALUE , MAX_VALUE.
System.out.println(Integer.MIN_VALUE);
System.out.println(Integer.MAX_VALUE);
this gives the maximum and a minimum value that the integer can hold
we can use these as follows:
int [] arr2 = {1, 2, -76573, -999, -88, -27, 6};
int max1 = Integer.MIN_VALUE;
for(int element: arr2){
if(element>max1){
max1 = element;
}
}
System.out.println("The array is :");
for(int element : arr2){
System.out.print(element+" ");
}
System.out.println();
System.out.println("The maximum element from the list of elements in an array is: " + max1);
it can also use to find minimum element as in the following question,
*/
//Q7.Write a java program to find the minimum element in an array.
System.out.println("Q7.");
int [] arr2 = {1, 2, -76573, -999, -88, -27, 6};
int min = Integer.MAX_VALUE;
for(int element: arr2){
if(element<min){
min = element;
}
}
System.out.println("The array is :");
for(int element : arr2){
System.out.print(element+" ");
}
System.out.println();
System.out.println("The minimum element from the list of elements in an array is: " + min);
System.out.println();
//Q8. write a java program to find whether an array ia sorted or not.
System.out.println("Q8.");
boolean issorted = true;
int [] arr3 = {1, 2100, 3, 455, 5, 34, 67};
for(int t = 0;t<arr3.length; t++){
if(arr3[t] > arr3[t+1]) {
issorted = false;
break;
}
}
if(issorted){
System.out.println("the given array is sorted. ");
}
else{
System.out.println("the given array is not sorted.");
}
}
}

