Advertisement
fosterbl

ForLoop2.java

Oct 8th, 2019
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.52 KB | None | 0 0
  1. /*
  2. Prompt the user and receive input for an int variable n. Write a loop that prints 1 through n, separated by commas. E.g., for n = 9 print 1, 2, 3, 4, 5, 6, 7, 8, 9
  3. */
  4. import java.util.Scanner;
  5.  
  6. public class ForLoop2{
  7.    public static void main(String[] args){
  8.       Scanner kb = new Scanner(System.in);
  9.       System.out.print("Enter a number: ");
  10.       int n = kb.nextInt();
  11.      
  12.       for(int i = 1; i <= n; i++){
  13.          System.out.print(i);
  14.          if( i != n ) System.out.print(", ");
  15.       }
  16.      
  17.    }
  18. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement