Finding the lcm of 2 nums using c [Solved] | Sololearn: Learn to code for FREE!
Новый курс! Каждый программист должен знать генеративный ИИ!
Попробуйте бесплатный урок
0

Finding the lcm of 2 nums using c [Solved]

I wanted to make a code in c that gives the lcm of 2 given numbers.But it is not working.Can someone please tell me whats wrong with this code? https://code.sololearn.com/cyIGwQVXHTnA/?ref=app

27th Feb 2021, 5:40 AM
Phantom713
Phantom713 - avatar
3 ответов
+ 2
That is because the value of `a` or the first number is changed to 1 on the loop and it is changing/incremented every loop. So for example: INPUT: 3 4 a = 3 b = 4 Then inside your loop condition, it changed `a` to 1. Then it increments every loop, as a result this will only get the value of b. 🔻=== LOOP 1 ===🔻 a = 1 b = 4 i = 1 🔻=== LOOP 2 ===🔻 a = 2 b = 4 i = 3 🔻=== LOOP 3 ===🔻 a = 3 b = 4 i = 3 🔻=== LOOP 4 ===🔻 a = 4 b = 4 i = 4 🔸Condition is now True since 4%4==0 LCM of 4 and 3 is 4 See, this only gets the value of b which is not the actual LCM. To Solve: 🔹Don't change the value of a. Additional: 🔹Your for loop is basically an infinite loop, you can use while loop instead. https://code.sololearn.com/c3aU2XvRyTiZ/?ref=app
27th Feb 2021, 6:26 AM
noteve
noteve - avatar
+ 1
I think in your lcm function you defined a as 1, while it's not supposed to be lol 😅 Oh, and I have made a code about this! https://code.sololearn.com/ch376aTA0q05/?ref=app Hope it works! Happy programming! :)
27th Feb 2021, 5:51 AM
TheCoder
+ 1
Thank you vey much guys😊.Actually I didnt notice that I already used a in function parameters and so used it again in loop.😁😅 Thank you again
28th Feb 2021, 2:49 AM
Phantom713
Phantom713 - avatar