Package - Armstrong number


/*
Write a java program using package to find the number is Armstrong number

Steps :
Create a parent folder (ooplab) and sub folder (Armstrong).


Create the files ArmstrongNumber.java in sub folder for finding Armstrong number

Create the files ArmNum.java in parent folder – to import Armstrong package, create the object and invoke the methods.

Compile ArmstrongNumber.java

Compile and execute ArmNum.java


*/


// ArmstrongNumber.java
// To be created and saved in folder Armstrong.
// Compile and create the bytecode file (ArmstrongNumber.class)

package Armstrong;

import java.util.*;

public class ArmstrongNumber
{
     Scanner s = new Scanner(System.in);
    
     public void fnArmstrong()
     {
          System.out.print("Enter a Number : ");
          int N = s.nextInt();
         
          int tmp = N;
          int sum = 0;
         
          while(tmp>0)
          {
              int r = tmp%10;
              sum += (r*r*r);
              tmp/=10;
          }
         
          if(sum==N)
              System.out.println(N+" is an Armstrong Number");
          else
              System.out.println(N+" is not an Armstrong Number");
     }
}


// ArmNum.java
// To be created and saved in parent folder.
// import the package, compile and execute

import Armstrong.*;

class ArmNum
{
     public static void main(String as[])
     {
          ArmstrongNumber an = new ArmstrongNumber();
          an.fnArmstrong();
     }
}

No comments:

Post a Comment

Don't be a silent reader...
Leave your comments...

Anu