Tuesday, 19 February 2013

GCD-LCM-of-2-INTEGERS



/*  Program to print GCD and LCM of two integers */



# include <stdio.h>
int main()
{
    int n1, n2, gcd, lcm, rem, numerator, denominator;
 
    printf("www.techterabyte.com\n");
    // enter two integer values
    printf("Enter two integers: ");
    scanf("%d %d", &n1, &n2);
 
    // comparing both integers
    if(n1>n2) {
       numerator =  n1;
       denominator = n2;
    }
    else {
       numerator = n2;
       denominator = n1;
    }

    // find remainder
    rem = n1%n2;

    while(rem!=0) {
       numerator = denominator;
       denominator = rem;
       rem = numerator % denominator;
    }

    gcd = denominator;
    lcm = (n1*n2)/gcd;

    // display GCD and LCM
    printf("\nGreatest Common Divisor...\n");
    printf("GCD of %d and %d = %d\n", n1, n2, gcd);
    printf("Least Common Multiple...\n");
    printf("LCM of %d and %d = %d\n", n1, n2, lcm);
    return 0;
}



Output:
www.techterabyte.com
Enter two integers: 120 30
Greatest Common Divisor...
GCD of 120 and 30 = 30
Least Common Multiple...
LCM of 120 and 30 = 120





No comments:

Post a Comment