
- Forum
- Programming Talk
- C and C++
- doubt in c to find power of 2
doubt in c to find power of 2
This is a discussion on doubt in c to find power of 2 within the C and C++ forums, part of the Programming Talk category; hi Plz clear my doubt how to write a code to find the power of 2 in a normal way ...
-
07-06-2006, 06:00 AM #1
- Join Date
- Jul 2006
- Answers
- 2
doubt in c to find power of 2
hi
Plz clear my doubt
how to write a code to find the power of 2 in a normal way and in single step?
PLZ REPLY AS SOON AS POSSIBLE
Last edited by leeanju2006; 07-06-2006 at 06:03 AM.
-
One of the way of doing this is using a function named as pow() in function c as below:
#include <stdio.h>
#include <math.h>
main()
{
int n,b;
printf("Input power value");
scanf("%d",n);
b=pow(2,n);
print("%d",b);
}
-
07-07-2006, 12:52 AM #3
- Join Date
- Jul 2006
- Answers
- 2
thank u very much sir. but is there any way to write the code without using the pow( ) fuction ?
-
07-13-2006, 11:22 PM #4
Hi leeanju2006,
I didn't get your problem clearly.... are you asking..
anything raised to power of 2... or 2 raised to the power of any thing????
x pow 2 or 2 pow x
1)
if it's x pow 2, then
int main()
{
int x,i ;
scanf("%d',&x);
if(x==0)
printf("1");
else
{
x= x*x;
printf("power of x when raised to power of 2 is :::%d", x);
getch()
}
}
2)
If 2 to be raised to pow of x ( 2 power something)
void main()
{
clrscr();
int d,ans=2,x;
printf("enter the pow to raise the 2");
scanf("%d",&x);
if(x<=0) { printf("1"); }
else
{
for(d=0;d<x-1;d++,ans=ans*2); printf("2 pow %d is ......%d", x, ans);
}
getch();
}
regards,
SVS.Aditya
-
Sponsored Ads

Reply With Quote





