Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

andrewface

macrumors 6502
Original poster
May 17, 2006
284
56
i have an assignment due for school and i cant figure out one question (has to be in c)

can someone help me write a program that takes two numbers that are input by the user and raises one to the power of the other...this cant be done with the exponent function it must be done by using looping

thanks :apple:
 

mduser63

macrumors 68040
Nov 9, 2004
3,042
31
Salt Lake City, UT
Are they both integers? If so, it's relatively easy and you should figure it out on your own (sorry). If not, I have to figure you're in a pretty advanced programming class...
 

XnavxeMiyyep

macrumors 65816
Mar 27, 2003
1,131
4
Washington
Let's assume these are positive integers. I don't want to ruin the whole thing for you, but I suggest using a for loop. You will need four variables, the two integers the user inputs, one two count in the for loop, and one for the final result. Have fun!

EDIT: Now that I see your previous message, do you know how to make for loops?
 

andrewface

macrumors 6502
Original poster
May 17, 2006
284
56
Let's assume these are positive integers. I don't want to ruin the whole thing for you, but I suggest using a for loop. You will need four variables, the two integers the user inputs, one two count in the for loop, and one for the final result. Have fun!

awwwww!!! i got that far...i just cant get it to work uhg
 

CaptainZap

macrumors regular
Jan 17, 2007
170
0
Let's assume these are positive integers. I don't want to ruin the whole thing for you, but I suggest using a for loop. You will need four variables, the two integers the user inputs, one two count in the for loop, and one for the final result. Have fun!

Well you could have three...
 

andrewface

macrumors 6502
Original poster
May 17, 2006
284
56
Let's assume these are positive integers. I don't want to ruin the whole thing for you, but I suggest using a for loop. You will need four variables, the two integers the user inputs, one two count in the for loop, and one for the final result. Have fun!

EDIT: Now that I see your previous message, do you know how to make for loops?

yes i know the theory but this is my first time trying...i get very confused with the variables tho

i want the program to go

enter a number: x
enter exponent: y

x to the power of y: answer

for (initial statement ; condition ; iterating statement) {
body
} right?
 

XnavxeMiyyep

macrumors 65816
Mar 27, 2003
1,131
4
Washington
awwwww!!! i got that far...i just cant get it to work uhg
Okay. Assuming you already know how to get the user to input the values, the for loop should repeat until it goes through as many times as the exponent's value, and each time, it should multiply an additional integer (which should be initialized to 1) by the base number that the user inputted. This additional integer would be the answer.

This would total four integers including the one that's being used to count in the for loop. CaptainZap, how would you do it with three? (I am no expert)
 

andrewface

macrumors 6502
Original poster
May 17, 2006
284
56
Okay. Assuming you already know how to get the user to input the values, the for loop should repeat until it goes through as many times as the exponent's value, and each time, it should multiply an additional integer (which should be initialized to 1) by the base number that the user inputted.

This would total four integers including the one that's being used to count in the for loop. CaptainZap, how would you do it with three? (I am no expert)

yes i know how to input values...can you show me what you mean for the for loop?...thats where i get confused the initializing
 

CaptainZap

macrumors regular
Jan 17, 2007
170
0
This would total four integers including the one that's being used to count in the for loop. CaptainZap, how would you do it with three? (I am no expert)

One for the Number/Result, one for the Exponent, and then another variable. I'll message you it since I don't want to spoil it for Andrew.
 

XnavxeMiyyep

macrumors 65816
Mar 27, 2003
1,131
4
Washington
can someone show me the code?
I think I realize what CaptainZap is saying, but for now, I think you should stick with the four variable method.

Time for some of the code, mixed with some pseudo code.

int base = input from user;
int exponent = input from user;
int i =0;
int result = 1;

//For loop goes here, which will modify the result variable

output result;

See if you can figure it out from here (not trying to be mean; just trying to teach a bit)
 

andrewface

macrumors 6502
Original poster
May 17, 2006
284
56
k i dont understand loops

#include <stdio.h>
main()
{
int n, b, e, i=0;

printf("Please enter a number:");
scanf("%d\n", &b);
printf("Please enter an exponent:");
scanf("%d", &e);

for (i=1
 

XnavxeMiyyep

macrumors 65816
Mar 27, 2003
1,131
4
Washington
k i dont understand loops

#include <stdio.h>
main()
{
int n, b, e, i=0;

printf("Please enter a number:");
scanf("%d\n", &b);
printf("Please enter an exponent:");
scanf("%d", &e);

for (i=1
First of all, you will find that, with the exception of counting variables, like i, you want to use descriptive words to name your variables instead of letters, or confusion will ensue!

Also, the result (which I assume is n), needs to be initialized to 1, not 0. Otherwise, when you multiply it by the base over and over again (HINT HINT) , it would just stay at 0.

for(i=0;i<e;i++) //Although, e should really be named exponent
{
//You're really close now. You can do this with just one line of code.
}
 

andrewface

macrumors 6502
Original poster
May 17, 2006
284
56
#include <stdio.h>
main()
{
int answer=1, base, exponent, i=0;

printf("Please enter a number:");
scanf("%d\n", &base);
printf("Please enter an exponent:");
scanf("%d", &exponent);

for (i=1;i<exponent;i++) {

printf("%d", base*i);
}

return 0;
}


i know thats wrong...why am i so bad at this?...im having alot of trouble understanding loops
do i put i=0 at the beginning like that?
 

XnavxeMiyyep

macrumors 65816
Mar 27, 2003
1,131
4
Washington
#include <stdio.h>
main()
{
int answer=1, base, exponent, i=0;

printf("Please enter a number:");
scanf("%d\n", &base);
printf("Please enter an exponent:");
scanf("%d", &exponent);

for (i=1;i<exponent;i++) {

printf("%d", base*i);
}

return 0;
}


i know thats wrong...why am i so bad at this?...im having alot of trouble understanding loops
Okay, a couple things. The printf does not actually save any results, so all this will do is print out the base times i over and over again. Your printf should come after the loop, and you should just print out the answer. (So I guess I was sort of unclear about only needing one more line. You just needed one line IN THE LOOP)

To calculate the answer, you want to keep multiplying it by the base over and over again (as this is how exponents are calculated). Good luck.
 

andrewface

macrumors 6502
Original poster
May 17, 2006
284
56
Okay, a couple things. The printf does not actually save any results, so all this will do is print out the base times i over and over again. Your printf should come after the loop, and you should just print out the answer. (So I guess I was sort of unclear about only needing one more line. You just needed one line IN THE LOOP)

To calculate the answer, you want to keep multiplying it by the base over and over again (as this is how exponents are calculated). Good luck.
ok so

answer=base*i

}

printf("%d", answer); ??
 

andrewface

macrumors 6502
Original poster
May 17, 2006
284
56
#include <stdio.h>
main()
{
int answer=1, base, exponent, i=0;

printf("Please enter a number:");
scanf("%d\n", &base);
printf("Please enter an exponent:");
scanf("%d", &exponent);

for (i=0;i<exponent;i++) {
answer=base*answer;

}
printf("%d", answer);
return 0;
}

//hmm ok it works now theres just a problem with my inputs
i get this
Please enter a number:2
2 //should be 2nd input after asks for exponent
Please enter an exponent:4 //answer
q2 has exited with status 0.
 

XnavxeMiyyep

macrumors 65816
Mar 27, 2003
1,131
4
Washington
#include <stdio.h>
main()
{
int answer=1, base, exponent, i=0;

printf("Please enter a number:");
scanf("%d\n", &base);
printf("Please enter an exponent:");
scanf("%d", &exponent);

for (i=0;i<exponent;i++) {
answer=base*answer;

}
printf("%d", answer);
return 0;
}

This code should work correctly.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.