southstarangel
southstarangel

Reputation: 11

How to turn an int to series of numbers?

I am trying to write a Java program that turns a number into series of numbers.

So for example

int number = 0;

Scanner input = new Scanner(System.in);

System.out.println("Enter your number: ");
number = input.nextInt();

Now for example the user typed the number 4, then I want to the program to print it like this:

1 2 3 4

How can I do this?

Upvotes: 0

Views: 103

Answers (1)

Aditya
Aditya

Reputation: 205

I hope this is what you were looking for

public void printSeriesOfNumbers(int n) {
   for(int i=1; i<=n; i++){
       System.out.print(i + " ");
   }
}

Upvotes: 1

Related Questions