Wednesday 9 October 2013

Program to print armstrong upto n

//Program to print armstrong upto n
//Developed by Ahlam Ansari

import java.io.*;
import java.util.*;
class armstrongSeries
{
    public static void main(String ahlam[])
    {
        int s=0,r,n,m,p=0,j=1;
        Scanner d=new Scanner(System.in);
        System.out.println("Enter the number");
        n=d.nextInt();
        for (int i=1;i<=n;i++)
        {
            m=i;
            s=0;
            p=0;
            while(m!=0)
            {   
                m=m/10;
                p++;   
            }
            int a=i;
            while(a!=0)
            {
               
                r=a%10;
                s=s+(int)Math.pow(r,p);
                a=a/10;   
            }
            if(i==s)
            System.out.print(i+ "\n");
        }
    }
}

/*
Output

C:\Users\Ahlam\Google Drive\My Lectures\Fall\OOPM\Programs>javac armstrongSeries.java

C:\Users\Ahlam\Google Drive\My Lectures\Fall\OOPM\Programs>java armstrongSeries
Enter the number
500
1
2
3
4
5
6
7
8
9
153
370
371
407

C:\Users\Ahlam\Google Drive\My Lectures\Fall\OOPM\Programs>
*/

No comments:

Post a Comment