Logo

Programming-Idioms

This language bar is your friend. Select your favorite languages!

Idiom #120 Read integer from stdin

Read an integer value from the standard input into the variable n

(def n (Integer/parseInt (read-line)))
#include <stdio.h>
int n[15];
fgets(n, 15, stdin);
#include <stdio.h>
int n;
scanf("%d", &n);
IDENTIFICATION DIVISION.
PROGRAM-ID. stdin.
PROCEDURE DIVISION.
    ACCEPT n
STOP RUN.
#include <iostream>
std::cin >> n;
using System;
n = int.Parse(Console.ReadLine());
import std.stdio;
readf("%d", &n);
import 'dart:io';
String? s = stdin.readLineSync();  
int? n = int.tryParse(s ?? "");
n = String.to_integer IO.gets ""
   integer :: n
   read (*,*) n
import "fmt"
_, err := fmt.Scan(&n)
import "fmt"
_, err := fmt.Scanf("%d", &n)
n <- (read :: String -> Int) <$> getContents
const {createInterface} = require('readline')

const rl = createInterface ({
  input: process.stdin,
  output: process.stdout
})

rl.question('Input an integer: ', response => {
  let n = parseInt (response)
  // stuff to be done with n goes here

  rl.close()
})
import java.util.Scanner;
Scanner in = new Scanner(System.in);
int n = in.nextInt();
import java.util.Scanner;
Scanner in = new Scanner(System.in);
n = in.nextInt();
import static java.lang.Integer.parseInt;
import java.util.Scanner;
Scanner s = new Scanner(System.in);
int n = parseInt(s.nextLine());
s.close();
(setf n (read-from-string (remove-if-not #'digit-char-p (read-line))))
n = io.read("n")
scanf("%d",&n);
fscanf(STDIN, "%d\n", $n);
read(n);
my $n = <> + 0;  # read a line from STDIN, add 0 to convert to int
n = int(input("Input Prompting String: "))
n = $stdin.gets.to_i
use std::io::BufRead;
let n: i32 = std::io::stdin()
    .lock()
    .lines()
    .next()
    .expect("stdin should be available")
    .expect("couldn't read from stdin")
    .trim()
    .parse()
    .expect("input was not an integer");
use std::io;
let mut input = String::new();
io::stdin().read_line(&mut input).unwrap();
let n: i32 = input.trim().parse().unwrap();
fn get_input() -> String {
    let mut buffer = String::new();
    std::io::stdin().read_line(&mut buffer).expect("Failed");
    buffer
}

let n = get_input().trim().parse::<i64>().unwrap();
#[macro_use] extern crate text_io;
let n: i32 = read!();
import scala.io.StdIn
import scala.util.Try
val n = Try(StdIn.readInt())

New implementation...
< >
programming-idioms.org