In: Computer Science
Correct and complete this C++ program. You are not allowed to include a directory nor changing a variable type or a loop syntax. Stick on the number of lines.
#include<iostream>
using namespace std;
int main ()
{
int N1, N2;
int power;
cout<<"Please enter N1 and N2 in order to calculate N1^N2. N1 should be a positive number. N2 should be between -9 and 9.\n";
cin>>N1>>N2;
while (…………..)
{
cout<<………………………………………….
cin>>………………………………………….
}
while (…………..)
{
cout<<………………………………………….
cin>>………………………………………….
}
……………………………………………..
if(power>0)
{
for(………… ; ………… ; …………)
………………………………………….
cout<<………………………………
}
else if(power<0)
{
for(………… ; ………… ; …………)
………………………………………….
cout<<………………………………
}
else
cout<<………………………………
return 0;
}
Code
#include<iostream>
using namespace std;
int main ()
{
int N1, N2;
int power;
cout<<"Please enter N1 and N2 in order to calculate N1^N2. N1 should be a positive number. N2 should be between -9 and 9.\n";
cin>>N1>>N2;
while (N1<0)
{
cout<<"N1 cannot be negative , must be >=0 , Please enter again\n";
cin>>N1;
}
while (!(N2>=-9 && N2<=9))
{
cout<<"N2 must lie between -9 and 9, Please enter again\n";
cin>>N2;
}
power =N2>=0?1:-1;
if(power>0)
{
for(int i=N2 ; i>0 ;i--)
power =power * N1;
cout<<"\nPower : "<<power;
}
else if(power<0)
{
for(int i=N2 ;i<0 ; i++)
power =power*N2;
cout<<"\nPower : "<<(-1/power);
}
else
cout<<"\nPower : "<<power;
return 0;
}
Screenshot
Output