 # How to accept a space separated integer

This is a beginner question but still i have problem while dealing with it.
for e.g , After T no. of cases,
i have to enter K N for each case
which are space separated integers.
How to do it?

1 Like

In C, you can simply use

``````scanf("%d%d", &K, &N);
``````

or

``````scanf("%d %d", &K, &N);
``````

or

``````scanf("%d", &K);
scanf("%d", &N);
``````

In C++, you can use

``````cin >> K >> N;
``````
4 Likes

In Java, you can use

``````import java.io.BufferedReader;
import java.util.StringTokenizer;
.
.
.
int K = Integer.parseInt(st.nextToken());
int N= Integer.parseInt(st.nextToken());
``````

In Python, you can use

``````[K, N] = [int(i) for i in raw_input().split()]
``````

or

``````K = input()
N = input()
``````
4 Likes

scanf("%d",&T);

for(i=0;i<T;i++)
{
scanf("%d %d",&K,&N);
}

Java :-

.
.
.

int K=Integer.parseInt(s);

int N=Integer.parseInt(s;

If Input is not very large which is normally the case,you can directly use Scanner to read the inputs.

``````Scanner sc=new Scanner(System.in);
``````

Now suppose you pass the input as

5 1 2 3 4 5

Now to take this input just keep on doing sc.nextInt()/sc.nextLong() and you will get the desired element from the input in your program.

if you are using java use split fn;
eg:

``````
String[] arr = str.split("\\s+");

``````

values will be stored in array arr.

In Python:

``n,k = map(int,raw_input().split);``
1 Like

how many times will you be asking questions
dont ask such silly questions after this
who taught you to program