15,887,485 members
Sign in
Sign in
Email
Password
Forgot your password?
Sign in with
home
articles
Browse Topics
>
Latest Articles
Top Articles
Posting/Update Guidelines
Article Help Forum
Submit an article or tip
Import GitHub Project
Import your Blog
quick answers
Q&A
Ask a Question
View Unanswered Questions
View All Questions
View C# questions
View C++ questions
View Javascript questions
View Visual Basic questions
View Python questions
discussions
forums
CodeProject.AI Server
All Message Boards...
Application Lifecycle
>
Running a Business
Sales / Marketing
Collaboration / Beta Testing
Work Issues
Design and Architecture
Artificial Intelligence
ASP.NET
JavaScript
Internet of Things
C / C++ / MFC
>
ATL / WTL / STL
Managed C++/CLI
C#
Free Tools
Objective-C and Swift
Database
Hardware & Devices
>
System Admin
Hosting and Servers
Java
Linux Programming
Python
.NET (Core and Framework)
Android
iOS
Mobile
WPF
Visual Basic
Web Development
Site Bugs / Suggestions
Spam and Abuse Watch
features
features
Competitions
News
The Insider Newsletter
The Daily Build Newsletter
Newsletter archive
Surveys
CodeProject Stuff
community
lounge
Who's Who
Most Valuable Professionals
The Lounge
The CodeProject Blog
Where I Am: Member Photos
The Insider News
The Weird & The Wonderful
help
?
What is 'CodeProject'?
General FAQ
Ask a Question
Bugs and Suggestions
Article Help Forum
About Us
Search within:
Articles
Quick Answers
Messages
Comments by Member NFOC (Top 6 by date)
Member NFOC
11-Aug-18 13:53pm
View
I think this one is suitable answer.
Member NFOC
29-Jul-18 11:45am
View
Thank you for your response it helped.
Member NFOC
29-Jul-18 1:37am
View
I'm thinking your code is also not under the conditions predefined. As your code is taking more than three variables(three variables should be like this two for exponents and base and one in for loop), by taking b inside the loop you are making a code similar to this:
for (int i = 0, result = 1; i < expo; i++ ) {
result *= base;
}
Member NFOC
28-Jul-18 15:22pm
View
Sir I'm thinking your code is also not under the conditions predefined. As your code is taking more than three variables(three variables should be like this two for exponents and base and one in for loop), by taking b inside the loop you are making a code similar to this:
for (int i = 0, result = 1; i < expo; i++ ) {
result *= base;
}
Member NFOC
28-Jul-18 14:50pm
View
Deleted
But i think that your code i.e.,
for (int i = exponent - 1, exponent = 1; i >= 0; i--)
{
exponent *= base;
}
also won't work for zero.
Also it will not work if we will print the result outside the loop. As inside the loop it is taking exponent variable as different variable inside the loop.
Updated code will look like this
int a;
int b;
std::cout << "Let's find the b power of a: " << std::endl;
std::cout << "Enter b: ";
std::cin >> b;
std::cout << "Enter a: ";
std::cin >> a;
if (b == 0) {
std::cout << "Result: " << 1 << std::endl;
}
for (int i = b-1 , b = 1; i >= 0; i--) {
b *= a;
if (i == 0) {
std::cout << "Result: " << b << std::endl;
}
}
Thanks again for coming for rescue and helping me make more optimised code.
Member NFOC
27-Jul-18 9:32am
View
Thanks it helped. Also i found a another similar code as yours by doing some adjustments like this:
int a;
int b;
std::cout << "Let's find the b power of a: " << std::endl;
std::cout << "Enter b: ";
std::cin >> b;
std::cout << "Enter a: ";
std::cin >> a;
for (int i = b-1 , b = 1; i >= 0; i--) {
//std::cout << "b: " << b << std::endl;
b *= a;
//std::cout << "i: " << i << std::endl;
if (i == 0) {
std::cout << "b: " << b << std::endl;
}
}
Show More