Finding the lcm of 2 nums using c [Solved] | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
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 Answers
+ 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