Logo

Programming-Idioms

This language bar is your friend. Select your favorite languages!
Select your favorite languages :
  • Or search :

Idiom #120 Read integer from stdin

Read an integer value from the standard input into variable n

#include <stdio.h>
int n[15];
fgets(n, 15, stdin);
#include <stdio.h>
int n;
scanf("%d", &n);
(def n (Integer/parseInt (read-line)))
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);
n = String.to_integer IO.gets ""
   integer :: n
   read (*,*) n
import "fmt"
_, err := fmt.Scanf("%d", &n)
import "fmt"
_, err := fmt.Scan(&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);
n = in.nextInt();
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();

New implementation...
programming-idioms.org