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.
//This program calculates the GCF of two positive integers.
//Developer:
#include <iostream>
usingnamespace 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.
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.
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.
//This program calculates the GCF and LCM of two positive integers.
//Developer:
#include <iostream>
usingnamespace 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.