Computer Science, asked by priyadharshinijoffic, 14 days ago

Find Key:
You are provided with 3 numbers: input1, input2 and input3.
Each of these are four digit numbers within the range >=1000 and <=9999.
Le
1000 <=input1 <= 9999
1000 <=input2 <= 9999
1000 <= input3 <= 9999
You are expected to find the Key using the below formula -
Key = [LARGEST digit in the thousands place of all three numbers - smallest
digit in the thousands place of all three numbers] [LARGEST digit in the
hundreds place of all three numbers - smallest digit in the hundreds place of all
three numbers] [LARGEST digit in the tens place of all three numbers - smallest
digit in the tens place of all three numbers] [LARGEST digit in the units place of
all three numbers - smallest digit in the units place of all three numbers]
Fore..g.
if input1 = 3521, input2=2452, input3=1352
then Key = (3-1] [5-3] [5-2] [2-1] = 2231
Assuming that the 3 numbers are passed to the given function, Complete the
function to find and return the Key.

Answers

Answered by aditijaink283
0

Answer:

The correct answer to the given question is in the explanation:

Explanation:

JAVA CODE:

import java.io.*;

import java.util.*;

import java.util.Collections;

class UserMainCode

{

  static int maxOfThree(int x, int y, int z) {

      return z > (x > y ? x : y) ? z : ((x > y) ? x : y);

  }

  static int minOfThree(int a, int b, int c) {

      return (a<b)?(a<c?a:c):(b<c?b:c);

  }

public static void main(String[] args)

{

   int input1 = 3521;

int input2 = 2452;

int input3 = 1352;

     int u=0,t=0,h=0,th=0;

  int length = 4;

  while(length!=0){

      if(length == 4) {

                  u = maxOfThree(input1%10,input2%10,input3%10) - minOfThree(input1%10,input2%10,input3%10);

      }

      if(length == 3){

               t = maxOfThree(input1%10,input2%10,input3%10) - minOfThree(input1%10,input2%10,input3%10);

      }

      if(length == 2){

            h = maxOfThree(input1%10,input2%10,input3%10) - minOfThree(input1%10,input2%10,input3%10);

      }

      if(length == 1){

            th = maxOfThree(input1%10,input2%10,input3%10) - minOfThree(input1%10,input2%10,input3%10);

      }

      input1/=10;

      input2/=10;

      input3/=10;

      length--;

      }

System.out.println(th*1000+h*100+t*10+u);

}

}

#SPJ2

Similar questions