SCHOOL OF CODE BUILDERS
Learn To CODE. Become A DEVELOPER.
Pages
HOME
DATA STRUCTURES
STRINGS
ARRAYS
MATRIX
BINARY TREES
LINKED LIST
STACK
QUEUE
SORTING
SEARCHING
C
PYTHON
PSEUDOCODE
CONTEST PROBLEMS
ALGORITHMS
PATTERNS
PHP
C PUZZLES
C INTERVIEW QUESTIONS
JAVA
C++
HASHING
RECURSION
BASIC C PROGRAMS
TCS-CODEVITA
FACEBOOK
CONTACT US
Java program amicable numbers
Java program amicable numbers
Output:
Enter two numbers to check they are amicable or not:
220
284
220 and 284 are amicable numbers
Implementation:
/* Amicable numbers are the numbers which has their sum of divisors equal to one another For example: consider the numbers a=220 and b=284 the divisors of 220 are: 1 2 4 5 10 11 20 22 44 55 110 the divisors of 284 are: 1 2 4 71 142 Now, adding the divisors of 220 1+2+4+5+10+11+20+22+44+55+110 = 284 , which is equal to number b=284 Now, adding the divisors of 284 1+2+4+71+142 = 220, which is equal to number a=220 Hence, the numbers 220 and 284 are amicable numbers */ import java.util.*; import java.lang.*; import java.io.*; class CodeRegister { public static void main (String[] args) throws java.lang.Exception { Scanner in = new Scanner(System.in); System.out.println("Enter two numbers to check they are amicable or not:"); int number1 = in.nextInt(); int number2 = in.nextInt(); int suma=0,sumb=0; for(int i=1;i<=(number1/2);i++) { if(number1%i==0) { suma += i; } } for(int i=1;i<=(number2/2);i++) { if(number2%i==0) { sumb += i; } } if(suma == number2 && sumb == number1) { System.out.println(number1+" and "+number2+" are amicable numbers"); } else { System.out.println(number1+" and "+number2+" are not amicable numbers"); } } }
PREVIOUS
NEXT
HOME