Loading ...
Sorry, an error occurred while loading the content.
 

java program question

Expand Messages
  • rehan1_siddiqui
    A program to find that entered number is armstrong or not.
    Message 1 of 4 , Dec 31, 2007
      A program to find that entered number is armstrong or not.
    • Randall R Schulz
      ... It s a pretty simple concept, really. I wrote this after reading your mail and looking up what an Armstrong Number is. Free. use as you please. Randall
      Message 2 of 4 , Dec 31, 2007
        On Monday 31 December 2007 07:44, rehan1_siddiqui wrote:
        > A program to find that entered number is armstrong or not.

        It's a pretty simple concept, really.

        I wrote this after reading your mail and looking up what an Armstrong
        Number is. Free. use as you please.


        Randall Schulz


        P.S. In case attachments are discarded:

        public
        class Armstrong
        {
        public
        static
        void
        main(String[] args)
        {
        for (String arg : args)
        System.out.format("%20s: %s%n", arg, isArmstrong(arg));
        }


        public
        static
        boolean
        isArmstrong(String aCandidate)
        {
        try {
        return isArmstrong(Long.parseLong(aCandidate));
        }

        catch (NumberFormatException exNF) {
        return false;
        }
        }


        public
        static
        boolean
        isArmstrong(long aCandidate)
        {
        int nDigits = ((int) Math.floor(Math.log10((double) aCandidate))) + 1;
        long remainder = aCandidate;

        for (int iDigit = 0; iDigit < nDigits; iDigit++) {
        int digit = (int) (aCandidate % 10l);
        remainder -= digit * digit * digit;
        aCandidate /= 10;
        }

        return remainder == 0;
        }
      • Randall R Schulz
        ... Gack! I took a special case based on the first Web page I found about Armstrong numbers. Give me a few minutes to fix it, please! Randall Schulz
        Message 3 of 4 , Dec 31, 2007
          On Monday 31 December 2007 08:14, Randall R Schulz wrote:
          > On Monday 31 December 2007 07:44, rehan1_siddiqui wrote:
          > > A program to find that entered number is armstrong or not.
          >
          > It's a pretty simple concept, really.

          Gack!

          I took a special case based on the first Web page I found about
          Armstrong numbers.

          Give me a few minutes to fix it, please!


          Randall Schulz
        • Randall R Schulz
          ... OK. The general version is attached. % java Armstrong 1 2 3 4 5 6 7 8 9 153 370 371 407 1634 8208 9474 54748 92727 93084 548834 1741725 4210818 9800817
          Message 4 of 4 , Dec 31, 2007
            On Monday 31 December 2007 08:20, Randall R Schulz wrote:
            > On Monday 31 December 2007 08:14, Randall R Schulz wrote:
            > > On Monday 31 December 2007 07:44, rehan1_siddiqui wrote:
            > > > A program to find that entered number is armstrong or not.
            > >
            > > It's a pretty simple concept, really.
            >
            > Gack!
            >
            > I took a special case based on the first Web page I found about
            > Armstrong numbers.
            >
            > Give me a few minutes to fix it, please!


            OK. The general version is attached.

            % java Armstrong 1 2 3 4 5 6 7 8 9 153 370 371 407 1634 8208 9474 54748
            92727 93084 548834 1741725 4210818 9800817 9926315 24678050 24678051
            88593477 146511208 472335975 534494836 912985153 4679307774
            1: true
            2: true
            3: true
            4: true
            5: true
            6: true
            7: true
            8: true
            9: true
            153: true
            370: true
            371: true
            407: true
            1634: true
            8208: true
            9474: true
            54748: true
            92727: true
            93084: true
            548834: true
            1741725: true
            4210818: true
            9800817: true
            9926315: true
            24678050: true
            24678051: true
            88593477: true
            146511208: true
            472335975: true
            534494836: true
            912985153: true
            4679307774: true


            Randall Schulz
          Your message has been successfully submitted and would be delivered to recipients shortly.