Computer Science, asked by BrainlyProgrammer, 2 months ago

New Question!!!

WAP in Java to print the maximum and minimum number of the given 5 numbers as input.
_
Restriction:-
=> Not to use any Conditional contruct
=> Not to use more than 2 Variables
_
All the best!​

Answers

Answered by Anonymous
1

Answer:

  • ᴇɴᴛᴇʀ ᴛʜᴇ ꜱɪᴢᴇ ᴏꜰ ᴀʀʀᴀʏ ᴀɴᴅ ᴄʀᴇᴀᴛᴇ ᴀɴ ᴀʀʀᴀʏ ᴏꜰ ᴛʜᴇ ɪɴᴘᴜᴛ ꜱɪᴢᴇ, ꜱᴀʏ ɴ.
  • ᴜꜱɪɴɢ ᴀ ʟᴏᴏᴘ ᴄᴏɴꜱᴛʀᴜᴄᴛ, ᴇɴᴛᴇʀ ɪɴᴛᴇɢᴇʀ ɪɴᴘᴜᴛꜱ ɪɴ ᴛʜᴇ ᴀʀʀᴀʏ.
  • ᴅᴇᴄʟᴀʀᴇ 2 ᴠᴀʀɪᴀʙʟᴇꜱ ᴛʜᴀᴛ ᴀʀᴇ ɢᴏɪɴɢ ᴛᴏ ꜱᴛᴏʀᴇ ᴛʜᴇ ᴍɪɴɪᴍᴜᴍ ᴀɴᴅ ᴍᴀxɪᴍᴜᴍ ᴇʟᴇᴍᴇɴᴛꜱ ʀᴇꜱᴘᴇᴄᴛɪᴠᴇʟʏ.
  • ɪɴɪᴛɪᴀʟɪᴢᴇ ᴛʜᴇᴍ ᴡɪᴛʜ ᴛʜᴇ ᴇʟᴇᴍᴇɴᴛ ᴛʜᴀᴛ ɪꜱ ᴀᴛ ᴛʜᴇ ꜰɪʀꜱᴛ ɪɴᴅᴇx ᴏꜰ ᴛʜᴇ ᴀʀʀᴀʏ.
  • ᴜꜱɪɴɢ ᴀɴᴏᴛʜᴇʀ ʟᴏᴏᴘ ᴄᴏɴꜱᴛʀᴜᴄᴛ, ᴄᴏᴍᴘᴀʀᴇ ᴇᴀᴄʜ ᴇʟᴇᴍᴇɴᴛ ᴡɪᴛʜ ᴛʜᴇ ᴍᴀx ᴀɴᴅ ᴍɪɴ.
  • ᴋᴇᴇᴘ ꜱᴡᴀᴘᴘɪɴɢ ᴀʟᴏɴɢ.
  • ᴀꜰᴛᴇʀ ᴛʜᴇ ʟᴏᴏᴘ ᴛᴇʀᴍɪɴᴀᴛᴇꜱ, ᴘʀɪɴᴛ ᴛʜᴇ ʀᴇꜱᴜʟᴛ ᴀᴄᴄᴏʀᴅɪɴɢʟʏ.

ᴊᴀᴠᴀ ᴄᴏᴅᴇ:-

/* ᴘʀᴏɢʀᴀᴍ ᴛᴏ ᴇɴᴛᴇʀ ᴀɴ ᴀʀʀᴀʏ ᴀɴᴅ ꜰɪɴᴅ ᴛʜᴇ ᴍᴀxɪᴍᴜᴍ ᴀɴᴅ ᴍɪɴɪᴍᴜᴍ ɴᴜᴍʙᴇʀ */

ɪᴍᴘᴏʀᴛ ᴊᴀᴠᴀ.ᴜᴛɪʟ.*;

ᴄʟᴀꜱꜱ ᴀʀʀᴍɪɴᴍᴀx

{

ᴘᴜʙʟɪᴄ ꜱᴛᴀᴛɪᴄ ᴠᴏɪᴅ ᴍᴀɪɴ()

{

ꜱᴄᴀɴɴᴇʀ ɪɴᴘ=ɴᴇᴡ ꜱᴄᴀɴɴᴇʀ(ꜱʏꜱᴛᴇᴍ.ɪɴ);

ꜱʏꜱᴛᴇᴍ.ᴏᴜᴛ.ᴘʀɪɴᴛ("\ɴ ᴇɴᴛᴇʀ ꜱɪᴢᴇ ᴏꜰ ᴀʀʀᴀʏ: ");

ɪɴᴛ ɴ=ɪɴᴘ.ɴᴇxᴛɪɴᴛ();

ɪɴᴛ ɪ,ꜱᴜᴍ=0;

ɪɴᴛ ᴀʀʀ[]=ɴᴇᴡ ɪɴᴛ[ɴ]; //ᴄʀᴇᴀᴛɪɴɢ ɴ-ꜱɪᴢᴇ ᴀʀʀᴀʏ

ꜰᴏʀ(ɪ=0;ɪ<ɴ;ɪ++) //ᴇɴᴛᴇʀɪɴɢ ɴ ɴᴜᴍʙᴇʀꜱ ɪɴ ᴀʀʀᴀʏ

{

ꜱʏꜱᴛᴇᴍ.ᴏᴜᴛ.ᴘʀɪɴᴛ("\ɴ ᴇɴᴛᴇʀ: ");

ᴀʀʀ[ɪ]=ɪɴᴘ.ɴᴇxᴛɪɴᴛ();

}

ɪɴᴛ ᴍᴀx=ᴀʀʀ[0],ᴍɪɴ=ᴀʀʀ[0]; //ɪɴɪᴛɪᴀʟɪᴢɪɴɢ ᴡɪᴛʜ ꜰɪʀꜱᴛ ᴇʟᴇᴍᴇɴᴛ.

ꜰᴏʀ(ɪ=0;ɪ<ɴ;ɪ++)

{

ɪꜰ(ᴀʀʀ[ɪ]>ᴍᴀx) //ᴄʜᴇᴄᴋɪɴɢ ᴍᴀxɪᴍᴜᴍ ᴇʟᴇᴍᴇɴᴛ

ᴍᴀx=ᴀʀʀ[ɪ];

ɪꜰ(ᴀʀʀ[ɪ]<ᴍɪɴ) //ᴄʜᴇᴄᴋɪɴɢ ᴍɪɴɪᴍᴜᴍ ᴇʟᴇᴍᴇɴᴛ

ᴍɪɴ=ᴀʀʀ[ɪ];

}

//ᴘʀɪɴᴛɪɴɢ ʀᴇꜱᴜʟᴛ

ꜱʏꜱᴛᴇᴍ.ᴏᴜᴛ.ᴘʀɪɴᴛʟɴ("\ɴ ᴍᴀxɪᴍᴜᴍ ɴᴜᴍʙᴇʀ: "+ᴍᴀx);

ꜱʏꜱᴛᴇᴍ.ᴏᴜᴛ.ᴘʀɪɴᴛʟɴ("\ɴ ᴍɪɴɪᴍᴜᴍ ɴᴜᴍʙᴇʀ: "+ᴍɪɴ);

}

}

Answered by anindyaadhikari13
3

Answer:

This is the required Java program for the question.

import java.util.*;

public class MinMax {

   public static void main() {

       int a[]=new int[4];

       a[0]=Integer.MIN_VALUE;

       a[1]=Integer.MAX_VALUE;

       Scanner sc=new Scanner(System.in);

       System.out.println("Enter 5 numbers...");

       for(a[3]=0;a[3]<5;a[3]++)  {

           System.out.print("Enter: ");

           a[2]=sc.nextInt();

           a[0]=Math.max(a[0],a[2]);

           a[1]=Math.min(a[1],a[2]);

       }

       System.out.println("Largest Number: "+a[0]);

       System.out.println("Smallest Number: "+a[1]);

       sc.close();

   }

}

Another Approach:

import java.util.*;

public class Max {

public static void main(String[] args) {

Scanner sc=new Scanner(System.in);

int a[]=new int[5];

System.out.println("Enter 5 numbers..");

a[0]=sc.nextInt();

a[1]=sc.nextInt();

a[2]=sc.nextInt();

a[3]=sc.nextInt();

a[4]=sc.nextInt();

System.out.println("Largest Number: "+Math.max(a[0],Math.max(a[1],Math.max(a[2],Math.max(a[3],a[4])))));

sc.close();

}

}

Explanation:

  • The Integer.MIN_VALUE stores the lowest value in the integer range whereas the Integer.MAX_VALUE stores the highest value in the integer range.
  • An array named a[] is declared in which, a[0] (number at first index) stores the highest number among 5 numbers. We assume that Integer.MIN_VALUE is the highest number.
  • Similarly, a[0} stores the lowest number among the 5 numbers. We assume that Integer.MAX_VALUE is the lowest number.
  • Now, we will iterate a loop 5 times. Inside the loop, we will ask the user to enter a number. The entered number will be stored at index 2.
  • Now, using Math.max() and Math.min() function, we will compare the number entered with the maximum and minimum values. So, at last, the maximum and minimum value will be stored at index 0 and 1 respectively.
  • At last, their values will be displayed on the screen.

Restrictions Followed:

  • Not to use any conditional construct. ✔
  • Not to use more than 2 variables. ✔ (Used only 1 variable - a[], array is considered as 1 variable only)

Refer to the attachment for output.

Attachments:
Similar questions