I'm ultra new to programming. I basically know only IFs, loops, variables. This is first time I used functions in my code and I'm not sure if I understand them correctly.
Basically it is a calculator that requests two natural positive numbers on input, and outputs it's addition, subtraction, multiplication, division and exponentiation. The only rule is: you can use only addition when calculating these expressions. I'm ultra new to programming. I basically know only IFs, loops, variables. This is first time I usentiation. It does not yet support: using '0' as input, using negative numbers as input, using floating point numbers as input, indivisible numbers.
My question is: can you please take a look at my code and just tell me if I made any mistakes that literally burn your eyes? What should I fix? What is especially bad?
#include <iostream>
using namespace std;
int addition(int a, int b)
{
return a + b;
}
int subtraction(int a, int b)
{
return a + (-b);
}
int multiplication(int a, int b)
{
int result = a;
for (int i = 1; i < b; i++)
result += a;
return result;
}
int division(int a, int b)
{
int counter = 0;
if (a % b == 0) {
do {
a = subtraction(a, b);
counter++;
} while (a > 0);
return counter;
}
else
return false;
}
int exponentiation(int a, int b)
{
int result = 1;
for (int i = 1; i <= b; i++)
result = multiplication(result, a);
return result;
}
int main()
{
int numberA, numberB;
cout << "Insert two positive natural numbers: ";
cin >> numberA >> numberB;
cout << "\nAddition: " << addition(numberA, numberB) << '\n';
cout << "Subtraction: " << subtraction(numberA, numberB) << '\n';
cout << "Multiplication: " << multiplication(numberA, numberB) << '\n';
cout << "Division: " << division(numberA, numberB) << '\n';
cout << "Exponentiation: " << exponentiation(numberA, numberB) << '\n';
return 0;
}
%
operator in your division function. And you really don't need to use%
there. Using unary-
also seems a bit questionable under the rules of the assignment, but they probably didn't actually mean to rule it out. \$\endgroup\$