Computer Science, asked by Akshat011, 1 year ago

WAP to print all Armstrong no.s from 1 to 500

Answers

Answered by QGP
8
Here's a JAVA Program which performs the given task. Copy the program into an IDE. Then the comments will be visible properly.


public class Armstrong_Mod      //Creating Class
{
    public static void main(String[] args)      //Creating Main Function
    {

        long n,sum,d,p;         //Some variables useful in calculations

        long limit=500;         //The limit upto which we want to find Armstrong Numbers

        System.out.println("A number is an Armstrong number, if the sum of cubes of the digits is equal to the original number.\n");

        System.out.println("The Armstrong Numbers till "+limit+" are: \n");

        for(n=1;n<=limit;n++)       //Loop Variable n will go from 1 to limit (here 500)
        {
            p=n;            //We will perform calculations on p. We copy value of n onto p
            sum=0;          //Sum represents sum of cubes of digits.

            do
            {
                d=p%10;             //The do-while loop extratcs the digits from p, and adds their cubes to sum
                sum+=d*d*d;
                p=p/10;
            }
            while(p!=0);

            if (sum==n)             //If sum of cubes of digits is equal to original number, then it is an Armstrong Number, and is printed
                System.out.println(n);

        }

        System.out.println();
    }
}
Similar questions