Advertisement
Shailrshah

Finding Armstrong Numbers in an Array

Aug 20th, 2013
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.01 KB | None | 0 0
  1. import java.io.*;
  2. class Arrays{
  3.     private static String getInput(String prompt){
  4.         BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
  5.         System.out.print(prompt);
  6.         System.out.flush();
  7.         try{
  8.             return stdin.readLine();
  9.         }catch(Exception e){
  10.             return "Error: " + e.getMessage();
  11.         }
  12.     }
  13.     public static void main(String args[]) {
  14.         String input = getInput("Enter the number of elements: ");
  15.         int n = Integer.parseInt(input);
  16.         int [] inputArray = new int [n];
  17.         int [] Armstrong = new int [n];
  18.         int [] nonArmstrong = new int [n];
  19.         int i, countArmstrong = 0, countnonArmstrong = 0;
  20.         for(i = 0; i<n; i++){
  21.             input = getInput("Enter an element: ");
  22.             inputArray[i] = Integer.parseInt(input);
  23.             int sum = 0;
  24.             int no  = inputArray[i];
  25.             while(no != 0){
  26.                 int remainder = no % 10;
  27.                 sum += remainder * remainder * remainder;
  28.                 no /= 10;
  29.             }
  30.             if(sum==inputArray[i])
  31.                 Armstrong[countArmstrong++] = inputArray[i];
  32.             else
  33.                 nonArmstrong[countnonArmstrong++] = inputArray[i];
  34.             }
  35.             int smallArmstrong = Armstrong[0];
  36.             int largeArmstrong = Armstrong[0];
  37.             int smallnonArmstrong = nonArmstrong[0];
  38.             int largenonArmstrong = nonArmstrong[0];
  39.             System.out.println("\n\nFound " +countArmstrong+" Armstrong Numbers");
  40.             for(i = 0; i < countArmstrong; i++){
  41.                 System.out.println(Armstrong[i]);
  42.                 if(Armstrong[i]<smallArmstrong)
  43.                     smallArmstrong = Armstrong[i];
  44.                 if(Armstrong[i]>largeArmstrong)
  45.                     largeArmstrong = Armstrong[i];
  46.             }
  47.             System.out.println("smallest: "+smallArmstrong+" largest: "+largeArmstrong);
  48.             System.out.println("\n\nFound " +countnonArmstrong+" The Non-Armstrong Numbers");
  49.             for(i = 0; i < countnonArmstrong; i++){
  50.                 System.out.println(nonArmstrong[i]);
  51.                 if(nonArmstrong[i]<smallnonArmstrong)
  52.                     smallnonArmstrong = nonArmstrong[i];
  53.                 if(nonArmstrong[i]>largenonArmstrong)
  54.                     largenonArmstrong = nonArmstrong[i];
  55.             }
  56.             System.out.println("smallest: "+smallnonArmstrong+" largest: "+largenonArmstrong);
  57.     }
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement