Helllo,
I have easy question, but can't find the solution. Need to print all numbers from 1 to N (10) but not in a row. The output needs to bee in single line separated by spaces - 1 2 3 4 5...
let n = 10; for (let i = 1; i <= n; i++) { console.log(i); }Can I do it without using array?
asked Dec 10, 2018 at 20:42
Николай МатевНиколай Матев
5712 gold badges9 silver badges20 bronze badges
1
Concatenate with a string in the loop instead, then console.log that string:
let n = 10; let str = ''; for (let i = 1; i <= n; i++) { str += i + ' '; } console.log(str.trim());
answered Dec 10, 2018 at 20:43
1
A different approach could be to create an array using fill and map to create an array, then join the values to create the string.
let n = 10 let r = new Array(n).fill(null).map((val, idx) => idx + 1).join(' ') console.log(r)
answered Dec 10, 2018 at 20:53
Get Off My LawnGet Off My Lawn
31.8k34 gold badges156 silver badges297 bronze badges
1
View Discussion
Improve Article
Save Article
View Discussion
Improve Article
Save Article
There are 2 methods to take input from the user which are separated by space which are as follows:
- Using BufferedReader Class and then splitting and parsing each value
- Using nextInt( ) method of Scanner class
Let us discuss both the methods one by one in order to get a better understanding by implementing the same clean java programs.
Method 1:
Using BufferedReader Class and then splitting and parsing each value.
Procedure:
- Using readline() method of BufferedReader and Scan the whole string.
- Split this String for str.split(” “)
- Iterate over the above array and parse each integer value using Integer.parseInt( ).
Example
Java
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
class GFG {
public static void main(String[] args)
throws IOException
{
BufferedReader bi = new BufferedReader(
new InputStreamReader(System.in));
int num[] = new int[10];
String[] strNums;
System.out.println("enter string of numbers");
strNums = bi.readLine().split(" ");
for (int i = 0; i < strNums.length; i++) {
num[i] = Integer.parseInt(strNums[i]);
}
System.out.println("printing stored numbers ");
for (int i = 0; i < strNums.length; i++) {
System.out.println(num[i]);
}
}
}
Output:
FIG = OUTPUT OF METHOD 1
Method 2: Using nextInt() method of Scanner class.
Procedure:
- Using the nextInt() method of Scanner class and scan to scan the input.
- Using for loop to store input in an array.
- Iterate through the above array and parse each integer using sc.nextInt()
Example
Java
import java.io.IOException;
import java.util.Scanner;
class GFG {
public static void main(String[] args)
throws IOException
{
System.out.println("enter input ");
Scanner sc = new Scanner(System.in);
int[] nums = new int[10];
int i;
for (i = 0; i < nums.length; i++) {
nums[i] = sc.nextInt();
}
System.out.println("printing stored values");
for (i = 0; i < nums.length; i++) {
System.out.println(nums[i] + " ");
}
}
}
Output:
FIG = OUTPUT OF METHOD 2
// Initializing variables long startTime, endTime; // Start time startTime = System.nanoTime(); { // Insert code here // Method 1 or method 2 code } // End time endTime = System.nanoTime();Note: The first method using Bufferedreader class and then splitting and parsing each value is much faster than using nixing() method of Scanner class. It is nearly 2 times faster than the second one. Bel;ow we do provide in order how to calculate the time consume by both methods by using nanotime method