The program shows how to Generate First n Whole Numbers in java. The program is pretty simple n is already defined in a program. Although you can use the scanner class to take the inputs from the user.
If you do not know the working process of do-while loop then click the link below.
Java Program to Generate First n Whole Numbers
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | //generate first n whole numbers using do-while loop public class DoWhileNNumbers { public static void main(String args[]) { int num = 0; int N = 10; System.out.println("First 5 whole Number "); do { System.out.print(num + " "); num++; // Incrementing } while ( num <= N ); } } |
Output:
First 5 whole Number
0 1 2 3 4 5 6 7 8 9 10