Write a program to find the LCM and GCD of two numbers using for loop
Answers
Answered by
1
/*
* C program to find the GCD and LCM of two integers using Euclids' algorithm
*/
#include <stdio.h>
void main()
{
int num1, num2, gcd, lcm, remainder, numerator, denominator;
printf("Enter two numbers\n");
scanf("%d %d", &num1, &num2);
if (num1 > num2)
{
numerator = num1;
denominator = num2;
}
else
{
numerator = num2;
denominator = num1;
}
remainder = numerator % denominator;
while (remainder != 0)
{
numerator = denominator;
denominator = remainder;
remainder = numerator % denominator;
}
gcd = denominator;
lcm = num1 * num2 / gcd;
printf("GCD of %d and %d = %d\n", num1, num2, gcd);
printf("LCM of %d and %d = %d\n", num1, num2, lcm);
}
$ cc pgm11.c $ a.out Enter two numbers 30 40 GCD of 30 and 40 = 10 LCM of 30 and 40 = 120
for C programming this is the program
* C program to find the GCD and LCM of two integers using Euclids' algorithm
*/
#include <stdio.h>
void main()
{
int num1, num2, gcd, lcm, remainder, numerator, denominator;
printf("Enter two numbers\n");
scanf("%d %d", &num1, &num2);
if (num1 > num2)
{
numerator = num1;
denominator = num2;
}
else
{
numerator = num2;
denominator = num1;
}
remainder = numerator % denominator;
while (remainder != 0)
{
numerator = denominator;
denominator = remainder;
remainder = numerator % denominator;
}
gcd = denominator;
lcm = num1 * num2 / gcd;
printf("GCD of %d and %d = %d\n", num1, num2, gcd);
printf("LCM of %d and %d = %d\n", num1, num2, lcm);
}
$ cc pgm11.c $ a.out Enter two numbers 30 40 GCD of 30 and 40 = 10 LCM of 30 and 40 = 120
for C programming this is the program
Similar questions