Power function [pow()] always giving a ZERO as result

100 Views Asked by At

Power function always giving a zero(0) as result no matter the input.

I am learning the power function, but it doesn't work.please help.

#include <stdio.h>
#include <math.h>

int main()
{
    int A,B; 
    int C=pow(A,B);
    printf("To find power please enter values of A and B \n");
    printf( "Please enter value of A: \n");
    scanf("%d",&A);
    printf("Please enter value of B: \n");
    scanf("%d",&B);
    printf("The power of A and B is : %d",C);
    return 0;
}
2

There are 2 best solutions below

1
bco2135 On

You're setting the value of C before you assign the A and C values.

Put int C = pow(a, b); right before you run printf("The power of A and B is : %d",C);

Complete code

#include <stdio.h>
#include <math.h>

int main()
{
    int A, B;
    printf("To find power please enter values of A and B \n");
    printf("Please enter value of A: \n");
    scanf("%d", &A);
    printf("Please enter value of B: \n");
    scanf("%d", &B);
    int C = pow(A, B);
    printf("The power of A and B is : %d", C);
    return 0;
}

The tag powershell seems random, you should have the tag set to the language that you're using.

1
N-CODER On

I just now moved the Int C=pow(A,B); line just before printf("The power of A and B is : %d",C); Basically moved it from top to bottom. And it worked!