Write a C program called powerofthree c that prompts the use

Write a C program, called power-of-three c, that prompts the user to input a positive integer, and then checks whether this integer is a power of 3. If the input is, indeed, equal to 3\" for some non- negative integer n (for example 1 30, 81 34, or 129140163 3 the program should tell the 17 user that the input is a powerof 3 and print the exponent n. If the input is a positive integer which is not a power of 3, the program should print a corresponding message. Finally, if the input is an integer less than 1, the program should terminate with an appropriate error message. Here are four sample runs of this program, assuming that the executable file is called power-of-three /home/userXYZ/ECE15/Lab1> power-of-three Enter a positive integer 81 81 is a power of 3 exponent 4 /home/userXYZ/ECE15/Lab1> power-of-three Enter a positive integer 1 1 is a power of 3 exponent E 0 /home/userXYZ/ECE15/Lab1> power-of-three Enter a positive integer 12 12 is not a power of 3 /home/userXYZ/ECE15/Lab1> power-of-three Enter a positive integer -1 Error: invalid integer entered Note: The number 3 should be introduced into the program as a symbolic constant BASE, using the #define compiler directive. It should not appear anywhere else in the program. Moreover, the program should continue working correctly when BASE is re-defined to stand for another integer greater than 1, including all the messages that the program prints out.

Solution

#include <stdio.h>
#define BASE 3
int main()
{
int num, i=0, n;
int flag = 1;
printf(\"Enter a positive number: \");
scanf(\"%d\", &num);
if(num<=0){
printf(\"Error: Invalid integer entered\ \");
return 1;
}
n = num;
while(n > 1){
if(n % BASE == 0){
n = n /3;
i++;
}
else{
flag = 0;
break;
}
}
  
if(flag == 1){
printf(\"%d is power of 3, exponent = %d\ \",num,i );
}
else{
printf(\"%d is not power of 3!\ \",num);
}

return 0;
}

Output:

sh-4.2$ gcc -o main *.c                                                                                                                                                                                                                                                  

sh-4.2$ main                                                                                                                                                                                                                                                             

Enter a positive number: 81                                                                                                                                                                                                                                              

81 is power of 3, exponent = 4                                                                                                                                                                                                                                           

sh-4.2$ gcc -o main *.c                                                                                                                                                                                                                                                  

sh-4.2$ main                                                                                                                                                                                                                                                             

Enter a positive number: 1                                                                                                                                                                                                                                               

1 is power of 3, exponent = 0                                                                                                                                                                                                                                            

sh-4.2$ main                                                                                                                                                                                                                                                             

Enter a positive number: 12                                                                                                                                                                                                                                              

12 is not power of 3!                                                                                                                                                                                                                                                    

sh-4.2$ main                                                                                                                                                                                                                                                             

Enter a positive number: -1                                                                                                                                                                                                                                              

Error: Invalid integer entered                                                                                                                                                                                                                                           

sh-4.2$ main                                                                                                                                                                                                                                                             

Enter a positive number: 27                                                                                                                                                                                                                                              

27 is power of 3, exponent = 3

 Write a C program, called power-of-three c, that prompts the user to input a positive integer, and then checks whether this integer is a power of 3. If the inp
 Write a C program, called power-of-three c, that prompts the user to input a positive integer, and then checks whether this integer is a power of 3. If the inp

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site