in Algorithms edited by
3,627 views
5 votes
5 votes

Study the following program

//precondition: x>=0
public void demo(int x)
{
    System.out.print(x % 10); 
    if (x % 10 != 0)
    {
        demo(x/10);
    }
    System.out.print(x%10);
}

Which of the following is printed as a result of the call demo $(1234)$?

  1. $1441$
  2. $3443$
  3. $12344321$
  4. $43211234$
in Algorithms edited by
3.6k views

2 Comments

it will print "4321001234" because demo(0) function call is also executing...
0
0
At demo(0) loop terminate by giving two zero as output.. I think so..
0
0

3 Answers

7 votes
7 votes

Option D

it Prints 43211234 as output

4 Comments

yeah its 4321001234 and not 43211234.
0
0
It's 4321001234 but i don't know why, please explain.
0
0

original question is 

//precondition: x>=0
public void demo(int x)
{
    System.out.print(x % 10);     
    if (x / 10 != 0)
    {
        demo(x/10);
    }
    System.out.print(x%10);
}
0
0
0 votes
0 votes

https://ide.geeksforgeeks.org/ifEWm3t0nr

Visit this link to run and check the program.

Answer is D

1 comment


if (x%10 != 0) then o/p 4321001234

if (x/10 != 0) then o/p 43211234

1
1
0 votes
0 votes
It prints 4321001234
Answer:

Related questions