Do while challenge | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Do while challenge

The challenge asked to make this in a do-while loop. I was not able to do this with a do-while loop but I was able to achieve this with a while and if loop. I was wondering if anyone could tell me what I was doing wrong or not understanding. Here was the problem Write a program that takes N numbers as input and outputs the numbers from N to 0, skipping the ones that are multiple of 3. Sample Input 7 Sample Output 7 5 4 2 1 0 https://code.sololearn.com/cJN9n08f7H5W/?ref=app

4th Dec 2020, 8:37 PM
Bobby Dixon
Bobby Dixon - avatar
11 Answers
+ 4
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner read = new Scanner(System.in); int number = read.nextInt(); do{ if(number == 0){ System.out.println(number); }else if(number%3==0){ //Please Subscribe to My Youtube Channel //Channel Name: Fazal Tuts4U }else{ System.out.println(number); } number--; }while(number >= 0); } }
3rd Sep 2021, 4:35 PM
Fazal Haroon
Fazal Haroon - avatar
+ 2
Your code works with do while loop: https://code.sololearn.com/c86OR3p6ImJK/?ref=app
4th Dec 2020, 9:36 PM
JaScript
JaScript - avatar
+ 2
This program using For loop import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner read = new Scanner(System.in); int number = read.nextInt(); //your code goes here for (int i = number; i >= 0; i--){ if (i%3==0){ continue; }else{ System.out.println(i); } } System.out.println(0); } }
27th May 2021, 6:16 PM
Abhishek Biswas
Abhishek Biswas - avatar
+ 1
Simple interchange condition in reverse do{ if ( i ==3) i--; System.out.println(i); i--; if (i%3 == 0 && i != 0) i--; continue; } while (i >=0 ); Syntax : while(condition) { //body } do{ //body } while(condition) ;
4th Dec 2020, 9:22 PM
Jayakrishna 🇮🇳
+ 1
Dan Carney cuz 0/3 is also 0, so it doesnt get printed out
31st Jan 2021, 1:00 PM
No Way!
No Way! - avatar
0
I'm struggling with this one, when I run the code it gives the output correctly except it will not return a value of 0 and I don't understand why. When number decreases to value 1, it will go through the while loop, print 1, decrease to 0. Now number equals 0, the condition says that when number is equal to or greater than 0 to run the while loop. I expect that it would go through the while loop, and print. What am I missing? Any help is appreciated. import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner read = new Scanner(System.in); int number = read.nextInt(); //your code goes here while (number >= 0) { if (number % 3 == 0) { number--; continue; } System.out.println(number); number--; } } }
6th Jan 2021, 3:20 PM
Dan Carney
Dan Carney - avatar
0
I quite don't understand why "--i:" it is used at the end of every single block. WHy don't use it only one time in loop?
18th Mar 2021, 11:51 AM
Yeisit
Yeisit - avatar
0
I was struggling too, that program doesn't print 0, but you need only to add: if(number%3 != 0 || number == 0)
18th Mar 2021, 1:00 PM
Vo Timur
Vo Timur - avatar
0
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner read = new Scanner(System.in); int number = read.nextInt(); //your code goes here do{ if(number % 3 != 0 || number == 0){ System.out.println(number); } number--; }while(number >= 0); } }
10th Aug 2021, 12:55 AM
Nicholas Rodriguez
Nicholas Rodriguez - avatar
0
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner read = new Scanner(System.in); int number; //your code goes here do{ number = read.nextInt(); for(number = number; number >=0; number--) { if (number%3!=0 || number==0){ System.out.println(number); } } } while(number>0); } }
11th Sep 2021, 3:00 PM
Jaime Tovar
Jaime Tovar - avatar
0
i used a for loop import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner read = new Scanner(System.in); int number = read.nextInt(); for (int x = number; x >= 0; x--) { if (x % 3 == 0 && x != 0) { continue; } System.out.println(x); } } }
15th Feb 2022, 6:01 PM
francisco espino
francisco espino - avatar