Armstrong number between 1 to n in JAVA programming


  1. import java.util.Scanner;
  2.  
  3.  
  4. public class ArmstrongNumBetween1toN {
  5.  
  6. static int armstrong(int n)
  7.  
  8. {
  9.  
  10. int sum=0,rem,num,ck=0;
  11.  
  12. num=n;
  13.  
  14. while(num!=0)
  15.  
  16. {
  17.  
  18.  
  19. rem=num%10;
  20.  
  21. sum=sum+(rem*rem*rem);
  22.  
  23. num=num/10;
  24.  
  25. }
  26.  
  27. return sum;
  28.  
  29. }
  30.  
  31. public static void main(String[] args) {
  32.  
  33. Scanner myScanner=new Scanner(System.in);
  34.  
  35. int i,ck;
  36.  
  37. System.out.print("Enter the number : ");
  38.  
  39. int n=myScanner.nextInt();
  40.  
  41.  
  42.  
  43.  
  44.  
  45. System.out.print("Armstrong numbers between 1 to "+n+" : ");
  46.  
  47. for(i=1;i<=n;i++){
  48.  
  49. ck=armstrong(i);
  50.  
  51. if(ck==i){
  52.  
  53. System.out.print(i+"\t");
  54.  
  55. }
  56.  
  57. }
  58.  
  59. System.out.println();
  60.  
  61. }
  62.  
  63.  
  64. }

No comments:

Post a Comment