Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 135,947 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,729 people online right now. Registration is fast and FREE... Join Now!




Unable to find the LCM

 
Reply to this topicStart new topic

Unable to find the LCM

arslan_09
20 May, 2008 - 09:20 AM
Post #1

New D.I.C Head
*

Joined: 14 Apr, 2008
Posts: 49


My Contributions
Write a C++ program which takes two integers as input and finds out the least common multiple (LCM) of both of them. LCM is the smallest integer which is divisible by both numbers. You are NOT allowed to use any if or switch statement in the whole program.

CODE
#include <iostream>
using namespace std;
int main()
{
    int x,y,z;
    cout<<"Enter First Integer:";
    cin>>x;
    cout<<"\nEnter Second Integer:";
    cin>>y;
    z=1;
    while(x/z==1 && y/z==1)
    {
        z=x/y;
        cout<<"The LCM of Both Numbers is:"<<z;
    }
    return 0;
}


well there is no error in running program but i cannot find the LCM. Anyone could help me where is the code missing or wrong.

thanks in advance.
User is offlineProfile CardPM
+Quote Post

Cerolobo
RE: Unable To Find The LCM
20 May, 2008 - 09:26 AM
Post #2

D.I.C Regular
Group Icon

Joined: 5 Apr, 2008
Posts: 440



Thanked: 31 times
My Contributions
CODE
while(x/z==1 && y/z==1)


Lets see....

Assuming x is 10, and y is 3

while(10/1 == 1 && 3 / 1 == 1)

10/1 = 10, so the first part is false.
3/1 = 3, so the second part is false as well.

The only way you will enter the while loop, is if x and y are 1.
User is offlineProfile CardPM
+Quote Post

arslan_09
RE: Unable To Find The LCM
20 May, 2008 - 09:30 AM
Post #3

New D.I.C Head
*

Joined: 14 Apr, 2008
Posts: 49


My Contributions
QUOTE(Cerolobo @ 20 May, 2008 - 10:26 AM) *

CODE
while(x/z==1 && y/z==1)


Lets see....

Assuming x is 10, and y is 3

while(10/1 == 1 && 3 / 1 == 1)

10/1 = 10, so the first part is false.
3/1 = 3, so the second part is false as well.

The only way you will enter the while loop, is if x and y are 1.



could you please correct it.
User is offlineProfile CardPM
+Quote Post

Cerolobo
RE: Unable To Find The LCM
20 May, 2008 - 09:42 AM
Post #4

D.I.C Regular
Group Icon

Joined: 5 Apr, 2008
Posts: 440



Thanked: 31 times
My Contributions
No... If you don't understand basic math and basic program flow, then correcting the above code will not help you in any way, and we are not here to do your homework.
User is offlineProfile CardPM
+Quote Post

arslan_09
RE: Unable To Find The LCM
20 May, 2008 - 09:46 AM
Post #5

New D.I.C Head
*

Joined: 14 Apr, 2008
Posts: 49


My Contributions
QUOTE(Cerolobo @ 20 May, 2008 - 10:42 AM) *

No... If you don't understand basic math and basic program flow, then correcting the above code will not help you in any way, and we are not here to do your homework.


mate i have tried myself and give you the code so if you know how to solve the problem then you should have to do so.

if not then just give me any idea of how to do that.
User is offlineProfile CardPM
+Quote Post

Cerolobo
RE: Unable To Find The LCM
20 May, 2008 - 10:01 AM
Post #6

D.I.C Regular
Group Icon

Joined: 5 Apr, 2008
Posts: 440



Thanked: 31 times
My Contributions
QUOTE(arslan_09 @ 20 May, 2008 - 10:46 AM) *
if you know how to solve the problem then you should have to do so.

The people on this forum are here by choice and choice alone. We do not "have to do so".

QUOTE
if not then just give me any idea of how to do that.

Looking at your code, You seem to be confused about what a LCM is.

Take the numbers 5 and 2. The LCM is 10. The easy way to do this is to just apply scalars to x and y, until you find a number that matches.

Other methods include find the prime factorization of the number; however, that will be far harder to do with out any if statements.
User is offlineProfile CardPM
+Quote Post

arslan_09
RE: Unable To Find The LCM
20 May, 2008 - 10:12 AM
Post #7

New D.I.C Head
*

Joined: 14 Apr, 2008
Posts: 49


My Contributions
QUOTE(Cerolobo @ 20 May, 2008 - 11:01 AM) *

QUOTE(arslan_09 @ 20 May, 2008 - 10:46 AM) *
if you know how to solve the problem then you should have to do so.

The people on this forum are here by choice and choice alone. We do not "have to do so".

QUOTE
if not then just give me any idea of how to do that.

Looking at your code, You seem to be confused about what a LCM is.

Take the numbers 5 and 2. The LCM is 10. The easy way to do this is to just apply scalars to x and y, until you find a number that matches.

Other methods include find the prime factorization of the number; however, that will be far harder to do with out any if statements.

\
thanks for your help.
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/1/08 08:50AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month