Click here to Skip to main content
15,900,500 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Write a program using only while loops to print all the even numbers from 1 to n

Input

10

Output

2

4

6

8

10

What I have tried:

I AM GET

TING ERROR
 v=int( input("153"))
rev=0

while 0!=v:

    r=v%10

rev=rev*10+rev
v=v/10
print(rev)
Posted
Updated 10-Apr-22 1:29am
Comments
CPallini 10-Apr-22 10:46am    
Copying a flawed, unrelated Python program while a Java program is required doesn't look a good practice, to me.

Quote:
What error I am having in BELOW CODE?

First of all, it is not Java code.
Then try to replace
Python
v=int( input("153"))
rev=0

while 0!=v:

    r=v%10

rev=rev*10+rev
v=v/10
print(rev)

With
Python
v=int( input("153"))
rev=0
while 0!=v:
    r=v%10
    rev=rev*10+rev
    v=v/10
    print(r)

Quote:
Write a program using only while loops to print all the even numbers from 1 to n

Your main problem is that this code do not fit the requirement, not even remotely.
Read again requirement and try again.
 
Share this answer
 
Comments
CPallini 10-Apr-22 10:44am    
5.
Patrice T 10-Apr-22 11:55am    
Thank you.
You have tagged your question "Java", but the code is Python. Also, your code has nothing to do with the question. You are supposed to print all even numbers, so dividing and multiplying by ten will not produce the correct results. Your loop should start at 2, increment by 2, and stop when the loop value is equal to, or greater than, the control number that is first input.
 
Share this answer
 
Comments
CPallini 10-Apr-22 10:44am    
5.
Richard MacCutchan 10-Apr-22 11:34am    
Thanks, but my "solution" is only a rewording of the original question.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900