i have to do a program that read a number and say if it is a power of 3, using recursivity function.
i already did but something is not right, please help
thanks
[CODE=cpp]# include <iostream.h>
# include <conio.h>
bool powerof3 (int n)
{
if((n%3)==0)
{
n=n/3;
powerof3(n);
}
else
{
if (n==1)
{
return true;
}
else
{
return false;
}
}
}
main()
{
int n;
bool a;
cout<<"escriba un numero"<<endl;
cin>>n;
a=powerof3(n);
if (a==true)
{
cout<<"el numero "<<n<<" es una potencia de 3"<<endl;
}
if (a==false)
{
cout<<"el numero "<<n<<" no es una potencia de 3"<<endl;
}
getch();
}[/CODE]
i already did but something is not right, please help
thanks
[CODE=cpp]# include <iostream.h>
# include <conio.h>
bool powerof3 (int n)
{
if((n%3)==0)
{
n=n/3;
powerof3(n);
}
else
{
if (n==1)
{
return true;
}
else
{
return false;
}
}
}
main()
{
int n;
bool a;
cout<<"escriba un numero"<<endl;
cin>>n;
a=powerof3(n);
if (a==true)
{
cout<<"el numero "<<n<<" es una potencia de 3"<<endl;
}
if (a==false)
{
cout<<"el numero "<<n<<" no es una potencia de 3"<<endl;
}
getch();
}[/CODE]
Comment