Skip to main content

How to code if two numbers are co-prime or not?

 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;
}

Input & Output:




Comments

Popular posts from this blog

10 Tips for Writing Faster and More Efficient Code

 10 Tips for Writing Faster and More Efficient Code Writing efficient code may seem like an impossible task when you’re just starting out with your programming journey, but the truth is that there are several steps you can take to make your code run faster and use fewer resources. You don’t have to be some sort of genius or even have years of experience to achieve this – here are 10 simple tips for writing more efficient code! 1) Use Type Alias When you need to define your own data type, consider using a type alias. This saves you from having to repeatedly reference more verbose names like String or NSNumber. Using a type alias also helps reduce pollution of existing types, which can cause problems later on. For example, adding an int property on top of NSObject will break a lot of code that uses that class without changing its definition. 2) Use Option Auto or Type Keywords Most developers have different preferences, but it is important to use Option Auto or Type Keywords. This wi...

C code to calculate product of even digits of a given number

 C code to calculate product of even digits of a given number Code Steps: 1. First input the number 2. Now initialize a variable =1, that stores the multiplication  3. Now start a while loop until number = 0 4. Inside while loop just find out all the digits by finding the remainder & check it if it is divisible by 2  5. If the number is divisible by 2 then multiply that number with the variable that was initialize = 1 6. At last return the multiplication storing variable.  C Code Snippet #include <stdio.h> int main (){     int num ;     int product = 1 ;     printf ( "Enter the number: " );     scanf ( " %d " , & num );     int temp = num ;     while ( num != 0 ){                 int digit = num % 10 ;         num = num / 10 ;         if ( digit % 2 == 0 ){         ...