Computer Science, asked by MeghaLakshmiB, 11 months ago

Question 7
Write a program to create an array to store 10 integers and print the largest integer and
the smallest integer in that array.

Answers

Answered by QGP
26

Largest and Smallest Integers in an Array - Java

We will take user input of 10 integers using the Scanner class and the \texttt{Scanner.nextInt()} method.

Then, we initialize two variables, \texttt{largestInteger} and \texttt{smallestInteger} with the first value in the array, which we have named \texttt{integerList[]} here.

We loop through the array. If any number is larger than \texttt{largestInteger}, then we assign that to the variable. If any number is smaller than \texttt{smallestInteger}, we assign that to the variable.

Finally, we print the results.

 \rule{300}{1}

ExtremeIntegers.java

import java.util.Scanner;       //Import Scanner

public class ExtremeIntegers {

   public static void main(String[] args) {

       Scanner sc = new Scanner(System.in);    //Create Scanner object

       int integerList[] = new int[10];    //Initialize an empty integer array

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

           System.out.print("Enter integer ["+(i+1)+"]: ");

           integerList[i] = sc.nextInt();  //Take user input for integers

       }

       int smallestInteger = integerList[0];   //Initialize smallest integer

       int largestInteger = integerList[0];    //Initialize largest integer

       for(int i = 1; i < 10; i++) {   //Loop through array to find largest and smallest integers

           if(largestInteger < integerList[i]) {

               largestInteger = integerList[i];

           }

           

           if(smallestInteger > integerList[i]) {

               smallestInteger = integerList[i];

           }

       }

       //Print the results

       System.out.println("The largest integer is: "+largestInteger);

       System.out.println("The smallest integer is: "+smallestInteger);

   }

}

Attachments:
Answered by tiger009
2

C Programming Language

Output:

How many elements:5

Enter the Array:

12

5  

35

25

28

The largest element is 35

The smallest element is 5

Explanation:

//set header file

#include<stdio.h>

//define a main method

int main()

{

 //declare integer type array variable

int a[50],i,n;

 //print the message

printf("How many elements:");

 //get input from the user

scanf("%d",&n);

 //print the message

printf("Enter the Array:\n");

 //set for loop to get elements from the user

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

 {

 scanf("%d",&a[i]);

 }

 //set two integer data type variables

 //and point them to the first element of the array

int largest=a[0];

 int smallest=a[0];

 //set for loop

for(i=1;i<n;++i)

{

   //check the largest element of the array

 if(a[i]>largest)

     //then assign the largest element into the variable

  largest=a[i];

   //check the smallest element of the array

 if(a[i]<smallest)

     //then assign the largest element into the variable

  smallest=a[i];

}

 //print the largest element with message

printf("The largest element is %d",largest);

 //print the smallest element with message

printf("\nThe smallest element is %d",smallest);

return 0;

}

The following are the description of the program.

  • Firstly, set the required header file then, define the main method and inside it.
  • Declare integer data type array variable 'a' then, declare two integer data type variables 'i' and 'n' and get input in the variable 'n' from the user.
  • Set the for loop to get the elements of the array from the user in the array variable 'a'.
  • Declare two variables 'largest' and 'smallest' to store the largest and the smallest element of the array then, point them to the 1st element of the array.
  • Set the for loop statement, that store the largest and the smallest elements of the array.

Learn More:

https://brainly.in/question/16131867

Similar questions