0

Amicable number please provide me solution for this my code is partially correct when I'm giving 6 6 then it's correct ans.

import java.util.Scanner; public class Program { public static void main(String[] args) { int x,y,i,sum=0; Scanner s=new Scanner(System.in); System.out.println("Enter two Numbers"); x=s.nextInt(); y=s.nextInt(); for(i=1;i<x;i++) { if(x%i==0) sum+=i; } if(sum==y) { sum=0; for(i=1;i<y;i++) { if(y%i==0) sum+=i; } } if(sum==x) System.out.println(x+" And "+y+" Are Amicable numbers...") ; else System.out.println("not Amicable numbers..."); } } /* Amicable numbers are two different numbers so related that the sum of the proper divisors of each is equal to the other number.

21st Feb 2020, 3:48 PM
Ankit Shastri
Ankit Shastri - avatar
3 Réponses
+ 1
Ankit Shastri WHY wrong? Can you explain? Is numbers x, y should not be same? IF Yes, then only it's wrong..
22nd Feb 2020, 5:28 AM
Jayakrishna 🇮🇳
0
Edit: Ankit Shastri Your code working perfectly.. Check for input 220 284 220 factors sum is 284. 284 factors sum is 220. For your correctness checking, display sum then observe output for clarity of any numbers....
21st Feb 2020, 4:04 PM
Jayakrishna 🇮🇳
0
Jayakrishna bro when you give input 6,6 then it prints amicable number. So it's wrong yr
22nd Feb 2020, 12:51 AM
Ankit Shastri
Ankit Shastri - avatar