Checking of Amical Pair

Problem Statement
 Two non-negative numbers n1 and n2 are said to constitute an amicable pair if the sum
of factors of n1 including 1 and excluding itself equals n2 and vice-versa. For example
220 and 284 are amicable pairs.
1_____      Write a C function sumFactors which takes a positive number n as its only argu-
             ment and returns the sum of n’s factors including 1 and excluding itself.
2_____       write a c function isamican    which takes two positive numbers n1 and n2 as its
             arguments and returns 1 if they constitute an amicable pair; otherwise, it returns
             0. It is to use the function sumFactors for this purpose.
3_____       Write a C main function which reads two positive numbers m and n prints proper
             messages depending upon whether they are amicable or not.


#include<stdio.h>
int sumFactor(int n1);
int isamican(int n1,int n2);
int main()
{
  int n1,n2,ans;
  printf("enterhte number n1 and n2 resp. \n");
  scanf("%d %d",&n1,&n2);
  ans=isamican(n1,n2);
  if(ans==1)
    {
      printf("these numbers are amicable pair \n");
    }
  else
    {
      printf("these numbers are not amicable pair \n");
    }
  return 0;
}
int isamican(int n1,int n2)
{
  int sum;
  sum=sumFactor(n1);
  if(sum==n2)
    {
      return 1;
    }
  else
    {
      return 0;
    }
}
int sumFactor(int n1)
{
  int sum=1,temp=2;
  while(temp<n1)
    {
      if(n1%temp==0)
{
 sum=sum+temp;
}
      temp++;
    }
  return sum;
}

No comments:

Post a Comment