We were required to write a program to check whether the
given number was of the form 3^n. ( i.e. 3 to the power n).
I knew that it can be done by bit manipulation but couldn't
make out the logic.

i want a solution in C/C++ language

Answer Posted / sreejesh1987

#include<stdio.h>
#include<conio.h>
void main()
{
int count=0,n,d;
clrscr();
printf("\tDigit 3powern\n");
printf("Enter a no:");
scanf("%d",&n);
d=n;
while(d%3==0)
{
d=d/3;
count++;
}
if(d==1)
printf("\nNumber %d is of 3^%d format",n,count);
else
printf("\nNumber %d is not in 3^n format",n);
getch();
}

Is This Answer Correct ?    17 Yes 6 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

One ship goes along the stream direction 28 km and in opposite direction 13 km in 5 hrs for each direction.What is the velocity of stream? `

1292


In a company 30% are supervisors and 40% employees are male if 60% of supervisors are male. What is the probability that a randomly chosen employee is a male or female?

1300


In a cycle race there are 5 persons named as J, K, L, M, N participated for 5 positions so that in how many number of ways can M make always before N?

1225


There six red shoes & 4 green shoes .If two of the shoes are drawn.What is the probability of getting two red shoes.

793


a solution is prepared by mixing two solution with sprit 20% & 60%…In what ration they should be mixed to get the 50% sprit in resultant solution .

706






How will u divide two numbers in a MACRO?

1339


Fill the empty slots. Three FOOTBALL teams are there. Given below the list of maches. played won lost draw Goals for Goals against A 2 2 *0 *0 *7 1 B 2 *0 *1 1 2 4 C 2 *0 *1 *1 3 7 the slots with stars are answers.

774


Olympic race : 4 contestants : Alan,charlie, Darren ,Brain. There are two races and average is taken to decide the winner.one person comes at the same position in both the race. Charlie always come before darren. Brian comes first once. Alan comes third at least once. Find the positions. Alan never comes last. Charlie & darren comes 2nd at least once.

814


A clock is late by 1 minute 27 seconds in a month. Then how much will it be late in 1 day?

1198


The age of the grand father is the sum of his three grandsons.The second is 2 year younger than first one and the third one is 2 year younger than the second one. Then what will be the age of the grandfather?

1237


Calculate the area of the triangle in square cm if a right angled triangle where the length of the shorter leg is one third of the length of its hypotenuse. And the length of the shorter leg is 7 cm.

722


What is the smallest number by which 4880 must be divided in order to make it into a perfect square ?

735


(x-y/3)-(y-x/3)=?

705


In a queue, a man is standing at the position 10th from the front and 15th from behind. How many persons are standing in the queue?

841


+ means * and * means / and / means % what is the value of these question 2+3*5/7 it was two question of these type.

712