Aim:
To write a java program to implement Adam Number and Amstrong Number. T
application should get a Boolean input. If Boolean value is true. Adam Number should be
calculated else Amstrong numbers should be calculated.
Algorithm:
- Find square of a number.
- Find reverse of a number.
- Find the square of the number in Step 2.
- Find reverse of a number in Step 3.
- Find whether the number obtained in Step 4 and Step 1 are equal or not.
Program:
import java.util.*; class Main { public static void main(String []args) { Scanner put=new Scanner(System.in); System.out.println("do u want calculate ADAM NUMBER or AMSTRONG NUMBER(0/1)"); int ch=put.nextInt(); if(ch==0) { System.out.println("ENTER THE N VALUE"); int n=put.nextInt(); System.out.println("ADAM NUMBER"); int count=0,i=0; while(i<n) { int sq=count*count; int revsq=reverseDigits(count); revsq=revsq*revsq; int revs=reverseDigits(revsq); if(sq==revs) { System.out.println(count); i++; } count++; } } else { System.out.println("ENTER THE N VALUE"); int n=put.nextInt(); System.out.println("AMSTRONG NUMBER"); int count=0,i=0; while(i<n) { if(count==ams(count)) { System.out.println(count); i++; } count++; } } } static int reverseDigits(int num) { int rev = 0; while (num > 0) { rev = rev * 10 + num % 10; num /= 10; } return rev; } static int ams(int num) { int rev = 0; int res=0; while (num > 0) { rev = num % 10; res=res+rev*rev*rev; num /= 10; } return res; } }
Execution:
OUTPUT : FOR ADAM NUMBER: do u want calculate ADAM NUMBER or AMSTRONG NUMBER (0/1) 0 ENTER THE N VALUE 5 ADAM NUMBER 0 1 2 3 11 FOR AMSTRONG NUMBER: do u want calculate ADAM NUMBER or AMSTRONG NUMBER (0/1) 1 ENTER THE N VALUE 5 AMSTRONG NUMBER 0 1 153 370 371