C Program to Find GCD of both Positive and Negative Numbers

@codevarsity
 · 
2.1k views
 · 

A simple C program to calculate GCD of two integers (for both positive and negative integers) using while statement.

Code

#include <stdio.h>
int main()
{
    int n1, n2;

    printf("Enter two integers: ");
    scanf("%d %d",&n1,&n2);

    // if user enters negative number, sign of the number is changed to positive
    n1 = ( n1 > 0) ? n1 : -n1;
    n2 = ( n2 > 0) ? n2 : -n2;

    while(n1!=n2)
    {
        if(n1 > n2)
            n1 -= n2;
        else
            n2 -= n1;
    }
    printf("GCD = %d",n1);

    return 0;
}

Output

Enter two integers: 81
-153
GCD = 9

Comments