Learn to Code via Tutorials on Repl.it!

← Back to all posts
Fraction Simplifier [TUT] [C++] [Python!!]
Whippingdot

Hello Hello, it is I, the famous Whippingdot!! Thank you for the roses, thank you. Anyway, this is a tutorial on creating a fraction simplifier. This is going to be a three four(maybe) part series of creating calculators. It gets harder and longer as time goes but...who cares. We will conquer all calculators. BTW I will remove the '[Python!!]' from the title if you want me to, as it is not a Python tutorial, but a C++(I put that Python thing there cause more people wud look at this 🤓🤓). This was supposed to be a four-part series of python calculators but @elipie created a hate post on python tutorials. Should we start...I think we should 😁😁. Also, if you didn't know, this is a tutorial on creating a fraction simplifier in C++. A fraction simplifier is a program that finds the most simplified form of a fraction you input.

Note: One thing I forget to say later is that in C++ you end almost all executable lines with a semi-colon(this excludes function's end curly parenthesis, #include, etc.)

Prerequisites: You should know what a fraction is and what a numerator is(the top of a fraction) and what a denominator is(the bottom of a fraction)

NOICE STOWART

Let us start this. Ok, first, look at the program. We are going to run along each line of it. I would just tell you the meaning of each line, but that wouldn't be a great tutorial would it. So let us start from scratch. I actually created this calculator in Python first but recently(a few mins ago) I converted it to C++(it was easy). I hope the people who came for the Python tag stayed cause C++ is similar to Python so you will understand this.

JUS STOORT ALRAEDY

EEEUUU(BOX)STEEM

Ok, first, at the start of C++ programs in which we need to 'print' something out or take an 'input' I always put `#include <iostream>`. What this does is it includes the 'iostream' library. 'iostream' stands for 'input-output stream'. Using this library we can now use the commands 'cout' and 'cin' which mean 'c output' and 'c input' respectively. C++ is derived from C(a programming language) and is basically C but enhanced(some people would say bloated) so that is why it is 'c output' and 'c input'. There are other things you can include to get different ways to input and output but I use 'iostream' cause I think it is the easiest way.

UBING NOMESPOCE YES TEE DEE SEBI-CODON

I used to put `using namespace std;` at the start of my program too but I stopped doing it when someone on repl.it told me not to use it. I don't exactly remember their reason why but on the internet the reason is:

Some people had said that is a bad practice to include the using namespace std in your source files because you're invoking from that namespace all the functions and variables. When you would like to define a new function with the same name as another function contained in the namespace std you would overload the function and it could produce problems due to compile or execute. It will not compile or executing as you expect.

This basically means that if you put `using namespace std;` at the start, it would include all functions from the namespace meaning if you create a function in your program with the wrong name(meaning a name which is defined in the namespace) even by mistake the program will have the output you did not expect. It might use the function from the namespace and not the function your created meaning your whole program goes 'kaboom'.

The replacement we use for `using namespace std;`(cause we do need some functions from it) is that we only put `using std::functionname;` for each function. If we need the cout function(which is in the namespace) we would put `using std::cout;` in the program. This is what we did for lines 3-4:

```.css-o0gntx{position:absolute;left:-9999px;}.css-4kcfpn{-webkit-align-items:stretch;-webkit-box-align:stretch;-ms-flex-align:stretch;align-items:stretch;border-width:0;border-style:solid;box-sizing:border-box;display:-webkit-box;display:-webkit-flex;display:-ms-flexbox;display:flex;-webkit-flex-basis:auto;-ms-flex-preferred-size:auto;flex-basis:auto;-webkit-flex-direction:column;-ms-flex-direction:column;flex-direction:column;-webkit-flex-shrink:0;-ms-flex-negative:0;flex-shrink:0;outline:none;min-height:0;min-width:0;position:absolute;left:-9999px;}using std::cout;
using std::cin;
using std::endl;```

DEPINITOON POR TOSE TREE FUNCSOONS

cout is the function for outputting(if you couldn't guess) cin is inputting and endl is a function we use in cout at the end of the line. It means 'go to the next line in the console'.

WEAR CUD GEET ELECTRICUTED EXOCUTEED

The place where you put all your code for the program and all is inside the main function. The main function is defined right before the code. In C++ you define functions using the variable type of the function followed by the name with parentheses(for arguments) and curly parentheses'{}'. Inside the curly parentheses is the code so that is usually why people write it like this:

```.css-o0gntx{position:absolute;left:-9999px;}.css-4kcfpn{-webkit-align-items:stretch;-webkit-box-align:stretch;-ms-flex-align:stretch;align-items:stretch;border-width:0;border-style:solid;box-sizing:border-box;display:-webkit-box;display:-webkit-flex;display:-ms-flexbox;display:flex;-webkit-flex-basis:auto;-ms-flex-preferred-size:auto;flex-basis:auto;-webkit-flex-direction:column;-ms-flex-direction:column;flex-direction:column;-webkit-flex-shrink:0;-ms-flex-negative:0;flex-shrink:0;outline:none;min-height:0;min-width:0;position:absolute;left:-9999px;}int main() {
// This is a comment(meaning the stuff in here doesn't run). The code is put in these curly parentheses
}```

but you can also write it like this:

```.css-o0gntx{position:absolute;left:-9999px;}.css-4kcfpn{-webkit-align-items:stretch;-webkit-box-align:stretch;-ms-flex-align:stretch;align-items:stretch;border-width:0;border-style:solid;box-sizing:border-box;display:-webkit-box;display:-webkit-flex;display:-ms-flexbox;display:flex;-webkit-flex-basis:auto;-ms-flex-preferred-size:auto;flex-basis:auto;-webkit-flex-direction:column;-ms-flex-direction:column;flex-direction:column;-webkit-flex-shrink:0;-ms-flex-negative:0;flex-shrink:0;outline:none;min-height:0;min-width:0;position:absolute;left:-9999px;}int main()
{
// Code here
}```

or like this:

`.css-o0gntx{position:absolute;left:-9999px;}.css-4kcfpn{-webkit-align-items:stretch;-webkit-box-align:stretch;-ms-flex-align:stretch;align-items:stretch;border-width:0;border-style:solid;box-sizing:border-box;display:-webkit-box;display:-webkit-flex;display:-ms-flexbox;display:flex;-webkit-flex-basis:auto;-ms-flex-preferred-size:auto;flex-basis:auto;-webkit-flex-direction:column;-ms-flex-direction:column;flex-direction:column;-webkit-flex-shrink:0;-ms-flex-negative:0;flex-shrink:0;outline:none;min-height:0;min-width:0;position:absolute;left:-9999px;}int main() {// Code here}`

People don't really do the last type as that makes everything squished together making it look very bad and you can't really understand the code. I used to use the second method but many people used the first one so I switched.

Also, one thing to note is that arguments in a function are things which the function uses later. When you call a function you have to put arguments in the parentheses. The arguments have to be the same type as defined by the function. The main function does not have to be called as it is called when you run the program. In any IDE you use it is the same, the main function you never call, but it has to be called main. The main function used to need to have arguments in the parentheses but nowadays you don't need to put those arguments.

The code by now:

```.css-o0gntx{position:absolute;left:-9999px;}.css-4kcfpn{-webkit-align-items:stretch;-webkit-box-align:stretch;-ms-flex-align:stretch;align-items:stretch;border-width:0;border-style:solid;box-sizing:border-box;display:-webkit-box;display:-webkit-flex;display:-ms-flexbox;display:flex;-webkit-flex-basis:auto;-ms-flex-preferred-size:auto;flex-basis:auto;-webkit-flex-direction:column;-ms-flex-direction:column;flex-direction:column;-webkit-flex-shrink:0;-ms-flex-negative:0;flex-shrink:0;outline:none;min-height:0;min-width:0;position:absolute;left:-9999px;}#include <iostream>

using std::cout;
using std::cin;
using std::endl;

int main() {

}```

TE CALL OF DUTY COD

Ok, so now inside the main function we have `int hcf = 0;` on the eighth line. The 'int' is the variable type. I forgot to explain this for the main function but it means that the type of the variable is an integer. For the main function, it means the main function returns an integer(that is why at the last line you have `return 0;` which is not needed nowadays, but it is good practice). We are saying the hcf variable is an integer and it is equal to 0. This is for now and it will change with the future code. The next two lines define other variables called 'numerator' and 'denominator' which we will get from the user in the next lines.

TE NOIXT LIONS

Here we have:

```.css-o0gntx{position:absolute;left:-9999px;}.css-4kcfpn{-webkit-align-items:stretch;-webkit-box-align:stretch;-ms-flex-align:stretch;align-items:stretch;border-width:0;border-style:solid;box-sizing:border-box;display:-webkit-box;display:-webkit-flex;display:-ms-flexbox;display:flex;-webkit-flex-basis:auto;-ms-flex-preferred-size:auto;flex-basis:auto;-webkit-flex-direction:column;-ms-flex-direction:column;flex-direction:column;-webkit-flex-shrink:0;-ms-flex-negative:0;flex-shrink:0;outline:none;min-height:0;min-width:0;position:absolute;left:-9999px;}cout << "Enter The Numerator: ";
cin >> numerator;```

We are first outputting to the console with `cout`. The thing that is outputted has to be followed by two less than symbols. There is no meaning to this that I know, and it is just how the cout function was made. We are outputting "Enter The Numerator: " but without using `endl` after as we want to get the input on the same line, not on a new line. The next line has the `cin` which takes an input, and again here we follow it by two greater than symbols from what I know have no meaning. We then put the variable name that we want to have the input stored in, which in this case is the numerator integer variable we defined earlier. `cin` automatically goes to a new line after taking the input so we don't need to use `endl` here too.

After this, we do the same thing but with the denominator so there is not anything to explain.

TE EBEN MOAR NOIST LIONS

Ok so this is WAAAAY more complicated:

```.css-o0gntx{position:absolute;left:-9999px;}.css-4kcfpn{-webkit-align-items:stretch;-webkit-box-align:stretch;-ms-flex-align:stretch;align-items:stretch;border-width:0;border-style:solid;box-sizing:border-box;display:-webkit-box;display:-webkit-flex;display:-ms-flexbox;display:flex;-webkit-flex-basis:auto;-ms-flex-preferred-size:auto;flex-basis:auto;-webkit-flex-direction:column;-ms-flex-direction:column;flex-direction:column;-webkit-flex-shrink:0;-ms-flex-negative:0;flex-shrink:0;outline:none;min-height:0;min-width:0;position:absolute;left:-9999px;}for (int counter = 2; counter <= numerator && counter <= denominator; counter += 1) {
if (numerator % counter == 0 && denominator % counter == 0) {
hcf = counter;
}
}```

So, first, we are using a for loop. What a for loop is, is it does something a specified amount of times or for the number of letters in a word or stuff like that. You can also use a while loop(runs while something is true) here but I prefer a for loop as it means fewer lines. After putting for we have parentheses in which we put the statement. Inside the parentheses, we first create a new variable called counter. We say it is an integer and it is equal to two, and then we put a semi-colon closing that statement. Next, we tell the for loop to run while the variable counter we just created is less than or equal to(<=) numerator and(the double &, '&&') the variable counter(again) is less than or equal to(<=) the denominator. We then have a semi-colon to end that statement and then we have the last statement in the parentheses which is `counter += 1` which means `counter = counter + 1` which basically increases the value of the counter by one each time the for loop runs. The first time though the counter does not get incremented by one. Inside the curly braces for the for loop(the code to be executed while the condition in the for loop is true) we have an if statement. This means 'if' something is true do the code inside it. The if statement starts with if and then has parentheses. Inside the parentheses, there is a statement which means 'if the remainder(% is the remainder operator) of the numerator divided by the counter is 0 and(&&) the remainder of the denominator divided by the counter is 0, do the code inside my curly parentheses'. I hope you understood that. Inside the curly parentheses, it says `hcf = counter;` as to find the hcf(highest common factor) of two numbers you need to find the number which divides both numbers equally, and in this case, it is counter whenever the if statement is true. Now this for loop will continue running until the counter is more than the numerator or the denominator. By this time you would've gotten the highest number which divides both the numerator and denominator equally.

YEND OB 'POG'RAM

Now we have the last few lines:

```.css-o0gntx{position:absolute;left:-9999px;}.css-4kcfpn{-webkit-align-items:stretch;-webkit-box-align:stretch;-ms-flex-align:stretch;align-items:stretch;border-width:0;border-style:solid;box-sizing:border-box;display:-webkit-box;display:-webkit-flex;display:-ms-flexbox;display:flex;-webkit-flex-basis:auto;-ms-flex-preferred-size:auto;flex-basis:auto;-webkit-flex-direction:column;-ms-flex-direction:column;flex-direction:column;-webkit-flex-shrink:0;-ms-flex-negative:0;flex-shrink:0;outline:none;min-height:0;min-width:0;position:absolute;left:-9999px;}if (hcf != 0) {
numerator /= hcf;
denominator /= hcf;
}

cout << "The most simplified form of your fraction is: " << numerator << "/" << denominator << endl;```

This is another 'if' statement underneath the for loop. It says 'if' hcf is not equal to(!=) 0 then do the code in the curly parentheses. We have this if statement as if both numbers do not have a common factor other than one, the hcf will stay 0 as defined earlier as we kept the counter's starting value as 2. If you change the counter's starting value to 1 then we don't need this if statement and we can directly write the code inside of it without this if statement. Some of the code inside of it is `numerator /= hcf;` which means `numerator = numerator / hcf`. The next line is the same thing as this one and so it means the same thing. This is basically dividing the top and bottom of the fraction by the hcf, which results in the simplest form of the fraction.

After that, we have a final cout statement which outputs some text, has less than symbols(that is what you use instead of + which is in Python), has 'numerator' which means it outputs the value of numerator, more less than symbols, a slash so it looks like a fraction, more less than symbols, 'denominator' which outputs the value of the variable 'denominator', more less than symbols, and endl, which ends the line and makes the console go to the next line.

We then have the `return 0;` we talked about earlier and that is all.

EN' COD

```.css-o0gntx{position:absolute;left:-9999px;}.css-4kcfpn{-webkit-align-items:stretch;-webkit-box-align:stretch;-ms-flex-align:stretch;align-items:stretch;border-width:0;border-style:solid;box-sizing:border-box;display:-webkit-box;display:-webkit-flex;display:-ms-flexbox;display:flex;-webkit-flex-basis:auto;-ms-flex-preferred-size:auto;flex-basis:auto;-webkit-flex-direction:column;-ms-flex-direction:column;flex-direction:column;-webkit-flex-shrink:0;-ms-flex-negative:0;flex-shrink:0;outline:none;min-height:0;min-width:0;position:absolute;left:-9999px;}#include <iostream>

using std::cout;
using std::cin;
using std::endl;

int main() {
int hcf = 0;
int numerator = 0;
int denominator = 0;

cout << "Enter The Numerator: ";
cin >> numerator;

cout << "Enter The Denominator: ";
cin >> denominator;

for (int counter = 2; counter <= numerator && counter <= denominator; counter += 1) {
if (numerator % counter == 0 && denominator % counter == 0) {
hcf = counter;
}
}

if (hcf != 0) {
numerator /= hcf;
denominator /= hcf;
}

cout << "The most simplified form of your fraction is: " << numerator << "/" << denominator << endl;

// Not Needed:
return 0;
}```

BOI BOI

Thank you for reading this full post(if you did read it) and I hope I helped you. I hope this helped you learn C++ and made you want to learn more. Hope you guys and galls have a good day. Tell me if there is a mistake in the post, like if I forgot to put a semi-colon in the explaining bits or something. That would be really helpful. I really hope you take an interest in C++ as it is a really mean rude hard irritating furious confusing nice and useful language to learn. Bye bye now!!

BTW i forgot to tell you but i was going to write a post in the share section with a bunch of rubbish for my 200 cycle special but i realized i wud REALLY get hated on so I decided this wud be enough(I was going to do this anyway). See chu loiter.

P.S. @Codemonkey51 please make your program say I am not a spammer just cause I said cycles once twice. Change it ploise, bye!!

Voters
Avakd
Kookiez
FlaminHotValdez
Highwayman
TsunamiOrSumth
programmeruser
zplusfour
Whippingdot
hotnewtop
FlaminHotValdez

C++ is similar to python

Ah yes. Also elipie's rant was on the basic python tutorial, and this is for doing something specific in python that not many people have done before, so yeah.

Whippingdot

Eh people learning C++ is better than people learning Python(almost everyone knows it). Also check out my first meme:
https://imgflip.com/i/4vy9h9 @FlaminHotValdez

zplusfour

NOICE

Whippingdot

Tank o, abso oi em goeeng tvo moik me langage troonlooter wen me oonbanned(I am banned right now, that is why I didn't make a proper program for my cycle special) @zplusfour

zplusfour

FlaminHotValdez

@Whippingdot wait if you're banned how are you commenting?

Whippingdot

👀👀 i am banned from coding mainly(i am also banned from most internet but welp...who cares) @FlaminHotValdez

FlaminHotValdez

@Whippingdot like ur parents don't allow u or u legit have the internet blocked or sumth

Whippingdot

My parents banned me from coding but they said do not use the computer other than for school but i am still using it here cause they didn't completely block the internet @FlaminHotValdez

FlaminHotValdez

@Whippingdot Dude...rip...how long will you be "not allowed" from coding?

Whippingdot

Till my exams are over(it was supposed to be for 4 months but my dad reduced it. It ends next week) @FlaminHotValdez

FlaminHotValdez

@Whippingdot Dude oof.

be a bad boy and do the internet anyway
Whippingdot

lul don't tell my parents @FlaminHotValdez