Quantcast
Channel: Active questions tagged gcc - Stack Overflow
Viewing all articles
Browse latest Browse all 21994

Why is my factorial program working but my almost identical pow program is not working?

$
0
0

Here is my factorial program—this is executing and giving a correct result:

#include <stdio.h>

int main()
{
    int n;

    printf("enter the no=");
    scanf("%d", &n);
    fun(n);
    printf("%d\n", fun(n));

    return 0;
}

int fun(int n)
{
    if(n == 0)
        return 1;
    else
        return fun(n - 1) * n;
}

This is my program to compute the power of a number—this is giving 0 instead of the correct result and yet is almost identical:

#include <stdio.h>

int main()
{
    int m, n;

    printf("enter the no=");
    scanf("%d%d", &m, &n);
    pow(m, n);
    printf("%d\n", pow(m, n));

    return 0;
}
int pow(int m, int n)
{
    if(n == 0)
        return 1;
    else
        return pow(m, n - 1) * m;
}

Both are running on same compiler.

Why is my factorial program working but my almost identical power program is not working?


Viewing all articles
Browse latest Browse all 21994

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>