What's new

Closed Ang gulo! haha

Status
Not open for further replies.

xxxolayxxx

Addict
Joined
Sep 20, 2018
Posts
12
Reaction
0
Points
69
Age
24
Sample Run:
Enter a number: 12534 <----- int num
5 is the hight digits in 12534
-
eh? yung ginawa po nung prof namin sa exam. (int num) lang.. hindi naman naka array. what i mean isang input lang pero nakuha nya yung 5 as highest? Paano po yun mga paps? help pp hehe salamat
 
Wala naman sa google paps.
This is the example.
int num = 12534;
Tass ang output nya,
5 is the highest digit in 12534.
-
ang logic... hindi naman array yung initialize at fix value sya. then paano na call ng ganun na lang. Hehe
 
binuksan mo yung link?

You do not have permission to view the full content of this post. Log in or register now.
public static void main(String[] args) {
int smallest = 0;
int large = 0;
int num;
System.out.println("enter the number");//how many number you want to enter
Scanner input = new Scanner(System.in);
int n = input.nextInt();
num = input.nextInt();
smallest = num; //assume first entered number as small one
// i starts from 2 because we already took one num value
for (int i = 2; i < n; i++) {
num = input.nextInt();
//comparing each time entered number with large one
if (num > large) {
large = num;
}
//comparing each time entered number with smallest one
if (num < smallest) {
smallest = num;
}
}
System.out.println("the largest is:" + large);
System.out.println("Smallest no is : " + smallest);
}


check mo to, ang ginagawa ay icompore yung una sa pangalawa and so on tapos kung ano yung pinka malaki ay naka store
 
I believe this is the solution of your Professor. He used division and modulo operator. Division by 10 in an integer and assign it within itself will remove its last digit + doing modulo operator by 10 will get the last digit.

Code:
import java.util.Scanner;

public class Main{
  public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    int num, temp, max = 0;
    num = in.nextInt();

    while(num > 0){
      temp = num % 10;
      num  = num / 10;
      if(temp > max){
        max = temp;
      }
      // System.out.println("get=" + temp + ":" + "remain=" + num);
    }
    System.out.println(max);
  }
}

Note: remove the comment part if you want to trace extraction
 
Pinagawa sa amin yan sir. Ang
ginawa ko,

yong int, convert ko muna sa string.

String temp = String.valueOf(num);

Tapos, inisa isa kong nilagay sa arraylist

ArrayList <Integer> tempLi = new ArrayList <> ();
for(int x = 0; x != temp.length(); x++){
tempLi.add(Integer.valueOf(temp.charAt(x) +""));
}
Collections.sort(tempLi);
System.out.print("highest : " +tempLi.get(tempLi.size() -1));
 
Last edited:
Status
Not open for further replies.
Back
Top