int m, n, pow;

	cout << "Enter m and n: ";
	cin >> m >> n;
	pow = 1;
	
	for (int i=1; i<=n; i++)
		pow = pow * m;

	cout << m << "^" << n << " = " << pow << endl;
	return 0;
}

I dont get how this is working :((

ty!

Recommended Answers

int m, n, pow;

	cout << "Enter m and n: ";
	cin >> m >> n;
	pow = 1;
	
	for (int i=1; i<=n; i++)
		pow = pow * m;

	cout << m << "^" << n << " = " << pow << endl;
	return 0; …
Jump to Post

All 3 Replies

int m, n, pow;

	cout << "Enter m and n: ";
	cin >> m >> n;
	pow = 1;
	
	for (int i=1; i<=n; i++)
		pow = pow * m;

	cout << m << "^" << n << " = " << pow << endl;
	return 0;
}

I dont get how this is working :((

ty!

Run through it in your head. If n = 4 and m = 2, then in your for loop you have:

pow = 1 * 2;
pow = 2 * 2;
pow = 4 * 2;
pow = 8 * 2;

2^5= 2*2*2*2*2;
=4*2*2*2
=8*2*2
=16*2
=32

see 2 is decreasing one by one.

for(loop the following code n times){
pow = pow multiplied by m
}

So if we are to loop 'pow * 3' 2 times, that would be:
pow = 1 * 3 = 3
pow = 3 * 3 = 9

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.20 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.