Ask your own question, for FREE!
MIT 6.00 Intro Computer Science (OCW) 18 Online
OpenStudy (anonymous):

Hello everyone, I am new here and I've found this problem: Write a program that asks a user to enter an integer and prints two integers, PWR and ROOT, such that 0

OpenStudy (microbot):

@ensar why dont you post what you have done till now so ppl can comment on that and maybe help you better. :)

OpenStudy (anonymous):

TO FIND THE ROOT YOU HAVE TO ENTER TWO NUMBERS FROM USER USING" PRINTF & SCANF" FUNCTIONS USING"STDIO.H". YOU CAN USE "POW(A,B)" FUNCTION TO GET POWER IN"#INCLUDE<MATH.H>. TRY IT

OpenStudy (anonymous):

@MicroBot this is exactly why I haven't posted my code. @saurav151 this is kind of more complex than I expected it to be since it should be something really simple i.e. using the while loop and maybe conditionals

OpenStudy (microbot):

@saurav151 "asks a user to enter an integer and prints two integers". i dont think that is what Ensar needs. but it is a good start for him. Also, @Ensar what is the language you are using?

OpenStudy (anonymous):

oh sorry for not posting that... @MicroBot I am learning Python through the course Into Computer Science (OCW) (6.00)

OpenStudy (microbot):

@Ensar to help you start as i haven't done this course and have to run out for an hour or so...start understanding completely what you are asked to do. you want the user to enter 1 integer and for that integer you will have to get (if it exists) a couple of other integers. if your int is lets say N, then ROOT**PWR =N where PWR must be in the region of(0,6)...means {1,2,3,4,5}.

OpenStudy (microbot):

get a scratch from that and when I come back if you still need help ill be happy to do it with you. Laterz!

OpenStudy (anonymous):

@MicroBot thanks... here is what I've come around so far (it works for 27 and 4 but it is obviously not working properly - if I don't post anything after this, please feel free to correct this version or to point me to the right direction): x = int(raw_input('Enter an integer: ')) root = 0 pwr = 0 while root**pwr < x: root += 1 pwr += 1 print 'Current step =', root, pwr if root**pwr != x or pwr>6 or pwr<0: print 'No such integers exist' else: if x<0: root = -root pwr = pwr print 'Integers are ' + str(root) + ' and ' + str(pwr)

OpenStudy (microbot):

@Ensar are you allowed to use for loops also?

OpenStudy (anonymous):

@MicroBot actually yes, I just checked

OpenStudy (microbot):

Also are you supposed to get negatives x from the user?

OpenStudy (anonymous):

it is not mentioned in the task but it was my guess when I wrote down everything on paper while trying to understand the requirements

OpenStudy (microbot):

ok let me see it a bit more ...problem is i dont have the python compiler at all so not sure if my syntax will be right...

OpenStudy (microbot):

keep in mind that (for positive x) root will be 0<root<x so you do not need to check any further. and pwr is only 0<pwr<6...as i mentioned before 1 2 3 4 5. so try to put these into for loops maybe.

OpenStudy (microbot):

actually wait...i got an only while loops solution i think but only for positive ints atm. it is short and simple...so the same thing about ranges i mentioned above try to put it in while loops (nested). hope it works cause i cant try it here. if you don't get anything ask me to send you a pseudocode for it .

OpenStudy (anonymous):

@MicroBot... I am obviously not doing sth right and I've been knocking my head against the wall for more than an hour so could you please just send me the code?

OpenStudy (microbot):

hmm ok ...let me know how it goes...sec

OpenStudy (microbot):

http://pastebin.com/vLFCA1gk

OpenStudy (anonymous):

hmmm... it doesn't work. It said that 'pwr' is not defined for the line 18 so I adjusted the indent, but then it just prints out x = 27 (e.g.)

OpenStudy (microbot):

you did correct where needed my code syntactically right? plus sry i initialized pwr as 0 while it has to be initialized as 1...could that fix it?

OpenStudy (microbot):

plus declare pwr also before or after line 4....

OpenStudy (microbot):

do not indent line 18 it should be outside all loops.

OpenStudy (anonymous):

but then pwr is not defined at all :/

OpenStudy (microbot):

define it at the start with root variable

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!