Input and Output (I/O) are fundamental concepts in programming.
- Input allows a program to receive data from the user, files, or other sources, while output allows it to display results.
- I/O is essential for creating interactive programs and performing tasks with dynamic data.
Table of Content
Input Output in C
stdio.h is the library in C that provides functions for input and output, like printf() and scanf().
- printf() is used to display messages or values on the screen. You can print text, numbers, and variables.
- scanf() is used to read input from the user and requires a format specifier to know the type of data.
- \n is used to move to a new line so that the next output appears below the previous one.
- Format specifiers tell C what type of data to read or print, for example: %d for integers, %f for floats, %c for characters, and %s for strings.
#include <stdio.h>
int main() {
int age;
float height;
char grade;
char name[20];
// Asking for input
printf("Enter your age, height, grade, and name: ");
// Reading input from the user
scanf("%d %f %c %s", &age, &height, &grade, name);
// Displaying the input using printf
printf("Your age: %d\n", age);
printf("Your height: %.2f\n", height); // .2f for 2 decimal places
printf("Your grade: %c\n", grade);
printf("Your name: %s\n", name);
return 0;
}
Input:
20 5.9 A Alice
Output:
Your age: 20
Your height: 5.90
Your grade: A
Your name: Alice
Input Output in CPP
#include <iostream> is the library in Cpp that provides input/output functions like cout and cin.
- cout is used to display messages or values on the screen. You can print text, numbers, and variables.
- cin is used to read input from the user. The variable type determines the kind of input it accepts.
- \n or endl is used to move to a new line in output.
#include <iostream>
using namespace std;
int main() {
int age;
float height;
char grade;
string name;
cout << "Enter your age, height, grade, and name: ";
cin >> age >> height >> grade >> name;
cout << "Your age: " << age << "\n";
cout << "Your height: " << height << "\n";
cout << "Your grade: " << grade << "\n";
cout << "Your name: " << name << "\n";
return 0;
}
Input:
20 5.9 A Alice
Output:
Your age: 20
Your height: 5.9
Your grade: A
Your name: Alice
Input Output in Java
Scanner (from java.util.Scanner) is used for input, and System.out is used for output.
- System.out.println() displays messages or values on the screen and moves to a new line.
- System.out.print() displays messages without moving to a new line.
- Scanner.nextInt(), nextFloat(), next(), nextLine() are used to read different types of input from the user.
import java.util.Scanner;
public class GFG {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter your age, height, grade, and name: ");
int age = sc.nextInt();
float height = sc.nextFloat();
char grade = sc.next().charAt(0);
String name = sc.next();
System.out.println("Your age: " + age);
System.out.println("Your height: " + height);
System.out.println("Your grade: " + grade);
System.out.println("Your name: " + name);
}
}
Input:
20 5.9 A Alice
Output:
Your age: 20
Your height: 5.9
Your grade: A
Your name: Alice
Input Output in Python
Python does not require a separate library for input/output; it uses built-in functions.
- print() is used to display messages or values on the screen. You can print text, numbers, or variables.
- input() is used to read input from the user as a string; type conversion (int(), float()) is used for numbers.
- \n is used inside print() to move to a new line; print() automatically moves to a new line by default
age, height, grade, name = input("Enter your age, height, grade, and name: ").split()
age = int(age)
height = float(height)
grade = grade
name = name
print("Your age:", age)
print("Your height:", height)
print("Your grade:", grade)
print("Your name:", name)
Input:
20 5.9 A Alice
Output:
Your age: 20
Your height: 5.9
Your grade: A
Your name: Alice
Input Output in C#
System namespace provides input/output functions, and Console is used for I/O.
- Console.WriteLine() displays messages or values on the screen and moves to a new line.
- Console.Write() displays messages without moving to a new line.
- Console.ReadLine() reads input from the user as a string; type conversion is needed for other types like int.Parse() or float.Parse().
using System;
class GFG {
static void Main() {
Console.Write("Enter your age, height, grade, and name: ");
string[] inputs = Console.ReadLine().Split();
int age = int.Parse(inputs[0]);
float height = float.Parse(inputs[1]);
char grade = char.Parse(inputs[2]);
string name = inputs[3];
Console.WriteLine("Your age: " + age);
Console.WriteLine("Your height: " + height);
Console.WriteLine("Your grade: " + grade);
Console.WriteLine("Your name: " + name);
}
}
Input:
20 5.9 A Alice
Output:
Your age: 20
Your height: 5.9
Your grade: A
Your name: Alice
Input Output in JavaScript
In Node.js, input/output is handled using readline module for input and console.log() for output.
- console.log() displays messages or values on the screen and moves to a new line.
- console.print() is not standard; output is mostly done via console.log().
- Input is read using readline.question() in Node.js or prompt() in browsers.
const readline = require('readline').createInterface({
input: process.stdin,
output: process.stdout
});
readline.question('Enter your age, height, grade, and name (space separated): ', input => {
const [age, height, grade, name] = input.split(' ');
console.log(`Your age: ${age}`);
console.log(`Your height: ${height}`);
console.log(`Your grade: ${grade}`);
console.log(`Your name: ${name}`);
readline.close();
});
Input:
20 5.9 A Alice
Output:
Your age: 20
Your height: 5.9
Your grade: A
Your name: Alice