Finding the greatest common factor and least common multiple of two numbers

Hey, I am currently trying to write a program for school that calculates the greatest common factor of two positive integers. I am having the most trouble finding out how to have the computer find the Greatest common factor and the least common multiple. I also just realized that if I put in a letter instead of a number the program keeps returning to the top endlessly and glitches out. Can you guys please help me solve these two problems?

So far I have it so that it asks for the 2 numbers and if you put in a negative integer it will return to the top and ask for the positive integers again.

This is what I have so far
I have the program ending just as a placeholder right now, that's not truly the end.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
//This program calculates the GCF of two positive integers.
//Developer:

#include <iostream>
using namespace std;

int main()
{
    //Variables
    //x and y represent the 2 positive integers.
    int x=0;
    int y=0;
    while ((x<=0||y<=0))
    {
          cout << "This program calculates the GCF of two positive integers. \n";
    
          //User imputs integers.
          cout << "Please enter the first POSITIVE integer. \n";
          cin >> x;
          cout << "Please enter the second POSITIVE integer. \n";
          cin >> y;
    
          //Computer checks integers to see if they are positive.
          //If not it will return to line 
          if ((x<0)||(y<0)) {
          cout << "Cannot use negative intigers. \n";
          }
    }
    system ("pause");
    return 0;
    
}


I am very new to c++ so if you see anything that I can clean up please let me know.

-Zach-
Last edited on
Check this site: http://www.dreamincode.net/code/snippet993.htm

Hope it HELPS!!!
Unfortunately this did not help me. after adding what I was missing from the program you showed me it still works the same. I doesn't find the GCF nor does it not glitch out when I enter a letter(I am entering letters because my teacher wants us to make it so that anyone can misuse it and not break it). I also have another problem now. He wants me to make it so that it will find the greatest common factor and the least common multiple. Thank you for trying to help though Aceix.
Yeah, if you wanna enter letters so it doesn't break, either you check for failbit or use strings.

Eg:
cout«".......";
cin»str;
stringstream(str)»x;

Where:
str is a string, and x is an int.
Ypu also have to #include <sstream> for the stringstream.

Sorry I cannot type more cause I'm not on my PC...
Okay, I have found out how to check both numbers as you enter them, and i have it properly finding greatest common factor now. All i have left is to make it also find least common multiple at the same time. Turns out i don't have to prevent the program from breaking if you put in a letter or decimal just yet. That will be my next assignment.

Here is my code for what I have so far:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
//This program calculates the GCF and LCM of two positive integers.
//Developer:

#include <iostream>
using namespace std;

int main()
{
    //Variables
    //x and y represent the 2 positive integers.
    int x=0;
    int y=0;
    int temp;
    
    cout << "This program calculates the GCF of two positive integers. \n";

    while ((x<=0)) {
    
          //User imputs x integer.
          cout << "Please enter the first POSITIVE integer. \n";
          cin >> x;

          if ((x<0)) {
                          cout << "Cannot use negative integers \n";
          }
          else {
               cout << "You entered: \n";
               cout << x << endl;
          }
          
    }
    
        while ((y<=0)) {
    
          //User imputs y integer.
          cout << "Please enter the second POSITIVE integer. \n";
          cin >> y;

          if ((y<0)) {
                          cout << "Cannot use negative integers \n";
          }
          else {
               cout << "You entered: \n";
               cout << y << endl;
          }
          
    }
    

    //Computer calculates GCF
    while( y!= 0) {
           temp = x % y;
           x = y;
           y = temp;
    }

    //The computer displays the GCF
    cout << "The greatest common factor is... \n";
    cout << x << endl;
    
    system ("pause");
    return 0;
    
}

Next I have to make it find LCM so if anyone can help that would be great.
Last edited on
Topic archived. No new replies allowed.