» » Check armstrong number or not


  1. package com.instanceofjavaTutorial; 
  2. import java.util.Scanner;

  3. public class ArmstrongNumber{
  4.  
  5. public static void main(String args[])
  6.  {
  7.  
  8.           int n, sum = 0, temp, r;
  9.  
  10.           Scanner in = new Scanner(System.in);
  11.  
  12.           System.out.println("Enter a number to check if it is an Armstrong number or not");     
  13.           n = in.nextInt();
  14.  
  15.           temp = n;
  16.           while( temp != 0 )
  17.           {
  18.  
  19.              r = temp%10;
  20.              sum = sum + r*r*r;
  21.              temp = temp/10; 
  22.  
  23.           }
  24.  
  25.           if ( n == sum )
  26.              System.out.println(n+"is an Armstrong number.");
  27.           else
  28.              System.out.println(n+"  is not an Armstrong number.");  
  29.        
  30.        }
  31. }


Output:

  1. Enter a number to check if it is an Armstrong number or not
  2. 153
  3. 153is an Armstrong number.



Instance Of Java

We will help you in learning.Please leave your comments and suggestions in comment section. if you any doubts please use search box provided right side. Search there for answers thank you.
«
Next
Newer Post
»
Previous
Older Post

No comments

Leave a Reply

Select Menu