Computer Science, asked by 21730676, 8 months ago

pleaseee helppp
Consider the following code. What could be the input of the user in order to get the

following output (in order): 8 4 2. Give two possible inputs.

Scanner input = new Scanner(System.in);

System.out.print("Enter a positive integer: " );

int n = input.nextInt();

while (n > 0) {

int d = n % 10;

if (d % 2 == 0)

System.out.print(d + " ");

n /= 10;

}

Answers

Answered by nishantmehta12057
0

Answer:

pleaseee helppp

Consider the following code. What could be the input of the user in order to get the

following output (in order): 8 4 2. Give two possible inputs.

Scanner input = new Scanner(System.in);

System.out.print("Enter a positive integer: " );

int n = input.nextInt();

while (n > 0) {

int d = n % 10;

if (d % 2 == 0)

System.out.print(d + " ");

n /= 10;

..........

Answered by pesh20gathoni
0

Answer:

Input : 248

Explanation:

{

Scanner input = new Scanner(System.in);

System.out.print("Enter a positive integer: " );

int n = input.nextInt();

while (n > 0) {

int d = n % 10;

if (d % 2 == 0)

System.out.print(d + " ");

n /= 10;

}

consider above code line by line,

when we give input as 248 input below operations will happen by loop wise.

While loop 1st iteration :

// while loop will check (248 > 0) , it's true , so loop iteration will start.

while (n > 0) {  

// d = n % 10; i.e. 248 % 10 = 8 , so d = 8

int d = n % 10;

// if (8 % 2 == 0) true,

if (d % 2 == 0)

// value of 'd' will be print i.e. 8

System.out.print(d + " ");

// n/=10 will do n= n/10 i.e. 248/10= 24.8

n /= 10;

}

While loop 2nd iteration :

// while loop will check (24.8 > 0) , it's true , so loop iteration will start.

while (n > 0) {  

// d = n % 10; i.e. 24.8 % 10 = 4.8 , but d is an int type veriable so  it's store only int value i.e. d = 4

int d = n % 10;

// if (4 % 2 == 0) true,

if (d % 2 == 0)

// value of 'd' will be print i.e. 4

System.out.print(d + " ");

// n/=10 will do n= n/10 i.e. 24.8/10= 2.48

n /= 10;

}

While loop 3rd iteration :

// while loop will check (2.48 > 0) , it's true , so loop iteration will start.

while (n > 0) {  

// d = n % 10; i.e. 2.48 % 10 = 2.48 , but d is an int type variable so  it's store only int value i.e. d = 2

int d = n % 10;

// if (2 % 2 == 0) true,

if (d % 2 == 0)

// value of 'd' will be print i.e. 2

System.out.print(d + " ");

// n/=10 will do n= n/10 i.e. 2.48/10= 0.248

n /= 10;

}

While loop 4th iteration :

// while loop will check (0.248 > 0) , it's not true , so loop iteration will end.

while (n > 0) {  

So, Output of this program is 8 4 2 if input will be 248

Similar questions