Elevate an input number without using advanced functions

Hello everybody. I am new here and also new in the world of c++.
Last night I was trying to see if is possible to multiply one number to another without using any given function(like pow) or the multiply sign or whatever.
Just using addition and subtract signs (+and -) with a for loop. It worked.

Now I am trying to understand if is possible to elevate one given number (inserted by the user) to another number without using again any function or the multiply / divide/ etc functions. Just add and subtract functions. Anyone could enlighten me please.

I am trying to learn first steps of c++ with no use of too many functions, just basics of loops and all that beginner stuff. But I also need some help. Thanks a lot.
Obviously you'll need to reuse your implementation of multiplication via summation.

It is only important to take care and not use addition more time than it is necessary. I.e. to multiply M by N you need not N additions. And to raise M to power N you need not N multiplications. :)
Well, here is what i did so far. I wrote down this code to find the quotient and the rest of two numbers (first number to second number).
#include <iostream>
using namespace std;

int main() {
int a,b,r;
r=0;

cin >>a>>b;

while (a>=b){
a=a-b;
r=r+1;
}
cout << a<< r;

return 0;
}


Is sloppy but works.

Now, id anyone could help me understand how would i be able to solve the mistery of Elevate one number to a second with basic functions (only addition and subtract) i would be so happy and i can go forward studying c++.


this seems to work
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include <iostream>
using namespace std;

int main()
{

	int num, pow, sum = 0, addNum = 0;

	cout << "Please enter a number: ";
	cin >> num;
	cin.ignore();

	cout << "Please enter the power of the previous number: ";
	cin >> pow;
	cin.ignore();

	int count = 1;
	
	for(int i = 0; i < pow; i++)
	{
		
		while(count > 0)
		{
			addNum = addNum + num;
			count--;
		}

		count = addNum;
		sum = addNum;
		addNum = 0;
	}
	
	if(pow == 0)
		sum = 1;

	cout << num << " to the power of " << pow << " is " << sum << endl;

	cin.ignore();
	return 0;
}
Topic archived. No new replies allowed.