Sunday, October 21, 2012

Java program to print amstrong numbers upto given number


import java.io.*;
class strangenum
{int num,temp,d,s;
  strangenum(int a)
  { num=a;}
  void gen_strange()
  { System.out.println("strnge numbers are");
       for(int i=0;i<=num;i++)
        {temp=i;
          s=0;
          while(temp>0)
          { d=temp%10;
             s+=d*d*d;
             temp/=10; }
      if(s==i)
        System.out.println(i); }
  }
  public static void main(String s[])throws IOException
  { BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
     System.out.println("Enter the limit");
     int a=Integer.parseInt(br.readLine());
     strangenum st=new strangenum(a);
     st.gen_strange();}
}

0 comments:

Post a Comment