C Program to find amicable pair numbers below 1000 - c programs
//C Program to find amicable pair numbers below 1000
// what are amicable numbers?
// Two numbers are amicable if the sum of the proper divisors of each is equal to the other number,
// Example of amicable pair: 220 and 284
#include <stdio.h>
void main()
{
long int test, chk, div, sum, n1, n2;
test = 0;
printf("Amicable pairs are");
while ( ++test < 1000 )
{
sum = div = 0;
while ( ++div <= test/2 )
{
if ( test % div == 0 )
sum += div;
}
chk = sum;
sum = div = 0;
while ( ++div <= chk/2 )
{
if ( chk % div == 0 )
sum += div;
}
if ( sum == test )
{
if ( test == chk )
continue;
n1 = test;
if ( n1 == n2)
continue;
n2 = chk;
printf("%d\t%d\n", n1, n2);
} }
// what are amicable numbers?
// Two numbers are amicable if the sum of the proper divisors of each is equal to the other number,
// Example of amicable pair: 220 and 284
#include <stdio.h>
void main()
{
long int test, chk, div, sum, n1, n2;
test = 0;
printf("Amicable pairs are");
while ( ++test < 1000 )
{
sum = div = 0;
while ( ++div <= test/2 )
{
if ( test % div == 0 )
sum += div;
}
chk = sum;
sum = div = 0;
while ( ++div <= chk/2 )
{
if ( chk % div == 0 )
sum += div;
}
if ( sum == test )
{
if ( test == chk )
continue;
n1 = test;
if ( n1 == n2)
continue;
n2 = chk;
printf("%d\t%d\n", n1, n2);
} }
could you please explain the code, how it works??
ReplyDeletesuperb dear
ReplyDelete