Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   Check for co-primes (http://www.go4expert.com/forums/check-co-primes-t630/)

 meghna 14Mar2006 10:20

Check for co-primes

Write a 'C' Program to Read two numbers and check whether they are Co-Primes or not

 coderzone 14Mar2006 11:51

Re: Check for co-primes

Simple. Just find the HCF between the 2 Nos and if its anything other than 1 or -1 then they are co-primes. Isnt finding HCF a simple logic.

Re: Check for co-primes

In mathematics, the integers a and b are said to be coprime or relatively prime if they have no common factor other than 1 and −1, or equivalently, if their greatest common divisor is 1.

In the program below, I've used the gcf() function to check whether the GCF of the two numbers is 1.

Code: cpp

`#include <stdio.h>#include <conio.h>int gcf( int n , int m ) ;int lcm( int n , int m ) ; int main(){    int a , b ;    clrscr();    printf( "Enter two integers\n" ) ;    scanf( "%d" , &a ) ;    scanf( "%d" , &b ) ;    if(gcf(a,b)==1)    {        printf("The numbers are co primes");    }    else    {        printf("The numbers are not co primes");    }    getch();    return 0 ;} /*gcfCalculates the gcf (greatest common factor) for a pairof integers.  If either number is zero, zero is returned.Negative inputs are treated as if they were positive.*/int gcf( int n , int m ){    if( n == 0 || m == 0 )        return 0 ;    if( n < 0 )         n = - n ;    if( m < 0 )         m = - m ;    /* subtract the larger from the smaller until they are equal */    while( 1 )         if( n > m )            n -= m ;        else if ( n < m )            m -= n ;        else            break ;    return n ;}`

 All times are GMT +5.5. The time now is 21:52.