Tuesday, 13 September 2016

Powers I

C++ PROGRAM


POWERS (without using in-built function)


In this program the user will enter any number 'a' and the power 'b' (which can be any whole number), and the output will be a^b, that is, 'a' raised to the power 'b'.

In this program I will not use the in-built function for calculating powers. I will use only 
for-loop.

Code:

#include<iostream.h>
#include<conio.h>
void main()
{
double a,b,i,p=1;
cout<<"Powers"<<\n";
cout<<"Enter any number: "
cin>>a;
cout<<"Raised to the power: ";
cin>>b;

for(i=1;i<=b;i++)
{
 p=p*a;
}

cout<<"Ans. "<<p;

getch();

}

Output:

First Run:

Second Run:





In the next program (Powers II), I will use in-built function for calculation of powers.



For any queries or suggestions please comment !

No comments:

Post a Comment