Computer Science, asked by theshivvam, 4 months ago

Write a program in java to input an array and print largest and smallest element.​

Answers

Answered by Anonymous
1

Answer:

this is a correct answer

Explanation:

Program to print the smallest element in an array

In this program, we need to find out the smallest element present in the array. This can be achieved by maintaining a variable min which initially will hold the value of the first element. Loop through the array by comparing the value of min with elements of the array. If any of the element's value is less than min, store the value of the element in min.

Program to print the smallest element in an array

Consider above array. Initially, min will hold the value 25. In the 1st iteration, min will be compared with 11. Since 11 is less than 25. Min will hold the value 11. In a 2nd iteration, 11 will be compared with 7. Now, 7 is less than 11. So, min will take the value 7. Continue this process until the end of the array is reached. At last, min will hold the smallest value element in the array.

Algorithm

STEP 1: START

STEP 2: INITIALIZE arr[] = {25, 11, 7, 75, 56}

STEP 3: min = arr[0]

STEP 4: REPEAT STEP 5 for(i=0; i< arr.length; i++)

STEP 5: if(arr[i]<min)

min=arr[i]

STEP 6: PRINT "Smallest element in given array:"

STEP 7: PRINT min

STEP 8: END

pls Mark me as brainiest and follow me

Answered by creativityproof
2

Answer:

This program Finds the Largest Number and the Smallest Number present in the Array Variable. It uses for loop to iterate the array upto the end of its index value and if conditions evaluates the biggest and smallest numbers and  that store in the specified variable.

public class FindBiggestSmallestNumber {

          public static void main(String[] args) {

               int numbers[] = new int[]{33,53,73,94,22,45,23,87,13,63};

               int smallest = numbers[0];

               int biggest = numbers[0];

                for(int i=1; i< numbers.length; i++)

               {

                         if(numbers[i] > biggest)

                               biggest = numbers[i];

                       else if (numbers[i] < smallest)

                               smallest = numbers[i];          

             

                 }

             System.out.println("Largest Number is : " + biggest);

               System.out.println("Smallest Number is : " + smallest);    

                     

                       

           

Similar questions