Exforsys
+ Reply to Thread
Results 1 to 4 of 4

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 ...

  1. #1
    leeanju2006 is offline Junior Member Array
    Join Date
    Jul 2006
    Answers
    2

    Question 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.

  2. #2
    norman is offline Member Array
    Join Date
    May 2006
    Answers
    43
    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);
    }


  3. #3
    leeanju2006 is offline Junior Member Array
    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 ?


  4. #4
    svs_aditya's Avatar
    svs_aditya is offline Aditya Array
    Join Date
    Jul 2006
    Location
    India
    Answers
    10
    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



Latest Article

Network Security Risk Assessment and Measurement

Read More...