Logo

Programming-Idioms

  • Lisp
  • Go
  • JS
  • Pascal
  • D
  • Python

Idiom #120 Read integer from stdin

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

n = int(input())
n = int(input("Input Prompting String: "))
(setf n (read-from-string (remove-if-not #'digit-char-p (read-line))))

(setf n (read))
if you don't care if it's explicitly an integer
import "fmt"
_, err := fmt.Scan(&n)

Warning: if the input has a leading 0, it will be interpreted as octal!
import "fmt"
_, err := fmt.Scanf("%d", &n)
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()
})

This example only works with nodeJS (server-side JS) because browser JS does not have a standard input.
read(n);
import std.stdio;
readf("%d", &n);
#include <stdio.h>
int n[15];
fgets(n, 15, stdin);

int n[15]; Int with 15 bytes to use.

Fgets gets user input, and sets n to what it gets

New implementation...