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.io.InputStreamReader;
import java.util.StringTokenizer;
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
.
.
.
StringTokenizer st = new StringTokenizer(br.readLine());
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 :-

java.io.BufferedReader br = new java.io.BufferedReader(new java.io.InputStreamReader(System.in));

.
.
.

String s=(br.readLine()).split(" ");

int K=Integer.parseInt(s[0]);

int N=Integer.parseInt(s[1];

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:


BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String str = br.readLine();
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
i will ask you a question try to answer it
who taught you to program

somebody please upvote me , i have questions to ask
thank you

Hi, this is a platform to learn. We learn by asking what we do not know, don’t we? So, what is the problem in asking, irrespective of the question being silly (in the eyes of someone else)?

3 Likes

It is

input_numbers = map(int,raw_input().split());

you have to call a function with () even though arguements are not needed in python unlike ruby

is it python? then why everybody use semicolon.