m2skills

armstrong java

Apr 18th, 2017
414
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.22 KB | None | 0 0
  1. // program to check if a given number is an Armstrong number or not
  2.  
  3. import java.util.Scanner;
  4. import java.util.ArrayList;
  5.  
  6. class armstrong{
  7.    
  8.     int number,upper,lower;
  9.     Scanner sc = new Scanner(System.in);
  10.    
  11.     public void getData(){
  12.         System.out.print("Enter the number : ");
  13.         this.number = sc.nextInt();
  14.     }
  15.    
  16.     public void getRange(){
  17.         System.out.print("Enter the upper limit : ");
  18.         this.upper = sc.nextInt();
  19.         System.out.print("Enter the lower limit : ");
  20.         this.lower = sc.nextInt();
  21.     }
  22.    
  23.     boolean armstrong(){
  24.         ArrayList<Integer> myNum = new ArrayList<Integer>();
  25.         int num = this.number;
  26.         int temp = 0;
  27.         int sum = 0;
  28.         while(num != 0){
  29.             temp = num%10;
  30.             num = num/10;
  31.             myNum.add(temp);
  32.         }  
  33.        
  34.         for(int i : myNum){
  35.             sum = sum + i*i*i;
  36.         }
  37.        
  38.         if(sum == this.number){
  39.             return true;
  40.         }
  41.         else{
  42.             return false;
  43.         }
  44.        
  45.     }
  46.    
  47.     void check(){
  48.         for(int i=this.lower; i<=this.upper;i++){
  49.             this.number = i;
  50.             boolean isArmstrong = armstrong();
  51.             if(isArmstrong){
  52.                 System.out.println(i);
  53.             }
  54.         }
  55.     }
  56.    
  57. }
  58.  
  59. public class armstrongChecker{
  60.     public static void main(String arg[]){
  61.         Scanner s = new Scanner(System.in);
  62.         boolean cont = true;
  63.         int choice = 0;
  64.         while(cont){
  65.             System.out.println("The following choices are availiable : ");
  66.             System.out.println("1.Check if a number is an Armstrong number or not");
  67.             System.out.println("2.Print all armstrong numbers in a given range");
  68.             System.out.print("Enter your choice : ");
  69.             choice = s.nextInt();
  70.            
  71.             while(choice > 2 || choice < 0){
  72.                 System.out.println("Wrong input!");
  73.                 System.out.print("Enter your choice : ");
  74.                 choice = s.nextInt();
  75.             }
  76.            
  77.             if(choice == 1){
  78.                 armstrong a1 = new armstrong();
  79.                 a1.getData();
  80.                 boolean isArmstrong = a1.armstrong();
  81.                 if(isArmstrong){
  82.                     System.out.println("The entered number is an Armstrong Number.");
  83.                 }
  84.                 else{
  85.                     System.out.println("The entered number is not an Armstrong Number.");
  86.                 }
  87.             }
  88.             else if(choice == 2){
  89.                 armstrong a1 = new armstrong();
  90.                 a1.getRange();
  91.                 a1.check();
  92.             }
  93.             System.out.print("Do you want to continue (1/0): ");
  94.             int n = s.nextInt();
  95.             if(n == 0){
  96.                 cont = false;
  97.             }  
  98.         }
  99.     }
  100. }
Add Comment
Please, Sign In to add comment