Input() Syntax in 20 Different Programming Languages
Tags: python, javascript, go, swift
Published: 2023-05-28 18:02:29

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. 

Python
```python
input("Enter a value: ")
```
Java
```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();
    }
}
```
C++
```c++
#include <iostream>
using namespace std;

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

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

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

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

func main() {
    fmt.Print("Enter a value: ")
    scanner := bufio.NewScanner(os.Stdin)
    scanner.Scan()
    input := scanner.Text()
}
```
PHP:
```php
<?php
echo "Enter a value: ";
$input = trim(fgets(STDIN));
?>
```
Rust:
```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");
}
```
MATLAB:
```matlab
prompt = 'Enter a value: ';
input = input(prompt, 's');
```
R
```r
input <- readline("Enter a value: ")
```
Kotlin
```kotlin
import java.util.*

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

void main() {
  stdout.write('Enter a value: ');
  String input = stdin.readLineSync();
}
```
TypeScript:
```typescript
const input = prompt("Enter a value:");

Lua (Corona SDK):

local inputField = native.newTextField( display.contentCenterX, display.contentCenterY, 200, 30 )

inputField.placeholder = "Enter a value"
```
C
```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.