The Gateway to Computer Science Excellence
+5 votes
1.8k views

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 by
edited by | 1.8k views
0
it will print "4321001234" because demo(0) function call is also executing...
0
At demo(0) loop terminate by giving two zero as output.. I think so..

2 Answers

+7 votes

Option D

it Prints 43211234 as output

by
0
correct
+1


NO. It will print 4321001234

0
yeah its 4321001234 and not 43211234.
0
It's 4321001234 but i don't know why, please explain.
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 votes

https://ide.geeksforgeeks.org/ifEWm3t0nr

Visit this link to run and check the program.

Answer is D

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

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

Answer:

Related questions

Quick search syntax
tags tag:apple
author user:martin
title title:apple
content content:apple
exclude -tag:apple
force match +apple
views views:100
score score:10
answers answers:2
is accepted isaccepted:true
is closed isclosed:true
52,345 questions
60,495 answers
201,848 comments
95,307 users