Advertisement
Shekhar777

Java Armstrong numbers

Oct 19th, 2020
465
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.76 KB | None | 0 0
  1. Q11-Given an integer N, your task is to print all the Armstrong numbers which are present between 1 to N.
  2. Ans-import java.io.*; // for handling input/output
  3. import java.util.*; // contains Collections framework
  4.  
  5. // don't change the name of this class
  6. // you can add inner classes if needed
  7. class Main {
  8.     public static void main (String[] args) {
  9.                       // Your code here
  10.         Scanner sc=new Scanner(System.in);
  11.         int N=sc.nextInt();
  12.         for(int i=1;i<=N;i++)
  13.         {   int sum=0;
  14.             int temp=i;
  15.             while(temp!=0)
  16.             {
  17.                  sum=sum+(int)Math.pow(temp%10,3);
  18.                  temp=temp/10;
  19.             }
  20.             if(sum==i)
  21.             {
  22.                 System.out.print(i+" ");
  23.             }  
  24.         }
  25.        
  26.     }
  27. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement