## Python || Simple Math Using Int & Float

This page will display the use of int and float data types. Since python is a flexible programming language, variable data types do not need to be explicitly declared like in C/C++/Java, but they still exist within the grand scheme of things.

==== ADDING TWO NUMBERS TOGETHER ====

To add two numbers together, you will have to first declare your variables by doing something like this.

``` #1 - Adding 2 Numbers Together Python # ============================================================================= # Author: Kenneth Perkins # Date: May 29, 2013 # Updated: Feb 16, 2021 # Taken From: http://programmingnotes.org/ # File: addition.py # Description: Demonstrates adding numbers together # ============================================================================= def main(): # declare variables # NOTE: data types do not need to be declared num1 = 0; num2 = 0; total = 0; # in Python, input is automatically cast as strings # so we convert everything to integers in order to do math num1 = int(input("Please enter the first number: ")) num2 = int(input("Please enter the second number: ")) # calculate the sum of the two numbers here total = num1 + num2 # display data to the screen. The argument list is # similar to that of the "printf" function in C/C++ print("The sum of %d and %d is: %d" % (num1, num2, total)) if __name__ == "__main__": main() # http://programmingnotes.org/ 123456789101112131415161718192021222324252627282930 # =============================================================================#    Author: Kenneth Perkins#    Date:   May 29, 2013#    Updated: Feb 16, 2021#    Taken From: http://programmingnotes.org/#    File:  addition.py#    Description: Demonstrates adding numbers together# =============================================================================def main():    # declare variables    # NOTE: data types do not need to be declared    num1 = 0;    num2 = 0;    total = 0;     # in Python, input is automatically cast as strings    # so we convert everything to integers in order to do math    num1 = int(input("Please enter the first number: "))    num2 = int(input("Please enter the second number: "))     # calculate the sum of the two numbers here    total = num1 + num2     # display data to the screen. The argument list is    # similar to that of the "printf" function in C/C++    print("The sum of %d and %d is: %d" % (num1, num2, total)) if __name__ == "__main__":    main()# http://programmingnotes.org/ ```

Notice in lines 12-14, I declared my variables, giving them a name. You can name your variables anything you want, with a rule of thumb as naming them something meaningful to your code (i.e avoid giving your variables arbitrary names like “x” or “y”). In line 22 the actual math process is taking place, storing the sum of “num1” and “num2” in a variable called “total.” I also initialized my variables to zero. You should always initialize your variables before you use them.

The above code should give you the following output:

```Please enter the first number: 25 Please enter the second number: 1 The sum of 25 and 1 is: 26```

==== SUBTRACTING TWO NUMBERS ====

Subtracting two numbers works the same way as the above code, and we would only need to edit the above code in one place to achieve that. In line 22, replace the addition symbol with a subtraction sign, and you should have something like this:

``` #2 - Subtracting 2 Numbers Together Python # ============================================================================= # Author: Kenneth Perkins # Date: May 29, 2013 # Updated: Feb 16, 2021 # Taken From: http://programmingnotes.org/ # File: subtraction.py # Description: Demonstrates subtracting numbers together # ============================================================================= def main(): # declare variables # NOTE: data types do not need to be declared num1 = 0; num2 = 0; total = 0; # in Python, input is automatically cast as strings # so we convert everything to integers in order to do math num1 = int(input("Please enter the first number: ")) num2 = int(input("Please enter the second number: ")) # calculate the difference of the two numbers here total = num1 - num2 # display data to the screen. print("The difference between %d and %d is: %d" % (num1, num2, total)) if __name__ == "__main__": main() # http://programmingnotes.org/ 1234567891011121314151617181920212223242526272829 # =============================================================================#    Author: Kenneth Perkins#    Date:   May 29, 2013#    Updated: Feb 16, 2021#    Taken From: http://programmingnotes.org/#    File:  subtraction.py#    Description: Demonstrates subtracting numbers together# =============================================================================def main():    # declare variables    # NOTE: data types do not need to be declared    num1 = 0;    num2 = 0;    total = 0;     # in Python, input is automatically cast as strings    # so we convert everything to integers in order to do math    num1 = int(input("Please enter the first number: "))    num2 = int(input("Please enter the second number: "))     # calculate the difference of the two numbers here    total = num1 - num2     # display data to the screen.    print("The difference between %d and %d is: %d" % (num1, num2, total)) if __name__ == "__main__":    main()# http://programmingnotes.org/ ```

The above code should give you the following output:

```Please enter the first number: 25 Please enter the second number: 1 The difference between 25 and 1 is: 24 ```

==== MULTIPLYING TWO NUMBERS ====

This can be achieved the same way as the 2 previous methods, simply by editing line 22, and replacing the designated math operator with the star symbol “*”.

``` #3 - Multiplying 2 Numbers Together Python # ============================================================================= # Author: Kenneth Perkins # Date: May 29, 2013 # Updated: Feb 16, 2021 # Taken From: http://programmingnotes.org/ # File: multiplication.py # Description: Demonstrates multiplying numbers together # ============================================================================= def main(): # declare variables # NOTE: data types do not need to be declared num1 = 0; num2 = 0; total = 0; # in Python, input is automatically cast as strings # so we convert everything to integers in order to do math num1 = int(input("Please enter the first number: ")) num2 = int(input("Please enter the second number: ")) # calculate the product of the two numbers here total = num1 * num2 # display data to the screen. print("The product of %d and %d is: %d" % (num1, num2, total)) if __name__ == "__main__": main() # http://programmingnotes.org/ 1234567891011121314151617181920212223242526272829 # =============================================================================#    Author: Kenneth Perkins#    Date:   May 29, 2013#    Updated: Feb 16, 2021#    Taken From: http://programmingnotes.org/#    File:  multiplication.py#    Description: Demonstrates multiplying numbers together# =============================================================================def main():    # declare variables    # NOTE: data types do not need to be declared    num1 = 0;    num2 = 0;    total = 0;     # in Python, input is automatically cast as strings    # so we convert everything to integers in order to do math    num1 = int(input("Please enter the first number: "))    num2 = int(input("Please enter the second number: "))     # calculate the product of the two numbers here    total = num1 * num2     # display data to the screen.    print("The product of %d and %d is: %d" % (num1, num2, total)) if __name__ == "__main__":    main()# http://programmingnotes.org/ ```

The above code should give you the following output:

```Please enter the first number: 8 Please enter the second number: 24 The product of 8 and 24 is: 192```

==== DIVIDING TWO NUMBERS TOGETHER ====

The resulting code will basically be the same as the other previous three, only instead of our variables being of type int within the print statement, they will be of type float.

``` #4 - Dividing 2 Numbers Together Python # ============================================================================= # Author: Kenneth Perkins # Date: May 29, 2013 # Updated: Feb 16, 2021 # Taken From: http://programmingnotes.org/ # File: division.py # Description: Demonstrates dividing numbers together # ============================================================================= def main(): # declare variables # NOTE: data types do not need to be declared num1 = 0; num2 = 0; total = 0; # in Python, input is automatically cast as strings # so we convert everything to integers in order to do math num1 = int(input("Please enter the first number: ")) num2 = int(input("Please enter the second number: ")) # calculate the quotient of the two numbers here total = num1 / num2 # display data to the screen. print("The quotient of %d and %d is: %f" % (num1, num2, total)) if __name__ == "__main__": main() # http://programmingnotes.org/ 1234567891011121314151617181920212223242526272829 # =============================================================================#    Author: Kenneth Perkins#    Date:   May 29, 2013#    Updated: Feb 16, 2021#    Taken From: http://programmingnotes.org/#    File:  division.py#    Description: Demonstrates dividing numbers together# =============================================================================def main():    # declare variables    # NOTE: data types do not need to be declared    num1 = 0;    num2 = 0;    total = 0;     # in Python, input is automatically cast as strings    # so we convert everything to integers in order to do math    num1 = int(input("Please enter the first number: "))    num2 = int(input("Please enter the second number: "))     # calculate the quotient of the two numbers here    total = num1 / num2     # display data to the screen.    print("The quotient of %d and %d is: %f" % (num1, num2, total)) if __name__ == "__main__":    main()# http://programmingnotes.org/ ```

The above code should give you the following output:

```Please enter the first number: 1 Please enter the second number: 25 The quotient of 1 and 25 is: 0.040000 ```

==== MODULUS ====

If you wanted to capture the remainder of the quotient you calculated from the above code, you would use the modulus operator (%).

From the above code, you would only need to edit line 22, from division, to modulus.

``` #5 - Getting the Remainder of 2 Numbers Python # ============================================================================= # Author: Kenneth Perkins # Date: May 29, 2013 # Updated: Feb 16, 2021 # Taken From: http://programmingnotes.org/ # File: modulus.py # Description: Demonstrates performing modulus on numbers # ============================================================================= def main(): # declare variables # NOTE: data types do not need to be declared num1 = 0; num2 = 0; total = 0; # in Python, input is automatically cast as strings # so we convert everything to integers in order to do math num1 = int(input("Please enter the first number: ")) num2 = int(input("Please enter the second number: ")) # calculate the mod of the two numbers here total = num1 % num2 # display data to the screen. print("%d mod %d is: %d" % (num1, num2, total)) if __name__ == "__main__": main() # http://programmingnotes.org/ 1234567891011121314151617181920212223242526272829 # =============================================================================#    Author: Kenneth Perkins#    Date:   May 29, 2013#    Updated: Feb 16, 2021#    Taken From: http://programmingnotes.org/#    File:  modulus.py#    Description: Demonstrates performing modulus on numbers# =============================================================================def main():    # declare variables    # NOTE: data types do not need to be declared    num1 = 0;    num2 = 0;    total = 0;     # in Python, input is automatically cast as strings    # so we convert everything to integers in order to do math    num1 = int(input("Please enter the first number: "))    num2 = int(input("Please enter the second number: "))     # calculate the mod of the two numbers here    total = num1 % num2     # display data to the screen.    print("%d mod %d is: %d" % (num1, num2, total)) if __name__ == "__main__":    main()# http://programmingnotes.org/ ```

The above code should give you the following output:

```Please enter the first number: 1 Please enter the second number: 25 1 mod 25 is: 1 ```

## C++ || Snippet – Round A Number To The Nearest Whole Number

This page will display a simple implementation of a function which rounds a floating point number to the nearest whole number. So for example, if the number 12.34542 was sent to the function, it would return the rounded value of 12.

REQUIRED KNOWLEDGE FOR THIS SNIPPET

```Modf - Modulus For Float Data How To Round A Number ```

``` Round A Number To The Nearest Whole Number C++ #include <iostream> #include <cmath> using namespace std; // function prototype double RoundNumber(double floatNumber); int main() { // declare variables double floatNumber = 0; cout<<"Enter in a floating point number to round: "; cin >> floatNumber; cout<<endl<<floatNumber<<" rounded to the nearest " "whole number is: "<< RoundNumber(floatNumber)<<endl; return 0; } double RoundNumber(double floatNumber) { // declare variables double intNumber = 0; // perform modulus (floatNumber % intNumber) // to render the decimal portion of the floatNumber double fraction = modf(floatNumber,&intNumber); // we round up if fraction >=.50 if(fraction >=.50) { ++intNumber; } return intNumber; }// http://programmingnotes.org/ 12345678910111213141516171819202122232425262728293031323334353637 #include <iostream>#include <cmath>using namespace std; // function prototypedouble RoundNumber(double floatNumber); int main(){ // declare variables double floatNumber = 0; cout<<"Enter in a floating point number to round: "; cin >> floatNumber;  cout<<endl<<floatNumber<<" rounded to the nearest " "whole number is: "<< RoundNumber(floatNumber)<<endl;  return 0;} double RoundNumber(double floatNumber){ // declare variables double intNumber = 0;  // perform modulus (floatNumber % intNumber) // to render the decimal portion of the floatNumber double fraction = modf(floatNumber,&intNumber);  // we round up if fraction >=.50 if(fraction >=.50) { ++intNumber; } return intNumber;}// http://programmingnotes.org/ ```

QUICK NOTES:
The highlighted lines are sections of interest to look out for.

The code is heavily commented, so no further insight is necessary. If you have any questions, feel free to leave a comment below.

Once compiled, you should get this as your output
(Note: the code was compile three separate times to display different output)

`====== RUN 1 ======`

``` Enter in a floating point number to round: 1.3333 1.3333 rounded to the nearest whole number is: 1 ====== RUN 2 ====== Enter in a floating point number to round: 35.56 35.56 rounded to the nearest whole number is: 36 ====== RUN 3 ====== Enter in a floating point number to round: 19.8728 ```

```19.8728 rounded to the nearest whole number is: 20 ```

## C++ || Simple Math Using Integer & Double

This page will display the use of int and double data types.

==== ADDING TWO NUMBERS TOGETHER ====

To add two numbers together, you will have to first declare your variables by doing something like this.

``` Adding 2 Numbers Together C++ // ============================================================================ // Author: Kenneth Perkins // Date: Jan 4, 2012 // Updated: Feb 15, 2021 // Taken From: http://programmingnotes.org/ // File: addition.cpp // Description: Demonstrates adding numbers together // ============================================================================ #include <iostream> int main() { int num1 = 0; int num2 = 0; int sum = 0; std::cout << "Please enter the first number: "; std::cin >> num1; std::cout << "\nPlease enter the second number: "; std::cin >> num2; // calculate the sum of the two numbers here sum = num1 + num2; std::cout << "\nThe sum of " <<num1<<" and "<<num2<< " is: "<< sum; return 0; }// http://programmingnotes.org/ 1234567891011121314151617181920212223242526 // ============================================================================//    Author: Kenneth Perkins//    Date:   Jan 4, 2012//    Updated: Feb 15, 2021//    Taken From: http://programmingnotes.org///    File:  addition.cpp//    Description: Demonstrates adding numbers together// ============================================================================#include <iostream>int main(){    int num1 = 0;    int num2 = 0;    int sum = 0;     std::cout << "Please enter the first number: ";    std::cin >> num1;    std::cout << "\nPlease enter the second number: ";    std::cin >> num2;     // calculate the sum of the two numbers here    sum = num1 + num2;     std::cout << "\nThe sum of " <<num1<<" and "<<num2<< " is: "<< sum;    return 0;}// http://programmingnotes.org/ ```

Notice in lines 12-14, I declared my variables, giving them a name. You can name your variables anything you want, with a rule of thumb as naming them something meaningful to your code (i.e avoid giving your variables arbitrary names like “x” or “y”). In line 22 the actual math process is taking place, storing the sum of “num1” and “num2” in a variable called “sum.” I also initialized my variables to zero. You should always initialize your variables.

The above code should give you the following output:

```Please enter the first number: 8 Please enter the second number: 24 The sum of 8 and 24 is: 32```

==== SUBTRACTING TWO NUMBERS ====

Subtracting two numbers works the same way as the above code, and we would only need to edit the above code in one place to achieve that. In line 22, replace the addition symbol with a subtraction sign, and you should have something like this:

``` Subtracting 2 Numbers Together C++ // ============================================================================ // Author: Kenneth Perkins // Date: Jan 4, 2012 // Updated: Feb 15, 2021 // Taken From: http://programmingnotes.org/ // File: subtraction.cpp // Description: Demonstrates subtracting numbers together // ============================================================================ #include <iostream> int main() { int num1 = 0; int num2 = 0; int sum = 0; std::cout << "Please enter the first number: "; std::cin >> num1; std::cout << "\nPlease enter the second number: "; std::cin >> num2; // calculate the difference of the two numbers here sum = num1 - num2; std::cout << "\nThe difference between " <<num1<<" and "<<num2<< " is: "<< sum; return 0; }// http://programmingnotes.org/ 1234567891011121314151617181920212223242526 // ============================================================================//    Author: Kenneth Perkins//    Date:   Jan 4, 2012//    Updated: Feb 15, 2021//    Taken From: http://programmingnotes.org///    File:  subtraction.cpp//    Description: Demonstrates subtracting numbers together// ============================================================================#include <iostream>int main(){    int num1 = 0;    int num2 = 0;    int sum = 0;     std::cout << "Please enter the first number: ";    std::cin >> num1;    std::cout << "\nPlease enter the second number: ";    std::cin >> num2;     // calculate the difference of the two numbers here    sum = num1 - num2;     std::cout << "\nThe difference between " <<num1<<" and "<<num2<< " is: "<< sum;    return 0;}// http://programmingnotes.org/ ```

The above code should give you the following output:

```Please enter the first number: 8 Please enter the second number: 24 The difference between 8 and 24 is: -16```

==== MULTIPLYING TWO NUMBERS ====

This can be achieved the same way as the 2 previous methods, simply by editing line 22, and replacing the designated math operator with the star symbol “*”.

``` Multiplying 2 Numbers Together C++ // ============================================================================ // Author: Kenneth Perkins // Date: Jan 4, 2012 // Updated: Feb 15, 2021 // Taken From: http://programmingnotes.org/ // File: multiplication.cpp // Description: Demonstrates multiplying numbers together // ============================================================================ #include <iostream> int main() { int num1 = 0; int num2 = 0; int sum = 0; std::cout << "Please enter the first number: "; std::cin >> num1; std::cout << "\nPlease enter the second number: "; std::cin >> num2; // calculate the product of the two numbers here sum = num1 * num2; std::cout << "\nThe product of " <<num1<<" and "<<num2<< " is: "<< sum; return 0; }// http://programmingnotes.org/ 1234567891011121314151617181920212223242526 // ============================================================================//    Author: Kenneth Perkins//    Date:   Jan 4, 2012//    Updated: Feb 15, 2021//    Taken From: http://programmingnotes.org///    File:  multiplication.cpp//    Description: Demonstrates multiplying numbers together// ============================================================================#include <iostream>int main(){    int num1 = 0;    int num2 = 0;    int sum = 0;     std::cout << "Please enter the first number: ";    std::cin >> num1;    std::cout << "\nPlease enter the second number: ";    std::cin >> num2;     // calculate the product of the two numbers here    sum = num1 * num2;     std::cout << "\nThe product of " <<num1<<" and "<<num2<< " is: "<< sum;    return 0;}// http://programmingnotes.org/ ```

The above code should give you the following output:

```Please enter the first number: 8 Please enter the second number: 24 The product of 8 and 24 is: 192```

==== DIVIDING TWO NUMBERS TOGETHER ====

This one is a little different from the other three. Before we would use integer variables to store our data. In division, when you divide numbers together, sometimes they end in decimals. Integer data types can not store decimal data (try it yourself and see), so here is where we use a floating point data type to store the values.

So the resulting code will basically be the same as the other previous three, only instead of our variables being of type int, they will be of type double.

``` Dividing 2 Numbers Together C++ // ============================================================================ // Author: Kenneth Perkins // Date: Jan 4, 2012 // Updated: Feb 15, 2021 // Taken From: http://programmingnotes.org/ // File: division.cpp // Description: Demonstrates dividing numbers together // ============================================================================ #include <iostream> int main() { int num1 = 0; int num2 = 0; double sum = 0; std::cout << "Please enter the first number: "; std::cin >> num1; std::cout << "\nPlease enter the second number: "; std::cin >> num2; // calculate the quotient of the two numbers here sum = (double)num1 / num2; std::cout << "\nThe quotient of " <<num1<<" and "<<num2<< " is: "<< sum; return 0; }// http://programmingnotes.org/ 1234567891011121314151617181920212223242526 // ============================================================================//    Author: Kenneth Perkins//    Date:   Jan 4, 2012//    Updated: Feb 15, 2021//    Taken From: http://programmingnotes.org///    File:  division.cpp//    Description: Demonstrates dividing numbers together// ============================================================================#include <iostream>int main(){    int num1 = 0;    int num2 = 0;    double sum = 0;     std::cout << "Please enter the first number: ";    std::cin >> num1;    std::cout << "\nPlease enter the second number: ";    std::cin >> num2;     // calculate the quotient of the two numbers here    sum = (double)num1 / num2;     std::cout << "\nThe quotient of " <<num1<<" and "<<num2<< " is: "<< sum;    return 0;}// http://programmingnotes.org/ ```

The above code should give the following output:

```Please enter the first number: 8 Please enter the second number: 24 The quotient of 8 and 24 is: 0.333333```

==== MODULUS ====

If you wanted to capture the remainder of the quotient you calculated from the above code, you would use the modulus operator (%).

From the above code, you would only need to edit line 22, from division, to modulus.

``` Getting the Remainder of 2 Numbers C++ // ============================================================================ // Author: Kenneth Perkins // Date: Jan 4, 2012 // Updated: Feb 15, 2021 // Taken From: http://programmingnotes.org/ // File: modulus.cpp // Description: Demonstrates performing modulus on numbers // ============================================================================ #include <iostream> int main() { int num1 = 0; int num2 = 0; int remainder = 0; std::cout << "Please enter the first number: "; std::cin >> num1; std::cout << "\nPlease enter the second number: "; std::cin >> num2; // find the remainder of the two numbers here remainder = num1 % num2; std::cout << "\nThe remainder of " <<num1<<" and "<<num2<< " is: "<< remainder; return 0; }// http://programmingnotes.org/ 1234567891011121314151617181920212223242526 // ============================================================================//    Author: Kenneth Perkins//    Date:   Jan 4, 2012//    Updated: Feb 15, 2021//    Taken From: http://programmingnotes.org///    File:  modulus.cpp//    Description: Demonstrates performing modulus on numbers// ============================================================================#include <iostream>int main(){    int num1 = 0;    int num2 = 0;    int remainder = 0;     std::cout << "Please enter the first number: ";    std::cin >> num1;    std::cout << "\nPlease enter the second number: ";    std::cin >> num2;     // find the remainder of the two numbers here    remainder = num1 % num2;     std::cout << "\nThe remainder of " <<num1<<" and "<<num2<< " is: "<< remainder;    return 0;}// http://programmingnotes.org/ ```

The above code should give the following output:

```Please enter the first number: 24 Please enter the second number: 8 The remainder of 24 and 8 is: 0```