Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Help needed to print the value nth number raised to the power n.

Help needed to print the value nth number raised to the power n.

Scheduled Pinned Locked Moved C / C++ / MFC
helpc++delphivisual-studioannouncement
7 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    Rajdeep_
    wrote on last edited by
    #1

    Hello People, I am quite a newbie to C++ programming and I got stuck up with a simple program. The program requires me to print the value for 2 raised to the power 20. I actually had the idea creeping about this program since 1 megabyte is 2 raised to the power 20. I wanted to find the result of that via this piece of code:

    #include
    #include
    int main()
    {
    int a=2;
    for (int i=2; i<=20; i++)
    {
    a=a*a;
    }
    cout<

    and the output I get is: 0

    The compiler/IDE I'm using is Borland C++ (latest version) but I know theres nothing wrong with the compiler though. There must be some logical mistake. Rectification would be appreciated folks. Hoping for help.

    Thanks!
    Rajdeep_

    L V V D 4 Replies Last reply
    0
    • R Rajdeep_

      Hello People, I am quite a newbie to C++ programming and I got stuck up with a simple program. The program requires me to print the value for 2 raised to the power 20. I actually had the idea creeping about this program since 1 megabyte is 2 raised to the power 20. I wanted to find the result of that via this piece of code:

      #include
      #include
      int main()
      {
      int a=2;
      for (int i=2; i<=20; i++)
      {
      a=a*a;
      }
      cout<

      and the output I get is: 0

      The compiler/IDE I'm using is Borland C++ (latest version) but I know theres nothing wrong with the compiler though. There must be some logical mistake. Rectification would be appreciated folks. Hoping for help.

      Thanks!
      Rajdeep_

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      You are not raising it to the 20th power, you are repeatedly squaring the value, The first time through the loop you get 4 (2*2), then 16 (4*4), then 256 (16*16), and so on.

      Use the best guess

      R 1 Reply Last reply
      0
      • L Lost User

        You are not raising it to the 20th power, you are repeatedly squaring the value, The first time through the loop you get 4 (2*2), then 16 (4*4), then 256 (16*16), and so on.

        Use the best guess

        R Offline
        R Offline
        Rajdeep_
        wrote on last edited by
        #3

        Thank you. I will work on that part.

        1 Reply Last reply
        0
        • R Rajdeep_

          Hello People, I am quite a newbie to C++ programming and I got stuck up with a simple program. The program requires me to print the value for 2 raised to the power 20. I actually had the idea creeping about this program since 1 megabyte is 2 raised to the power 20. I wanted to find the result of that via this piece of code:

          #include
          #include
          int main()
          {
          int a=2;
          for (int i=2; i<=20; i++)
          {
          a=a*a;
          }
          cout<

          and the output I get is: 0

          The compiler/IDE I'm using is Borland C++ (latest version) but I know theres nothing wrong with the compiler though. There must be some logical mistake. Rectification would be appreciated folks. Hoping for help.

          Thanks!
          Rajdeep_

          V Offline
          V Offline
          V J NAGA VARA PRASAD
          wrote on last edited by
          #4

          a is a integer data type :) .you are printing the value by multiplying it 19 times and which in return it exceeds more than LONG data type :sigh: .hence your output is always zero :doh: . There is no compiler problem. :) I think you got what I am explaining you. :omg:

          1 Reply Last reply
          0
          • R Rajdeep_

            Hello People, I am quite a newbie to C++ programming and I got stuck up with a simple program. The program requires me to print the value for 2 raised to the power 20. I actually had the idea creeping about this program since 1 megabyte is 2 raised to the power 20. I wanted to find the result of that via this piece of code:

            #include
            #include
            int main()
            {
            int a=2;
            for (int i=2; i<=20; i++)
            {
            a=a*a;
            }
            cout<

            and the output I get is: 0

            The compiler/IDE I'm using is Borland C++ (latest version) but I know theres nothing wrong with the compiler though. There must be some logical mistake. Rectification would be appreciated folks. Hoping for help.

            Thanks!
            Rajdeep_

            V Offline
            V Offline
            Vaclav_
            wrote on last edited by
            #5

            int x,total = 1 ; for(x=0; x<20; x++){ total += 1<

            L 1 Reply Last reply
            0
            • V Vaclav_

              int x,total = 1 ; for(x=0; x<20; x++){ total += 1<

              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #6

              total = 1<<20;

              Use the best guess

              1 Reply Last reply
              0
              • R Rajdeep_

                Hello People, I am quite a newbie to C++ programming and I got stuck up with a simple program. The program requires me to print the value for 2 raised to the power 20. I actually had the idea creeping about this program since 1 megabyte is 2 raised to the power 20. I wanted to find the result of that via this piece of code:

                #include
                #include
                int main()
                {
                int a=2;
                for (int i=2; i<=20; i++)
                {
                a=a*a;
                }
                cout<

                and the output I get is: 0

                The compiler/IDE I'm using is Borland C++ (latest version) but I know theres nothing wrong with the compiler though. There must be some logical mistake. Rectification would be appreciated folks. Hoping for help.

                Thanks!
                Rajdeep_

                D Offline
                D Offline
                David Crow
                wrote on last edited by
                #7

                You need to multiply a by 2 twenty times. Something like:

                int a = 1;
                for (int i = 0; i < 20; i++)
                a = a * 2;

                "One man's wage rise is another man's price increase." - Harold Wilson

                "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

                "Show me a community that obeys the Ten Commandments and I'll show you a less crowded prison system." - Anonymous

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups