C Program to check if the given two numbers are friendly pair or not
Login/Signup to comment
7 comments on “C Program to check if the given two numbers are friendly pair or not”
×
30+ Companies are Hiring
Get Hiring Updates right in your inbox from PrepInsta
//programm for friendly no. or not:-
#include
int main(){
int p,q,sum1=0,sum2=0,i,n1,t1,n2,t2;
printf(“enter first no.”);
scanf(“%d”,&n1);
printf(“enter second number:”);
scanf(“%d”,&n2);
t1=n1;
t2=n2;
for(i=1;i<n1;i++){
if(n1%i==0){
sum1=sum1+i;
}
}
p=sum1/t1;
for(i=1;i<n2;i++){
if(n2%i==0){
sum2=sum2+i;
}
}
q=sum2/t2;
if(p==q)
printf("friendly pair number");
else
printf("not friendly pair number");
return 0;
}
Simple code using functions:
#include
int abundant(int num)
{
int i,temp,sum=0;
temp=num;
for(i=1;i<temp;i++)
{
if(temp%i==0)
sum+=i;
}
return sum/num;
}
int main()
{
//enter value
int num1,num2;
// ai1 – abundant index of num1 and ai2 for num2
int ai1,ai2;
printf("Enter 2 number\n");
scanf("%d\n%d",&num1,&num2);
ai1=abundant(num1);
ai2=abundant(num2);
//checking condition
if(ai1==ai2)
printf("friendly pair");
else
printf("Not friendly pair");
return 0;
}
#include
void main()
{
int i, n1, n2, sum1=0, sum2=0;
scanf(“%d %d”, &n1, &n2);
for(i=1; i<n1; i++){
if(n1%i==0){
sum1+=i;
}
}
for(i=1; i<n2; i++){
if(n2%i==0){
sum2+=i;
}
}
if(sum1==n2 && sum2==n1)
printf("Friendly Pair");
else
printf("Not Friendly");
}
Is it correct? If it’s not, please help me to correct it.
#include
Int sum_of_divisors(int n);
int main(){
int n1,n2,sum1,sum2;
printf(“Enter two numbers:”);
scanf(“%d%d”, &n1,&n2);
sum1=sum_of_divisors(n1);
sum2=sum_of_divisors(n2);
if(sum1/n1==sum2/n2){
printf(“%d and %d are amicable”, n1,n2);
}else{
printf(“%d and %d are not amicable”, n1,n2);
}
return 0;
}
int sum_of_divisors(int n){
int i, sum=0;
for(i=1;i<=n;i++){
if(n%i==0){
sum=sum+i;
}
}
return sum;
}
/******************************************************************************
Welcome to GDB Online.
GDB online is an online compiler and debugger tool for C, C++, Python, PHP, Ruby,
C#, VB, Perl, Swift, Prolog, Javascript, Pascal, HTML, CSS, JS
Code, Compile, Run and Debug online from anywhere in world.
*******************************************************************************/
#include
int main()
{
int a,b,i,j,k=0,k2=0,arr[100],arr2[100],sum=0,sum2=0;
scanf(“%d %d”,&a,&b);
for(i=1;i<=a;i++)
{
if(a%i==0)
{
arr[k]=i;
k++;
}
}
for(j=1;j<=b;j++)
{
if(b%j==0)
{
arr2[k2]=j;
k2++;
}
}
for(i=0;i<k;i++){
sum=sum+arr[i];
}
for(i=0;i<k2;i++){
sum2=sum2+arr2[i];
}
if((sum/a)==(sum2/b))
{
printf("friendly");
}
else
printf("not");
}
Above code is incomplete.
#include
int main()
{
//1 Create two variables to use in first and second numbers
int i;
int f_Num,s_Num;
//2 two more variables created to store the sum of the divisors
int f_DivisorSum = 0;
int s_DivisorSum = 0;
//3 Asking user to enter the two numbers
printf(“Enter two numbers to check if Amicable or not : “);
scanf(“%d %d”,&f_Num,&s_Num);
//4 Using one variable for loop and second to check for each number
for(int i=1;i<f_Num;i++)
{
//5 Condition check
if(f_Num % i == 0)
f_DivisorSum = f_DivisorSum + i;
}
//6 Calculating the sum of all divisors
for(int i=1;i<s_Num;i++)
{
if(s_Num % i == 0)
s_DivisorSum = s_DivisorSum + i;
}
int fai=(f_DivisorSum/f_Num);
int sai=(s_DivisorSum/s_Num);
//7 Check condition for friendly numbers
if(fai==sai)
{
printf("%d and %d are Amicable numbers\n",f_Num,s_Num);
}
else
{
printf("%d and %d are not Amicable numbers\n",f_Num,s_Num);
}
return 0;
}
The smallest pair of amicable numbers is (220, 284). They are amicable because the sum of proper divisors of 220 (1, 2, 4, 5, 10, 11, 20, 22, 44, 55 and 110) is 284; and the sum of proper divisors of 284 (1, 2, 4, 71 and 142) is 220.
the above program is correct, except in the if statement …..after the if statement there should be printf function. that’s it.
the above explanation is wrong, 6 and 28 are not Amaicable numbers.
Amaicable numbers have the sum of the proper divisors of each is equal to the other number.