C code for two numbers to check if they are co-prime or not
Question: How can we understand if two numbers are coprime or not?
Ans: A Prime Number is defined as a Number which has no factor other than 1 and itself. But, Co-prime Numbers are Considered in pairs and two Numbers are Co-prime if they have a Common factor as 1 only. Their HCF is 1.
Now come to the main point that is how to code it in C language so that we can get the correct output.
C code snippet:
#include<stdio.h>
int main (){
int num1,num2,hcf;
printf("Enter the first number: ");
scanf("%d",&num1);
printf("Enter the second number: ");
scanf("%d",&num2);
for(int i=1;i<=num1;i++)
{
if(num1%i==0 && num2%i==0)
{
hcf = i;
}
}
if(hcf == 1)
{
printf("%d and %d are CO-PRIME NUMBERS.", num1, num2);
}
else
{
printf("%d and %d are NOT CO-PRIME NUMBERS.", num1, num2);
}
return 0;
}
Comments
Post a Comment