Thread: Can't do this question using while or for loop

  1. #1
    Registered User
    Join Date
    Oct 2017
    Posts
    4

    Question Can't do this question using while or for loop

    Write a program that prints the powers of two from 2^0 to 2^10, i.e. the output of the program should look like this:

    2^0 = 1
    2^1 = 2
    2^2 = 4...

    2^10 = 1024

    Note 1: Although you may want to use the pow function in this program, you are encouraged not to.

    Note 2: This program does not require any input from the user.

    Please can someone help me?

    Thanks

  2. #2
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Quote Originally Posted by Zuri786 View Post
    Can't do this question using while or for loop
    To be clear, you're not allowed to use any loops?
    If not, you can just code each equation in sequence.

    Quote Originally Posted by Zuri786 View Post
    Note 1: Although you may want to use the pow function in this program, you are encouraged not to.
    It sounds like you are encouraged to write your own exponent function, but it's not mandatory (i.e. you are given a pre-existing alternative).

    Quote Originally Posted by Zuri786 View Post
    Please can someone help me?
    What have you written so far, and how are you stuck?

  3. #3
    Registered User
    Join Date
    Oct 2017
    Posts
    4
    Quote Originally Posted by Matticus View Post
    To be clear, you're not allowed to use any loops?
    If not, you can just code each equation in sequence.



    It sounds like you are encouraged to write your own exponent function, but it's not mandatory (i.e. you are given a pre-existing alternative).



    What have you written so far, and how are you stuck?
    I haven't got the code on me right now but can you do me a sample of what you mean because i'm new to programming?

    Thanks

  4. #4
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    We are here to help you with problems, not provide hand-outs. If you're new to programming, then you really need to try solving this on your own, and ask for help only when you get stuck. The programming assignments are only going to get more difficult, so now is a great time to start practicing and building up your skills.

    Surely you have learned enough by now to declare variables and print their values. So start making an attempt to write this program, and if you get stuck, post your progress (in code tags) and explain exactly what you are stuck on.

  5. #5
    Banned
    Join Date
    Aug 2017
    Posts
    861
    Code:
    #include<stdio.h>
    /* A recursive function to get a^b
      Works only if a >= 0 and b >= 0  */
    int pows(int a, int b)
    {
       if(b)
         return multiply(a, pows(a, b-1));
       else
        return 1;
    }    
     
    /* A recursive function to get x*y */
    int multiply(int x, int y)
    {
       if(y)
         return (x + multiply(x, y-1));
       else
         return 0;
    }
     
    /* driver program to test above functions */
    int main()
    {
          printf("\n 2^0 = %d", pows(2,0));
         printf("\n 2^1 = %d", pows(2,1));
        printf("\n 2^2 = %d", pows(2,2));
         printf("\n 2^3 = %d", pows(2,3));
         printf("\n 2^4 = %d", pows(2,4));
         printf("\n 2^5 =  %d\n", pows(2,5));
         printf("\n 2^10 =  %d\n", pows(2,10));
      
     
      return 0;
    }
    output
    Code:
    [userx@devuan powers]$ ./a.out
    
     2^0 = 1
     2^1 = 2
     2^2 = 4
     2^3 = 8
     2^4 = 16
     2^5 =  32
    
     2^10 =  1024
    [userx@devuan powers]$
    Write you own Power without using multiplication(*) and division(/) operators - GeeksforGeeks

    but you'd better understand how it is really working, that is why I provided the link ...

    how functions can call themselves, what values are being returned
    putting printf in functions and looking at the values,


    something like this to try and figure it out
    Code:
    /* A recursive function to get a^b
      Works only if a >= 0 and b >= 0  */
    int pows(int a, int b)
    { printf("b = %d\n", b);
       if(b){
        printf("%d\n", multiply(a, pows(a, b-1)));
         return multiply(a, pows(a, b-1));
    }
       else
        return 1;
    }
    I'm sure that teacher may know the answer is out there too. so you'd should make sure you're up to snuff on that.
    it's hard being a teacher now days, the answers are on google.
    Last edited by userxbw; 10-05-2017 at 08:25 PM.

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    It seems more likely that there should be a comma in the title.

    Can't do this question using while or for loop
    Vs
    Can't do this question, using while or for loop

    A simple loop and the << operator is all you need.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 09-22-2016, 09:08 AM
  2. question for loop
    By ursu09 in forum C Programming
    Replies: 2
    Last Post: 02-06-2016, 11:05 AM
  3. loop question.
    By ashlee in forum C Programming
    Replies: 3
    Last Post: 10-30-2010, 01:37 PM
  4. question about while loop
    By sfff in forum C Programming
    Replies: 2
    Last Post: 10-25-2009, 12:40 AM
  5. Loop question
    By Jas11 in forum C++ Programming
    Replies: 31
    Last Post: 04-28-2005, 07:16 PM

Tags for this Thread