Posted by Jason on 14:37:00 09-08-2002
I just started learning C today! I'm so happy! I was making a calculator and I don't know how to make exponents. I have just the code, but I don't know how to tell the computer that its an exponent. Here's the code I have so far (It just asks for two numbers. First one (a) is supposed to be the number, second is the exponent, which is b. The answer is c.).
Code:
#include <stdio.h>
int main()
{
int a, b, c;
printf("Enter the number:");
scanf("%d", &a);
printf("Enter the exponent:");
scanf("%d", &b);
c = a ** b; /* This is the exponent line. I don't know what to do here in order to make an exponent. */
printf("%d with an exponent of %d = %dn", a, b, c);
return 0;
}
It'd be good if you could tell me how to make exponents, thanks!
_________________
Yes, I know. I am only 10 years old, but hey, this is the site for YOUNG programmers.
Visit my forum: Chat Zone
[ This Message was edited by: Jason on 2002-09-08 14:50 ]
Posted by sg on 15:10:00 09-08-2002
c = a ** b; .. i think that this line is not right, what is ** ? i think you should try ---->--> c = a ^ b;
That should work.
Posted by seunosewa on 18:03:00 09-08-2002
No, no. Were you joking, sg?
Something like this is more likely to work:
//At the beginning of your program:
#include <math.h>
...
...
...
c = pow (a, b);
...
...
[ This Message was edited by: seunosewa on 2002-09-08 18:09 ]
Posted by Jason on 04:54:00 09-09-2002
Thanks, seunosewa! It worked! I owe you one .
[addsig]
Posted by sg on 06:29:00 09-09-2002
sorry about that , but Jason here is another implementation of the exponent function without using the math.h header file.
#include
int a, b, c;
int pow(int,int);
int main()
{
printf("Enter the number:");
scanf("%d", &a);
printf("Enter the exponent:");
scanf("%d", &b);
c = pow(a,b); /* this calls the function pow, which i have created not the one present in the math.h header file */
printf("%d with an exponent of %d = %d", a, b, c);
return 0;
}
int pow(int a,int b)
{
int reslt = 1,count;
for(count = a; b -->= 1 ; b--)
reslt = (reslt * a);
return (reslt);
}
i hope you like this example better, do copy it and play around with the codei have pasted ....
Posted by seunosewa on 08:20:00 09-09-2002
Well, I would say that its better to include the math.h header file under normal conditions ... and I can't think of any exception right now (hint, hint)
Posted by AntiHalcyon on 08:13:00 09-15-2002
Couldn't you just do something like:
int a=input number;
int b=exponent;
int x;
int answer;
answer=a;
for(x=0; x<b; x++){
answer*=answer;
}
if(b==0){
answer=1;
}
if(b==1){
answer=a;
}
printf("answer:%i", answer);
_________________
Huh? Stop confusing me.
[ This Message was edited by: AntiHalcyon on 2002-09-15 08:18 ]
Posted by Smerdyakov on 09:50:00 09-15-2002
Obviously that won't work for exponents that aren't nonnegative integers. Also, for exponents that are non-tiny but small enough that the result of exponentiation will fit in an integer, the floating point exp might be faster, since it will use fancy tables 'n' stuff.
Posted by AntiHalcyon on 03:48:00 09-16-2002
True... Well I was wrong as usual.
[addsig]