C++ Program of (a) power b without using any builtin functions

in #programming7 years ago

Program Body

#include< iostream >
#include< conio.h >
using namespace std;
int main(){
int ex,re,num,n;
const int z=0;
cin>>num;
cin>>ex;
if(num>0&&ex>0){

re=numnum;
for(n=2;n<ex;n++){
re=re
num;

}
cout<<re;
}else if(num<0&&ex>0){

re=numnum;
for(n=2;n<ex;n++){
re=re
num;

}
cout<<re;
}
else if(num>0&&ex<0){
cout<<"0.";
ex=-ex;
if(num>9&&num<100){

        for(n=2;n<ex;n++){
    cout<<z;
}
cout<<num;
}else if(num>0&&num<10){
        for(n=1;n<ex;n++){
    cout<<z;
}
cout<<num;
}else if(num>99&&num<1000){
        for(n=3;n<ex;n++){
    cout<<z;
}
cout<<num;
}

}else if(num<0&&ex<0){
cout<<"- 0.";
ex=-ex;
for(n=1;n<ex;n++){
cout<<z;
}
cout<<-(num);
}
else if(ex==0){
cout<<1;
}
else {
cout<<"Invalid exponent expression"<<endl;
}
getch();
return 0;
}

Written In C++


Source

Made in Visual Studio


source

Thanks and I hope you liked it!

Sort:  

its really a great logic in this program

I am thankful

Can you explain it in details?

good work bro...

amazing post

Thank you

  • The formatting, it is very important.Maybe people read this outside steemit.com someway, ok, but… (Also, in steemit.com * disappears…)
  • The style, too, is very important. Spaces help readability. I've used this tool to reformat the code and read it, because otherwise it would have been a pain.
  • conio.h is a Microsoft Windows thing, avoid that for general C++ code; moreover it is used just to add a pause hit-a-key-to-continue; that's a problem I've seen often when students don't know how to run a program from the console (Windows open a console of its own for the text output of the program, but it is closed as the program ends, hence the “need” to pause) — very rookie! Don't do it: run the program from the console instead.
  • If the intent is to show how to compute xn without using “builtin” functions… make the function and show how to use it, do not put all inside the main.
  • The algorithm is wastefully complex, hard to trace, and wrong in several ways. For example, if you use 2 and -2 as input, it gives 0.02 as result, instead of 0.25 (1/4). If the inputs are 3 and 100, the output is -818408495 (check for overflow…)

Coin Marketplace

STEEM 0.13
TRX 0.29
JST 0.035
BTC 109210.40
ETH 2662.55
USDT 1.00
SBD 0.78