Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // program to check if a given number is an Armstrong number or not
- import java.util.Scanner;
- import java.util.ArrayList;
- class armstrong{
- int number,upper,lower;
- Scanner sc = new Scanner(System.in);
- public void getData(){
- System.out.print("Enter the number : ");
- this.number = sc.nextInt();
- }
- public void getRange(){
- System.out.print("Enter the upper limit : ");
- this.upper = sc.nextInt();
- System.out.print("Enter the lower limit : ");
- this.lower = sc.nextInt();
- }
- boolean armstrong(){
- ArrayList<Integer> myNum = new ArrayList<Integer>();
- int num = this.number;
- int temp = 0;
- int sum = 0;
- while(num != 0){
- temp = num%10;
- num = num/10;
- myNum.add(temp);
- }
- for(int i : myNum){
- sum = sum + i*i*i;
- }
- if(sum == this.number){
- return true;
- }
- else{
- return false;
- }
- }
- void check(){
- for(int i=this.lower; i<=this.upper;i++){
- this.number = i;
- boolean isArmstrong = armstrong();
- if(isArmstrong){
- System.out.println(i);
- }
- }
- }
- }
- public class armstrongChecker{
- public static void main(String arg[]){
- Scanner s = new Scanner(System.in);
- boolean cont = true;
- int choice = 0;
- while(cont){
- System.out.println("The following choices are availiable : ");
- System.out.println("1.Check if a number is an Armstrong number or not");
- System.out.println("2.Print all armstrong numbers in a given range");
- System.out.print("Enter your choice : ");
- choice = s.nextInt();
- while(choice > 2 || choice < 0){
- System.out.println("Wrong input!");
- System.out.print("Enter your choice : ");
- choice = s.nextInt();
- }
- if(choice == 1){
- armstrong a1 = new armstrong();
- a1.getData();
- boolean isArmstrong = a1.armstrong();
- if(isArmstrong){
- System.out.println("The entered number is an Armstrong Number.");
- }
- else{
- System.out.println("The entered number is not an Armstrong Number.");
- }
- }
- else if(choice == 2){
- armstrong a1 = new armstrong();
- a1.getRange();
- a1.check();
- }
- System.out.print("Do you want to continue (1/0): ");
- int n = s.nextInt();
- if(n == 0){
- cont = false;
- }
- }
- }
- }
Add Comment
Please, Sign In to add comment