/* Program for Calculating and Displaying POWER of entered number without using Function */ #include<iostream> using namespace std; int main() { int i = 0, a, b; float p = 1; cout << "Enter any number for base?"; cin >> a; cout << "Enter any number for power?"; cin >> b; if (b == 0) cout << "Power = 1\n"; else if ((a>0 && b>0) || (a<0 && b>0)) { while (i < b) { p = p*a; i++; } cout << "Power=" << p << endl; } else { while (i > b) { p = p * (1.0 / a); i--; } cout << "Power=" << p << endl; } return 0; } OR /* Program for Calculating and Displaying POWER of entered number without using Function */ #include<iostream> using namespace std; int main() { int i = 0, n, b, e; float p = 1; cout << "Enter any number for base?"; cin >> n; cout << "Enter any number for power?"; cin >> e; if (e < 0) b = -e; else b = e; while (i < b) { p = p*n; i++; } if (e == 0) cout << "Power = 1\n"; else if (e>0) cout << "Power=" << p << endl; else cout << "Power=" << 1.0 / p << endl; return 0; }
This blog is about coding in some commonly used "High Level Languages" like C, C++, Java etc. At first, I will post here basic programs written in C++ language only.
Wednesday, November 19, 2014
C++ Program for calculating "Power" without using built-in function
Labels:
C++ Programs
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment