Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.*;
- class Arrays{
- private static String getInput(String prompt){
- BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
- System.out.print(prompt);
- System.out.flush();
- try{
- return stdin.readLine();
- }catch(Exception e){
- return "Error: " + e.getMessage();
- }
- }
- public static void main(String args[]) {
- String input = getInput("Enter the number of elements: ");
- int n = Integer.parseInt(input);
- int [] inputArray = new int [n];
- int [] Armstrong = new int [n];
- int [] nonArmstrong = new int [n];
- int i, countArmstrong = 0, countnonArmstrong = 0;
- for(i = 0; i<n; i++){
- input = getInput("Enter an element: ");
- inputArray[i] = Integer.parseInt(input);
- int sum = 0;
- int no = inputArray[i];
- while(no != 0){
- int remainder = no % 10;
- sum += remainder * remainder * remainder;
- no /= 10;
- }
- if(sum==inputArray[i])
- Armstrong[countArmstrong++] = inputArray[i];
- else
- nonArmstrong[countnonArmstrong++] = inputArray[i];
- }
- int smallArmstrong = Armstrong[0];
- int largeArmstrong = Armstrong[0];
- int smallnonArmstrong = nonArmstrong[0];
- int largenonArmstrong = nonArmstrong[0];
- System.out.println("\n\nFound " +countArmstrong+" Armstrong Numbers");
- for(i = 0; i < countArmstrong; i++){
- System.out.println(Armstrong[i]);
- if(Armstrong[i]<smallArmstrong)
- smallArmstrong = Armstrong[i];
- if(Armstrong[i]>largeArmstrong)
- largeArmstrong = Armstrong[i];
- }
- System.out.println("smallest: "+smallArmstrong+" largest: "+largeArmstrong);
- System.out.println("\n\nFound " +countnonArmstrong+" The Non-Armstrong Numbers");
- for(i = 0; i < countnonArmstrong; i++){
- System.out.println(nonArmstrong[i]);
- if(nonArmstrong[i]<smallnonArmstrong)
- smallnonArmstrong = nonArmstrong[i];
- if(nonArmstrong[i]>largenonArmstrong)
- largenonArmstrong = nonArmstrong[i];
- }
- System.out.println("smallest: "+smallnonArmstrong+" largest: "+largenonArmstrong);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement