I'm currently learning C++ using PPP by Bjarne Stroustrup. There is a Practice question:
Write a program that prompts the user to enter two integer values. Store these values in int variables named val1 and val2. Write your program to determine the smaller, larger, sum, difference, product, and ratio of these values and report them to the user.
For ratio I want it to be like The ratio of 500 and 700 is 5:7. I known the answer of this could be
double ratio = 500/700;
But I want to show this as 5:7 rather than 500/700 = 0.714.
How can I do that?
Edit: - That's how I did it.
#include <iostream>
#include <cmath>
void Ratio(double val1, double val2)
{
double temp1 = val1;
double temp2 = val2;
for(int num = 0; num < 20; num++)
{
if(num == 0 || num == 1)
continue;
if(std::fmod(temp1, num) == 0 && std::fmod(temp1, num) == 0)
{
temp1 /= num;
temp1 /= num;
num = 0;
}
}
std::cout<<"Ratio of " << val1 << " and " << val2 << " is " << val1 << ":" << val2<< std::endl;
}
int main()
{
Ratio(500, 700);
}
I'm still a beginner in programming and this really is something for me. Anyone have a better solution for this.