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
@ensar why dont you post what you have done till now so ppl can comment on that and maybe help you better. :)
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
@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
@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?
oh sorry for not posting that... @MicroBot I am learning Python through the course Into Computer Science (OCW) (6.00)
@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}.
get a scratch from that and when I come back if you still need help ill be happy to do it with you. Laterz!
@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)
@Ensar are you allowed to use for loops also?
@MicroBot actually yes, I just checked
Also are you supposed to get negatives x from the user?
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
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...
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.
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 .
@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?
hmm ok ...let me know how it goes...sec
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.)
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?
plus declare pwr also before or after line 4....
do not indent line 18 it should be outside all loops.
but then pwr is not defined at all :/
define it at the start with root variable
Join our real-time social learning platform and learn together with your friends!