Home > Back-end >  Making an Armstrong number checker but if condition is not working
Making an Armstrong number checker but if condition is not working

Time:03-28

I was Making A Armstrong number checker not for only 3 digits numbers for wich i used Math.pow() method but after using it the if else statement is not working also when the condition is true. Here is the code.

import java.util.Scanner;
import java.lang.Math;
class Main {
 ////////////////////////////////////////////

 ////////////////////////////////////////////
 public static void main(String args[])
{
 System.out.println("Hello world!");
 Scanner sc = new 
 Scanner(System.in);
 int num = sc.nextInt();
 int  numc = num ; 
 double rem = 0;
 double cu = 0;
 int val = 0;
 int val2 = 0;
 
 
 while(num != 0){
  rem = num;
  
  while(numc != 0){
   numc /=10;
   int i = 0;
   i  ;
   val2  = i; 
  }


  cu = Math.pow(rem,val2 );
  val  = cu; 
  num /= 10;
 }


 if(val == numc){
    System.out.println("Yes its a " val2 "  Armstrong number because its returning "   val "after Calculations ");
   }
   else{
     System.out.println("No its not a " val2 " digit Armstrong number because its returning "   val  " after Calculations ");
            
           }
 
}
}
///////////////////////////////////////////

Compiling this code in terminal

And this is the Compilation of my code

CodePudding user response:

if(val == numc){ - This if part is the root cause of your problem . you are dividing numc by 10 for calculations . So at the end it will become 0 . so you will be checking if val == 0 which goes to the else loop.

So I would suggest to assign the input from the user to another variable which you can use for checking the final if - else part.

Like int input = num and at the end if(val==input){ . This would resolve your issue.

CodePudding user response:

The num and numc become zero due to "/= 10" operation. Hence the if condition fails.

Also you need not compute the length of integer every time.

Don't have the reputation to comment hence giving a full fledged solution.

Following is my solution to your problem. All the best!

import java.util.Scanner;
import java.lang.Math;

class Main {
    ////////////////////////////////////////////

    ////////////////////////////////////////////
    public static void main(String args[]) {
        System.out.println("Hello world!\n");
        Scanner sc = new Scanner(System.in);
        int num = sc.nextInt();
        int numc = num;
        double rem = 0;
        double cu = 0;
        int val = 0;
        int val2 = countNumOfDigits(num);
        
        while (num != 0) {
            rem = num % 10;
            cu = Math.pow(rem, val2);
            val  = cu;
            num /= 10;
        }

        if (val == numc) {
            System.out.println("Yes its a "   val2   " digit  Armstrong number because its returning "   val
                      "after Calculations ");
        } else {
            System.out.println("No its not a "   val2   " digit Armstrong number because its returning "   val
                      " after Calculations ");

        }

    }
    
    private static int countNumOfDigits(int number) {
        if (number < 100000) {
            if (number < 100) {
                if (number < 10) {
                    return 1;
                } else {
                    return 2;
                }
            } else {
                if (number < 1000) {
                    return 3;
                } else {
                    if (number < 10000) {
                        return 4;
                    } else {
                        return 5;
                    }
                }
            }
        } else {
            if (number < 10000000) {
                if (number < 1000000) {
                    return 6;
                } else {
                    return 7;
                }
            } else {
                if (number < 100000000) {
                    return 8;
                } else {
                    if (number < 1000000000) {
                        return 9;
                    } else {
                        return 10;
                    }
                }
            }
        }
    }
}
  • Related