Home > Mobile >  Is there a way to use If-else statement instead of while loop?
Is there a way to use If-else statement instead of while loop?

Time:04-24

Im trying to find the largest digit in an inputted number and all examples i can see are using while and im a bit troubled in coding it using if else. this is my sample JAVA code:

Scanner cs1 = new Scanner (System.in);
        System.out.println ("Input three digit number : ");
        int num = cs1.nextInt ();
       
        int reminder, Largest_number= 0;
        
        while (num > 0)
        {
            reminder = num % 10;
           
            if (Largest_number< reminder) 
            {
                Largest_number= reminder;
            }
  


            num = num / 10;
        }
     
        System.out.println("\nOutput : " Largest_number);
       
        cs1.close();
    }
}

CodePudding user response:

You can loop through that number without loop.

public static void main(String[] args) {
    Scanner cs1 = new Scanner(System.in);
    System.out.println("Input three digit number : ");
    int num = cs1.nextInt();

    int largestNumber = 0;



    System.out.println("\nOutput : "   getLargestNumber(Math.abs(num), largestNumber));

    cs1.close();
}

static int getLargestNumber(int num, int largestNumber){
    if (num>0){
        int reminder = num % 10;

        if (largestNumber < reminder) {
            largestNumber= reminder;
        }
        num = num/10;

        largestNumber = getLargestNumber(num, largestNumber);

    }
    return largestNumber;

}

What I am doing here is basically I am mimicing standard for or while loops with a thing called recursion.

From GeeksforGeeks:

What is Recursion? The process in which a function calls itself directly or indirectly is called recursion and the corresponding function is called as recursive function.

So I'm recursively calling static method getLargestNumber(int num, int largest_number) until I reach the moment when I do not enter if (num>0) statement.

And since calling of getLargestNumber is happening inside of that if statement, then the recursion stops, and I get the final result back.

UPDATE

You algorithm is wrong. You need to pass Absolute value of entered integer. Otherwise you algorithm will give wrong answer if you pass negative value as input.

Changed num to Math.abs(num).

CodePudding user response:

Easier will be to:

  1. Treat input as String
  2. Than create the Chars array from this input string
  3. Sort it
  4. Get the last element from sorted array.

Please find the working code:

import java.util.Arrays;

public class BigDataETLStackQuestion {
    public static void main(String[] args) {
        String input = "24513231231";
        char[] chars = input.toCharArray();
        Arrays.sort(chars);
        System.out.println(chars);
        System.out.println("The largest digit: "   chars[chars.length - 1]);
    }
}

The sorted array:

11122233345
The largest digit: 5

If you are interested in programming, please visit my blog: https://bigdata-etl.com/

CodePudding user response:

This is a solution without any kind of loop but only if there is guaranteed to have as input 3-digit numbers.

import java.util.Scanner;

public class App {
    public static void main(String[] args) throws Exception {
        Scanner cs1 = new Scanner (System.in);
        System.out.print("Input three digit number: ");
        String threeDigitNum = cs1.next();
       
        int largestNum, tmpNum;

        // assuming 1st digit is the largest
        largestNum = threeDigitNum.charAt(0) - '0';

        // checking if 2nd digit is greater than 1st
        tmpNum = threeDigitNum.charAt(1) - '0';
        if( tmpNum > largestNum ) 
            largestNum = tmpNum;
        
        // checking if 3nd digit is greater than 1st or 2nd
        tmpNum = threeDigitNum.charAt(2) - '0';
        if( tmpNum > largestNum ) 
            largestNum = tmpNum;

        System.out.println("\nOutput : " largestNum);
       
        cs1.close();
    }
}

Note: Any char is reprsented as an integer value, and that is what charAt() method returns. So from it subtracting the chrachter '0' (represented by it's integer value) it gives as a result the integer equivalent of that value. In case of digits, for example '8' - '0' will result in integer value 8.

  •  Tags:  
  • java
  • Related