📅  最后修改于: 2022-03-11 15:04:42.228000             🧑  作者: Mango
/*
Using While loop
The following code assigns to the variable ‘max’ the number that’s the largest between num1 and num2.
*/
res = (x > y) ? x : y;
/* Code */
#include
int main() {
int x, y, res;
printf("Enter two positive integers: ");
scanf("%d %d", &x, &y);
res = (x > y) ? x : y;
while (1) {
if (res % x == 0 && res % y == 0) {
printf("The LCM obtained from %d and %d is %d.", x, y, res);
break;
}
++res;
}
return 0;
}
/* Output
Enter two positive integers: 7
35
The LCM obtained from 7 and 35 is 35.
*/
/*
Function
The same algorithm is followed for the while loop as in the lcm program in c using while, except this time, we have a function call as:
*/
lcm = findLCM(x, y);
/* Code */
#include
int findLCM(int x, int y, int res);
int main()
{
int x, y, res;
printf (" \n Enter two positive integers: ");
scanf ("%d %d", &x, &y);
res = (x > y) ? x : y;
int lcm=findLCM(x,y,res);
printf ( " \n The LCM obtained from %d and %d is %d. ", x, y, res);
return 0;
}
int findLCM ( int x, int y, int res)
{
while (1) {
if (res % x == 0 && res % y == 0) {
return res;
break;
}
else{++res;}
}
}
/* Output */
Enter two positive integers: 7
35
The LCM obtained from 7 and 35 is 35.