LCM 2 liczb w C
/*
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 <stdio.h>
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 <stdio.h>
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.
ShrutiBongale