package displaynumbers;
import java.util.Scanner;
public class DisplayNumbers {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter a number: ");
int n = input.nextInt();
// Using a for loop
for (int i = 1; i <= n; i++) {
System.out.print(i + " ");
}
// Using a while loop
int i = 1;
while (i <= n) {
System.out.print(i + " ");
i++;
}
// Using a do-while loop
i = 1;
do {
System.out.print(i + " ");
i++;
} while (i <= n);
System.out.println();
}
}
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter