I used a class this time and added a few more options plus a while loop, check it out and let me know what I did wrong and how to improve it.
MyMath.h
MyMath.cpp
main.cpp
for the sqrt algorithm in MyMath.cpp i searched online how to do it, it took me awhile to find a good guy to explain it right. It looked very difficult at first but as soon as I understood it, I realized that it is really easy. I like that I learned that much so far. Tomorrow will learn more and code more.
Thanks for the help guys
-Jekwa
MyMath.h
Code
#ifndef MYMATH_H
#define MYMATH_H
class MyMath
{
public:
MyMath();
int add(int x, int y);
int multiply(int x, int y);
double divide(double x, double y);
int subtract(int x, int y);
double sqrt(double x);
int exponent(int x, int y);
};
#endif // MYMATH_HCode
#include "MyMath.h"
MyMath::MyMath()
{
}
int MyMath::add(int x, int y)
{
int answer = x + y;
return answer;
}
int MyMath::subtract(int x, int y)
{
int answer = x - y;
return answer;
}
double MyMath::divide(double x, double y)
{
double answer = x / y;
return answer;
}
int MyMath::multiply(int x, int y)
{
int answer = x * y;
return answer;
}
double MyMath::sqrt(double x)
{
double g;
g = x * 50;
for (int i = 0; i <= x * 2; i++)
{
if (g == x)
return g;
if (g != x)
g = (g + x / g) / 2;
}
return g;
}
int MyMath::exponent(int x, int y)
{
int a = 1;
for (int i = 0; i < y; i++)
a *= x;
return a;
}Code
#include <iostream>
#include "MyMath.h"
int main()
{
MyMath math;
int x = 0, y = 0;
int operation = 0;
bool running = true;
while (running)
{
std::cout << "Enter two numbers" << std::endl;
std::cin >> x >> y;
std::cout << "Choose your operation\n1.Addition\n2.Subtraction\n3.Division\n4.Multiplication\n5.Square root\n6.Exponent\n7.Quit" << std::endl;
std::cin >> operation;
switch (operation)
{
case 1:
std::cout << math.add(x, y) << std::endl;
break;
case 2:
std::cout << math.subtract(x, y) << std::endl;;
break;
case 3:
std::cout << math.divide(x, y) << std::endl;;
break;
case 4:
std::cout << math.multiply(x, y) << std::endl;;
break;
case 5:
std::cout << math.sqrt(x) << std::endl;;
break;
case 6:
std::cout << math.exponent(x, y) << std::endl;;
break;
case 7:
running = false;
std::cout << "Quitting...";
break;
default:
std::cout << "Unknown command!" << std::endl;
}
}
return 0;
}for the sqrt algorithm in MyMath.cpp i searched online how to do it, it took me awhile to find a good guy to explain it right. It looked very difficult at first but as soon as I understood it, I realized that it is really easy. I like that I learned that much so far. Tomorrow will learn more and code more.
Thanks for the help guys
-Jekwa



![[Image: chain.png]](https://i.postimg.cc/NFnfH9zY/chain.png)