How to Write Input() in 20 Different Languages

Photo by Alex Chumak on Unsplash

How to Write Input() in 20 Different Languages

While working on a project today using C, I realized I had forgotten the input syntax.

Thanks to ChatGPT, which came in handy, I got the syntax, and that made me smile as the more we use other programming languages, the more we mix and forget some of the core concepts of others.

As a Python Dev, I have always loved Python's simple yet elegant syntax, and I believe many devs would feel the same.

I decided to compile 20 different syntaxes for wringing input; if your preferred language is not on the list, you can join the fun and add yours in the comment.

  1. Python
input("Enter a value: ")
  1. Java
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter a value: ");
        String input = scanner.nextLine();
    }
}
  1. C++
#include <iostream>
using namespace std;

int main() {
    cout << "Enter a value: ";
    string input;
    cin >> input;
    return 0;
}
  1. JavaScript
const input = prompt("Enter a value:");
  1. Ruby
puts "Enter a value:"
input = gets.chomp
  1. C#
using System;

class Program {
    static void Main(string[] args) {
        Console.WriteLine("Enter a value:");
        string input = Console.ReadLine();
    }
}
  1. Swift:
import Foundation

print("Enter a value:")
if let input = readLine() {
    // Use the input here
}
  1. Go:
package main

import (
    "bufio"
    "fmt"
    "os"
)

func main() {
    fmt.Print("Enter a value: ")
    scanner := bufio.NewScanner(os.Stdin)
    scanner.Scan()
    input := scanner.Text()
}
  1. PHP:
<?php
echo "Enter a value: ";
$input = trim(fgets(STDIN));
?>
  1. Rust:
use std::io;

fn main() {
    println!("Enter a value:");
    let mut input = String::new();
    io::stdin().read_line(&mut input).expect("Failed to read line");
}
  1. MATLAB:
prompt = 'Enter a value: ';
input = input(prompt, 's');
  1. R
input <- readline("Enter a value: ")
  1. Kotlin
import java.util.*

fun main() {
    val scanner = Scanner(System.`in`)
    print("Enter a value: ")
    val input = scanner.nextLine()
}
  1. Perl
print "Enter a value: ";
my $input = <>;
chomp $input;
  1. Lua
io.write("Enter a value: ")
local input = io.read()
  1. Bash
echo "Enter a value: "
read input
  1. PowerShell
$input = Read-Host "Enter a value"
  1. Dart:
import 'dart:io';

void main() {
  stdout.write('Enter a value: ');
  String input = stdin.readLineSync();
}
  1. TypeScript:
const input = prompt("Enter a value:");
  1. Lua (Corona SDK):
local inputField = native.newTextField( display.contentCenterX, display.contentCenterY, 200, 30 )

inputField.placeholder = "Enter a value"
  1. C
include <stdio.h>

int main()
{
  int age;
  printf("How old are you: ");
  scanf("%d", & age);
  printf("You are %d years old", age);
  return 0;
}

If you have corrections, please include them and let me know your preferred language. I want the list to get to 50, help me reach my goal.

Thanks.

If you find this post exciting, find more exciting posts like this on Learnhub Blog; we write everything tech from Cloud computing to Frontend Dev, Cybersecurity, AI and Blockchain.