I've just started learning C++ and I need to finda way to add, subtract, multi, and divide fractions. Any help would be great!
Printable View
I've just started learning C++ and I need to finda way to add, subtract, multi, and divide fractions. Any help would be great!
well...I'm not good myself but...you need to have different variables for the numbers and then the operator...for example, you can initialize two integer variables:
(Note: you must type #include <iostream.h> at the top of your cpp file)
int num1 = 0;
int num2 = 0;
next you decide on an operator...say multiplication.
cin>>num1; /*that's the code to get a value for the first
variable from the user*/
cin>>num2; /*that's the same thing for variable num2*/
//now to display all the information... cout<< is ConsoleOUT
//anytime something is in quotes " " they are literal
cout<<num1<<" * "<<num2<<" = "<<num1*num2<<endl;
/*in that last bit, we basically said...show num1, the * symbol, num2, the = symbol, and the answer to the multiplication question*/
Try this:
Code:float x = 4.25;
float y = 1.165;
cout << (2*x)+(y/3);
Thanks alot for the help i think i got it.
You better get some tutorial on what are INT/FLOAT/DOUBLE .. then you'll be able to use them. Hint: Float and double are real numbers, but float is a little different. It's got a FLOATING POINT. :)
Now if you want to do this:
(1/2) + (1/3) = 5/6
in fraction form, not using decimals, like we all did in arithmetic, that's another kettle of fish that you can try when you get to classes and overloading operators, etc.