Thread: exp

  1. #1
    Registered User
    Join Date
    Nov 2015
    Posts
    82

    exp

    i wrote this program, it works fine, but prints 0 when i enter "2, 32". is this because of the length of the number that is returned?

    Code:
    #include <stdio.h>#include <math.h>
    #include <stdlib.h>
    
    
    int expon(int x, unsigned y);
    
    
    int main() {
    	unsigned expo;
    	int base;
    	scanf("%d %u",&base,&expo);
    	printf("result is %d",expon(base,expo));
    	return 0;
    }
    
    
    int expon(int x, unsigned y) {
    	int res=1;
    	for (int i=1;i<=y;i++){res*=x;}
    	return res;
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Probably. Print INT_MAX and compute 2 to the power of 32 yourself and you will see.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed