1
#include <stdio.h>

void power(int *x,int *y);
 
int main()

{

    int m,n;
    printf("Enter the number and power :-");
    scanf("%d%d",&m,&n);
    power(&m,&n);
    
}

void power(int* x, int* y)

{

    int i,result=1;
    if(*y==0)
        printf("The result = 1");
    else
    {
        for(i=0; i<*y; i++)
        {
            result *= (*x);
        }
        printf("The result of %d^%d = %d",*x,*y,result);
    }
}

The result of 10^10 comes out to be 1410065408 I don't know what am I doing wrong

bruno
  • 32,421
  • 7
  • 25
  • 37
  • Have you tried running your code line by line in a debugger while monitoring the values of all variables? If not, then you may want to read this: [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/q/25385173/12149471) You may also want to read this: [How to debug small programs](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/) – Andreas Wenzel Aug 13 '20 at 18:17
  • you have an overflow, replace `int` by `long long` or `int64_t` to be sure and you will have the right result – bruno Aug 13 '20 at 18:18
  • 10^10 does not fit the 32-bit `int` variable. – Weather Vane Aug 13 '20 at 18:19
  • On most platforms, [`INT_MAX`](https://en.cppreference.com/w/c/types/limits) is defined as 2.1 billion, which is 9 decimal places. This means that an `int` can represent a number between `-2,147,483,648` and `+2,147,483,647`. The result of your mathematical operation falls outside that range. – Andreas Wenzel Aug 13 '20 at 18:22
  • 2
    Ignoring UB from overflow ... `(10*10*10*10*10*10*10*10*10*10) % (2^32) = 1410065408` ==> `10^10 = 2*4294967296 + 1410065408` – pmg Aug 13 '20 at 18:23
  • 1
    In C, there is no such thing as *call by reference*. – M. Nejat Aydin Aug 13 '20 at 18:46

3 Answers3

2

you visibly have an overflow, probably your intare on 32bits

using long long :

#include <stdio.h>

void power(long long *x,long long *y);
 
int main()

{

    long long m,n;
    printf("Enter the number and power :-");
    scanf("%lld%lld",&m,&n);
    power(&m,&n);
    
}

void power(long long* x, long long* y)

{

    long long i,result=1;
    if(*y==0)
        printf("The result = 1");
    else
    {
        for(i=0; i<*y; i++)
        {
            result *= (*x);
        }
        printf("The result of %lld^%lld = %lld",*x,*y,result);
    }
}

Compilation and execution:

pi@raspberrypi:/tmp $ gcc -Wall p.c
pi@raspberrypi:/tmp $ ./a.out
Enter the number and power :-10 10
The result of 10^10 = 10000000000pi@raspberrypi:/tmp $ 

To be sure to use 64b int you can use int64_t

bruno
  • 32,421
  • 7
  • 25
  • 37
  • I guess you don't need to use `long long` to pass the base & the power. Using an integer here will be memory saving. – Rohan Bari Aug 13 '20 at 18:34
  • @RohanBari I admit I did a lazy global change, anyway the OP using big numbers the base can need a long long already without producing overflow because power enough small, and why not having the base 0 and a power requiring a long long too ? :-) – bruno Aug 13 '20 at 18:36
1

You're using int for the result, and it doesn't fit. You need to use long. The size of int is 32 bits which has a max signed value of 2147483647.

rallen911
  • 148
  • 9
1

Your given program uses 4-bytes integer which is capable to hold the values between -2,147,483,648 to 2,147,483,647 but 10^10 is too large to hold it.

Taking the appropriate type (such as long long) to hold such large outcome will easily solve your issue (notice the comments as an explanation to the code):

#include <stdio.h>

// The function signature
void power(int *x, int *y);

int main(void) {
    int bs, pw;
    
    printf("Enter the value of base and power: ");

    // Verifying the inputs
    if (scanf("%d%d", &bs, &pw) != 2)
        // If the values were incorrectly entered, then it returns
        // with an error code without further actions
        return 1;
        
    power(&bs, &pw);
    
    return 0;
}

// The function definition    
void power(int *x, int *y) {
    long long result = 1;

    // Dereferencing and using assignment operator for self-multiplication with
    // the dereferenced 'x'
    for (int i = 0; i < *y; i++)
        result *= *x;
    
    printf("Results: %lld\n", result);
}

A sample test case:

Enter the value of base and power: 10 10
Results: 10000000000

Works on OnlineGDB as well.

Rohan Bari
  • 7,482
  • 3
  • 14
  • 34