Article View: comp.std.c
Article #4725HELP!! Beginner's question...
From: Ramon Mariano Jr
Date: Wed, 31 Jan 1996 08:57
Date: Wed, 31 Jan 1996 08:57
49 lines
2152 bytes
2152 bytes
Hello everybody!
I'm a beginning C-programmer and for the past couple of days, I've been
totally stuck in my program. The purpose of my program is to ask the user
for an integer 'i' and raise it by a power 'n'. I've been succesful in
computing 5 to the 6th power, 3 to the 4th power, and other small-sized
equations. However, when I try to raise 5 to the 7th power, I get "12589"
when it really should be "78125". My teacher told me that instead of using
plain 'int', I should use 'long int'. I tried that, but it's still not
working. If anyone can help me out here, I'd GREATLY appreciate it! I've
included my code below... (by the way, I'm not allowed to use the <math.h>
library)
------------------------------------------------------------------------
#include <stdio.h>
int main(void)
{
int i, /* integer */
n, /* power to raise integer */
i_total, /* integer's total value*/
count; /* keeps track of number of loops run */
/* asks for an integer and stores it in 'i' */
printf("Enter a non-negative integer: ");
scanf("%d", &i);
/* asks for a power and stores it in 'n' */
printf("What power should we raise it to? ");
scanf("%d", &n);
/* if 'n' is negative, program will treat it as a '0' */
if (n < 0){
n = 0;
}
i_total = 1; /* gives i_total an initial total of '1' */
for (count = 1; count <= n; count = count + 1){
i_total = i_total * i;
}
/* prints out the inputs and result */
printf("%d raised to the %dth power is %d\n\n", i, n, i_total);
return(0);
}
Technical Details
Message-ID:
<Pine.OSF.3.91l.960131005708.7552A-100000@saul3.u.washington.edu>Path:
arc1-ca.pugleaf.net!saul3.u.washington.edu!rmarianoShow Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!saul3.u.washington.edu!rmariano From: Ramon Mariano Jr <rmariano@u.washington.edu> Newsgroups: comp.std.c Subject: HELP!! Beginner's question... Date: Wed, 31 Jan 1996 00:57:49 -0800 Organization: University of Washington Message-ID: <Pine.OSF.3.91l.960131005708.7552A-100000@saul3.u.washington.edu> NNTP-Posting-Host: saul3.u.washington.edu Mime-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII NNTP-Posting-User: rmariano