A Way For Learning

Smallest Number

No comments
Smallest Number: Write a python program that can be formed by multiplying 3 digits of a given
integer.

def small_product_3(n):
 if len(n)>=3:
  l = sorted(str(n))
  return int(l[0]) * int(l[1]) * int(l[2])
 elif len(n)== 2:
  l = sorted(str(n))
  return int(l[0]) * int(l[1])
 else:
  return n

print( small_product_3( input() ) )

JAVA

import java.util.*;

public class Solution {

 public static String stringSort(String original) {
  char[] chars = original.toCharArray();
  Arrays.sort(chars);
  String sorted = new String(chars);
  return sorted;
 }

 public static int small_product_3(String n) {
  if(n.length() >= 3) {
   n = stringSort(n);
   return (Integer.parseInt(n.charAt(0) + "") * Integer.parseInt(n.charAt(1) + "") * Integer.parseInt(n.charAt(2) + ""));
  } else if(n.length() == 2) {
   n = stringSort(n);
   return (Integer.parseInt(n.charAt(0) + "") * Integer.parseInt(n.charAt(1) + ""));
  } else {
   return Integer.parseInt(n);
  }
 }


 public static void main(String[] args) {
  Scanner scan = new Scanner(System.in);
  String n = scan.nextLine();
  System.out.println(small_product_3(n));
 }
}



/*def small_product_3(n):
 if len(n)>=3:
  l = sorted(str(n))
  return int(l[0]) * int(l[1]) * int(l[2])
 elif len(n)== 2:
  l = sorted(str(n))
  return int(l[0]) * int(l[1])
 else:
  return n

print( small_product_3( input() ) )*/

No comments :

Post a Comment