Function to find the given number is a power of 2 or not?
Answers were Sorted based on User's Feedback
Answer / abhinay
main()
{
int n,m;
do
{
m=n%2;
n=m;
}
while(n>1);
if(m==1)
{
Printf("Number is not a power of 2");
}
else
{
printf("Number is a power of 2");
}
}
| Is This Answer Correct ? | 1 Yes | 0 No |
Answer / sagar bhagat
#include<stdio.h>
#include<conio.h>
void main()
{
int num,i,flag=0,temp;
clrscr();
printf("\nEnter the number=");
scanf("%d",&num);
if(num<=0)
printf("Plz enter anoter num");
else
{
temp=num;
for(i=1;(i<num) ;i++)
{
if((i*i)==temp)
{
flag=1;
break;
}
}
}
if(flag==1)
printf("\nNumber %d is in power of 2 of %d",num,i);
else
printf("\nNumber %d is not in power of 2 ",num);
getch();
}
| Is This Answer Correct ? | 1 Yes | 0 No |
Answer / sujan.t
/* 100% correct answer */
int n = 3;
boolean bool = true;
int reminder;
while (n >1)
{
reminder = n % 2;
if(reminder != 0)
{
bool = false;
break;
}
else
{
n = n / 2;
}
}
if (bool == true)
printf("The number is a power of two");
else
printf("The number is NOT A power of two");
| Is This Answer Correct ? | 1 Yes | 1 No |
Answer / anyone
//Revision for Ahmed Ihmeid answer:
//Operator % prototype: int % int
bool isPowerOf2(int num)
{
int x;
x = num % (int(ceil(sqrt(num)));
if( x == 0)
return true; //it is a power of 2
else
return false; //it is not
}
proof:
4 % 2 = 0
16 % 4 = 0
22 % 5 != 0
| Is This Answer Correct ? | 2 Yes | 2 No |
Answer / s.v.prasad reddy,lifetree conv
#include<stdio.h>
#include<conio.h>
#include<math.h>
main()
{
int num,i=0,flag;
clrscr();
printf("\nEnter the number:=");
scanf("%d",&num);
if(num==0 || num==1 || (num%2)!=0) /* Validation Part */
{
printf("\n We can't express the given number to power
of 2");
return;
}
for(;;}
{
if(pow(2,i)==num)
{
flag=1;
break;
}
if(pow(2,i)>num)
{
flag=0;
break;
}
i++;
}
if(falg==1)
printf("\n %d number is 2 power of %d",num,i);
else
printf("\n%d number can't be expressed as power of 2",num);
getch();
}
| Is This Answer Correct ? | 3 Yes | 4 No |
Answer / ahmed ihmeid
bool isPowerOf2(int num)
{
float x;
x = num % (sqrt(num));
if( x == 0)
return true; //it is a power of 2
else
return false; //it is not
}
proof:
4 % 2 = 0
16 % 4 = 0
22 % 4.7 != 0
| Is This Answer Correct ? | 1 Yes | 2 No |
Answer / hassan noureddine
int isPowrOf2 (unsigned int number)
{
float x;
if (number <= 0) return (0);
x = log(number) / log(2);
return ( (int) ( ((int)x*log(2))/log(number)));
}
| Is This Answer Correct ? | 4 Yes | 6 No |
Answer / sudhesh
#include<stdio.h>
main()
{
int num,result;
printf("\nEnter the number:=");
scanf("%d",&num);
if (result=num&(num-1))
printf("No\n");
else
printf("Yes\n");
}
| Is This Answer Correct ? | 1 Yes | 3 No |
int isPowerOf2(unsigned int n)
{
float r=n;
while(r>1) r/=2.0;
return (r==1)?1:0; /* 1-> TRUE; 0-> FALSE*/
}
| Is This Answer Correct ? | 9 Yes | 12 No |
Answer / sheikh rasel
#include<stdio.h>
main()
{
int i;
if(i%2=0)
printf("The given number is a power of 2");
else
printf("The given number is not a power of 2");
return 0;
}
| Is This Answer Correct ? | 6 Yes | 133 No |
write a program that will print %d in the output screen??
Write the program with at least two functions to solve the following problem. The members of the board of a small university are considering voting for a pay increase for their 10 faculty members. They are considering a pay increase of 8%. Write a program that will prompt for and accept the current salary for each of the faculty members, then calculate and display their individual pay increases. At the end of the program, print the total faculty payroll before and after the pay increase, and the total pay increase involved.
List at least 10 sorting methods indicating their average case complexity, worst case complexity and best case complexity.
what is the output of following question? void main() { int i=0,a[3]; a[i]=i++; printf("%d",a[i] }
What is a void pointer? When is a void pointer used?
The program will first compute the tax you owe based on your income. User is prompted to enter income. Program will compute the total amount of tax owed based on the following: Income Tax 0 - $45,000 = 0.15 x income $45,001 - $90,000 = 6750 + 0.20 x (income – 45000) $90,001 - $140,000 = 15750 + 0.26 x (income – 90000) $140,001 - $200,000 = 28750 + 0.29 x (income – 140000) Greater than $200,000 = 46150 + 0.33 x (income – 200000) Dollar amounts should be in dollars and cents (float point numbers with two decimals shown). Tax is displayed on the screen.
What is a file descriptor in c?
Explain how do you determine whether to use a stream function or a low-level function?
What does typeof return in c?
What are the similarities between c and c++?
a simple program in c language
Explain about the functions strcat() and strcmp()?