Question #4918

Write a program to read two integers x and y then to calculate x to the power y by using while statement

Expert's answer

# include <iostream>

using namespace std;

int main()

{

int x, y = 0, i = 0,c = 1;

cout<<"Enter x(number): ";

cin>>x;

cout<<"Enter y(power): ";

cin>>y;

if (x == 0 && y == 0)

& cout<<"Nondefinable notion"<<endl;

else if (y >= 0)

{

while (y != 0 )

{

& c=c*x;

& y--;

&

}

cout<<"x^y = "<<c<<endl;

}

else//y<0

{

& while (y != 0 )

{

&

& c=c*x;

& y++;

&

}

& cout<<"x^y: "<<(double)1/c<<endl;

}

system("PAUSE");

return 0;

}

using namespace std;

int main()

{

int x, y = 0, i = 0,c = 1;

cout<<"Enter x(number): ";

cin>>x;

cout<<"Enter y(power): ";

cin>>y;

if (x == 0 && y == 0)

& cout<<"Nondefinable notion"<<endl;

else if (y >= 0)

{

while (y != 0 )

{

& c=c*x;

& y--;

&

}

cout<<"x^y = "<<c<<endl;

}

else//y<0

{

& while (y != 0 )

{

&

& c=c*x;

& y++;

&

}

& cout<<"x^y: "<<(double)1/c<<endl;

}

system("PAUSE");

return 0;

}

## Comments

## Leave a comment