Thread: please help to find the power of number using recursion ?

  1. #1
    Registered User
    Join Date
    Dec 2016
    Posts
    3

    please help to find the power of number using recursion ?

    hello am new here so am no sure if am suppose to post this question here but please help

    my question is

    Write a c program to find out the power of a number using recursion. The base number should be an odd number and it must be between 11 and 99. The power should be an even number and it must be between 2 and 20 ??

  2. #2
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688
    What have you tried so far? Post some code with some questions or problems. If you understand the basic premise of recursion this isn't too hard.
    Double Helix STL

  3. #3
    Old Fashioned
    Join Date
    Nov 2016
    Posts
    137
    Quote Originally Posted by genius010 View Post
    hello am new here so am no sure if am suppose to post this question here but please help

    my question is

    Write a c program to find out the power of a number using recursion. The base number should be an odd number and it must be between 11 and 99. The power should be an even number and it must be between 2 and 20 ??
    This isn't even a question, this is an assignment with a couple question marks thrown onto the end.

  4. #4
    Registered User
    Join Date
    Dec 2016
    Posts
    3
    Code:
    #include <stdio.h>
    
     
    
    //function for calculating power
    
    long int getPower(int b,int p)
    
    {
    
        long int result=1;
    
        if(p==0) return result;
    
        result=b*(getPower(b,p-1));  //call function again
    
    }
    
    void main()
    
    {
    
        int base,power, temp, temp1;
    
        long int result;
    
         
    
        printf("Enter value of base: ");
    
        scanf("%d",&base);
    
        temp=base;
    
        if(temp<=99 && temp>=11)
    
        {
    
           
    
             printf("Enter value of power: ");
    
             scanf("%d",&power);
    
        
    
             result=getPower(base,power);
    
         
    
             printf("%d to the power of %d is: %ld\n",base,power,result);
    
         
    
            
    
       }
    
       else
    
       {
    
       printf("Base should be between 11 and 99\n");
    
       }
    
    }
    these are the code and it is working but i only need when i enter even number it must show me message to enter odd number and the opposite in the power
    also the power doesn't show message enter between 2 and 20 ?

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    I guess that's why you're copying other people's code then.
    C program to calculate power of a number using recursion - C solved programs

    To be honest, you need to evaluate why you're even on this course if all you're going to do is copy/paste other people's work without understanding it. Assuming you're paying for your education, you're just wasting your time and money. The piece of paper at the end will be worthless.

    Here's a free snippet
    Code:
    if ( x % 2 == 0 ) {
      // x is even
    } else {
      // x is odd
    }
    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.

  6. #6
    Registered User
    Join Date
    Dec 2016
    Posts
    3
    Quote Originally Posted by Salem View Post
    I guess that's why you're copying other people's code then.
    C program to calculate power of a number using recursion - C solved programs

    To be honest, you need to evaluate why you're even on this course if all you're going to do is copy/paste other people's work without understanding it. Assuming you're paying for your education, you're just wasting your time and money. The piece of paper at the end will be worthless.

    Here's a free snippet
    Code:
    if ( x % 2 == 0 ) {
      // x is even
    } else {
      // x is odd
    }
    unfortunately i admit that you are right but am just want learn something form c programming because my job is network administrator not coding

    however it still not working
    Code:
    ‘x’ undeclared (first use in this function)
                if ( x % 2 == 0 ) {
                     ^
    even.c:54:17: note: each undeclared identifier is reported only once for each fu

  7. #7
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Perhaps you should be learning C from a book, where you are taken through the most basic of concepts first.

    Tutorials (such as this) may also help you get started (hint: the problem you are having is explained on that page).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Recursion to find sum of digits of n digit number ?
    By justine in forum C Programming
    Replies: 7
    Last Post: 11-26-2012, 05:35 AM
  2. Replies: 9
    Last Post: 07-23-2011, 01:23 AM
  3. Is number power of 2?
    By Roaring_Tiger in forum C Programming
    Replies: 2
    Last Post: 08-25-2004, 11:38 AM
  4. power of 2? without loops or recursion
    By rahuls in forum C Programming
    Replies: 8
    Last Post: 03-20-2003, 05:10 PM
  5. power recursion
    By datainjector in forum C Programming
    Replies: 2
    Last Post: 11-06-2002, 04:56 PM

Tags for this Thread