Thread: Can Anybody help me out, in converting my code to iterative method.

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    5

    Can Anybody help me out, in converting my code to iterative method.

    Hello,

    I am new on programming, is currently using dev c++. I was asked to write a method to find x^y, where y is a power of x. Using recursive and iterative solution. I already have the recursive one, but I don't know how to make it to iterative. Here is my recursive solution.
    Code:
    #include<stdio.h>
    
    
    int power (int, int);
    
    int main ()
    
    {
    	int base, exp, result;
    
    	
    	printf("Please enter a base number\n");
    	scanf("%d",&base);
    	printf("Please enter a exponent number\n");
    	scanf("%d",&exp);
    
    	result = power(base, exp);
    
    
    	printf("\n The result is:%d\n",result);
    	
    	system("pause");
        return 0;
    }
    
    
    
    int power (int base, int exp)
    {
       if(exp >= 1)
           return base * (power(base,exp - 1));
    
       else
           return 1;
    }
    Help would highly be appreciated. Thanks in advance..
    Last edited by Salem; 03-14-2011 at 09:47 AM. Reason: Added [code][/code] tags

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. cygwin on win64
    By Vanzemljak in forum Tech Board
    Replies: 3
    Last Post: 01-12-2011, 04:28 PM
  2. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  3. Converting C++ code to C
    By alixi in forum C Programming
    Replies: 3
    Last Post: 09-21-2004, 02:23 PM
  4. True ASM vs. Fake ASM ????
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 04-02-2003, 04:28 AM
  5. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM

Tags for this Thread