Saturday, 12 October 2013

Program to import a package that will check whether the no is armstrong or not

This program is written in two steps.

Step 1: Write a package and compile it.

//Package to check whether the no is armstrong or not
//Developed by Ahlam Ansari
   
package ArmstrongCheck;
public class ArmstrongPkg
{
    public String checkArmstrong(int n)
    {
        int s=0,r,m,p=0,j=1;
        m=n;
        while(m!=0)
        {   
            m=m/10;
            p++;   
        }
        int a=n;
        while(a!=0)
        {
            r=a%10;
            s=s+(int)Math.pow(r,p);
            a=a/10;   
        }
        if(n==s)
            return("is an Armstrong Number");
        else
            return("is not an Armstrong Number");
    }
}

/*
C:\Users\Ahlam\Google Drive\My Lectures\Fall\OOPM\Programs>javac -d . ArmstrongPkg.java
*/

Step 2: Write a program that imports that package and create the object of the class you want to import and call the function using that object

//Program to import a package that will check whether the no is armstrong or not
//Developed by Ahlam Ansari
import ArmstrongCheck.ArmstrongPkg;   
import java.io.*;
import java.util.*;
class MainArmPkg
{
    public static void main(String ahlam[])
    {
        int n;
        Scanner d=new Scanner(System.in);
        System.out.println("Enter the number");
        n=d.nextInt();
        ArmstrongPkg arm=new ArmstrongPkg ();
        System.out.print(n+ " " +arm.checkArmstrong(n));
    }
}

/*
Output
C:\Users\Ahlam\Google Drive\My Lectures\Fall\OOPM\Programs>javac MainArmPkg.java
C:\Users\Ahlam\Google Drive\My Lectures\Fall\OOPM\Programs>java MainArmPkg
Enter the number
153
153 is an Armstrong Number
C:\Users\Ahlam\Google Drive\My Lectures\Fall\OOPM\Programs>java MainArmPkg
Enter the number
154
154 is not an Armstrong Number
C:\Users\Ahlam\Google Drive\My Lectures\Fall\OOPM\Programs>
*/


No comments:

Post a Comment