Input and output method
Input and output are essential concepts in programming languages. Input refers to the process of providing data to a program or function, while output refers to the process of displaying or returning data from a program or function.
Most programming languages provide built-in functions or methods for handling input and outpu. Here given code example.
/*
C program for
input and output method
*/
// Include header file
#include <stdio.h>
int main()
{
// Declare variables
int i;
char c;
float f;
double d;
long long int lli;
long double ld;
// scanf method are used to initialize value of
// custom input to its associate variable.
// Here parameter one , indicates formation of input value
// and second is address of associate variable
// Syntax
// scanf ("format specifier of variables",address of variables)
// Some Input example
printf("Enter an integer : ");
// %d for int
scanf("%d", & i);
printf("Enter an character : ");
// %c for char
scanf(" %c", & c);
printf("Enter an float : ");
// %f for float
scanf("%f", & f);
printf("Enter an double : ");
// %lf for double
scanf("%lf", & d);
printf("Enter an long long int and an long double value : \n");
// %lld long long int and %Lf for long double
// Two input in single scanf
scanf("%lld%Lf", & lli, & ld);
// print method are used to display custom user message
// And variables values.
// Example show above input values
printf("\n i : %d", i);
printf("\n c : %c", c);
printf("\n f : %f", f);
printf("\n d : %lf", d);
printf("\n lli : %lld", lli);
printf("\n ld : %Lf", ld);
return 0;
}
Output
Enter an integer : 12
Enter an character : A
Enter an float : 12.05
Enter an double : 1234553.44
Enter an long long int and an long double value :
1234567
987648238.3332
i : 12
c : A
f : 12.050000
d : 1234553.440000
lli : 1234567
ld : 987648238.333200
import java.util.Scanner;
/*
Java program for
input and output method
*/
public class InputOutput
{
public static void main(String[] args)
{
// Create Scanner object
Scanner enter = new Scanner(System.in);
System.out.print("Enter an integer : ");
// nextInt() for int
int intValue = enter.nextInt();
// Character Input
System.out.print("Enter an character : ");
// next().charAt(0) for character
char charValue = enter.next().charAt(0);
// Float Input
System.out.print("Enter an float : ");
// nextFloat() for float
float floatValue = enter.nextFloat();
// Double Input
System.out.print("Enter an double : ");
// nextDouble() for int
double doubleValue = enter.nextDouble();
// Boolean Input
System.out.print("Enter an Boolean : ");
// nextBoolean() for int
boolean boolValue = enter.nextBoolean();
// use to separate above inputs
enter.nextLine();
// String Input
System.out.print("Enter String : ");
// nextLine() for int
String strValue = enter.nextLine();
// Short Input
System.out.print("Enter an short : ");
// nextShort() for int
short shortValue = enter.nextShort();
// Other
// nextByte() for Byte
// Input method
// print method
System.out.print(" Input integer : " + intValue);
// printf method
System.out.printf("\n Input character : %c", charValue);
// println method (include new line)
System.out.println("\n Input float : " + floatValue);
System.out.println(" Input Boolean : " + boolValue);
System.out.print(" Input String : " + strValue);
System.out.printf("\n Input Short : %d", shortValue);
}
}
Output
Enter an integer : 45
Enter an character : J
Enter an float : 45.5
Enter an double : 32345345.4345
Enter an Boolean : true
Enter String : Java Code
Enter an short : 3
Input integer : 45
Input character : J
Input float : 45.5
Input Boolean : true
Input String : Java Code
Input Short : 3
// Include header file
#include <iostream>
#include <string>
using namespace std;
/*
C++ program for
input and output method
*/
int main()
{
int intValue ;
char charValue ;
float floatValue;
double doubleValue;
string strValue;
cout << "Enter an integer : ";
cin >> intValue;
// Character Input
cout << "Enter an character : ";
cin >> charValue;
// Float Input
cout << "Enter an float : ";
cin >> floatValue;
// String inputs
cout << "Enter string : ";
cin.ignore();
getline(cin,strValue);
// Accepting a double Input
cout << "Enter an double : ";
cin >> doubleValue;
// Output
cout << " Input integer : " << intValue <<endl;
cout << " Input character : "<< charValue <<endl;
cout << " Input doubleValue : "<< doubleValue <<endl;
cout << " Input float : " << floatValue << endl;
cout << " Input String : " << strValue << endl;
return 0;
}
Output
Enter an integer : 32
Enter an character : P
Enter an float : 53.4
Enter string : C++ Code
Enter an double : 345666.434
Input integer : 32
Input character : P
Input doubleValue : 345666
Input float : 53.4
Input String : C++ Code
// Include namespace system
using System;
/*
Csharp program for
input and output method
*/
public class InputOutput
{
public static void Main(String[] args)
{
// Console.ReadLine() are use to input in runtime
// Its returns an string value so we need to convert before its use
Console.Write("Enter an integer : ");
// Integer inputs
int intValue = Convert.ToInt32(Console.ReadLine());
// Character Input
Console.Write("Enter an character : ");
// next().charAt(0) for character
char charValue = Convert.ToChar(Console.ReadLine());
// Double Input
Console.Write("Enter an double : ");
double doubleValue = Convert.ToDouble(Console.ReadLine());
// Boolean Input
Console.Write("Enter an Boolean : ");
Boolean boolValue = Convert.ToBoolean(Console.ReadLine());
// String Input
Console.Write("Enter String : ");
String strValue = Console.ReadLine();
// Display method
Console.Write(" Input integer : " + intValue);
Console.WriteLine("\n Input String : " + strValue);
Console.WriteLine($" Input Double : {doubleValue} ");
Console.WriteLine("Input Boolean {0} Input character {1}",boolValue,charValue);
}
}
Output
Enter an integer : 56
Enter an character : S
Enter an double : 345.534
Enter an Boolean : true
Enter String : C# Code
Input integer : 56
Input String : C# Code
Input Double : 345.534
Input Boolean True Input character S
<?php
/*
Php program for
input and output method
*/
function main()
{
// readline() and fscanf() input method
// Int Input
$intValue = (int) readline("Enter an integer : ");
// Float Input
$floatValue = (float) readline("Enter an float : ");
echo("Enter an character : ");
// Character Input
fscanf(STDIN, "%c", $charValue);
// String Input
$strValue = readline("Enter String : ");
echo("\n Input integer : ".$intValue);
echo("\n Input character : ". $charValue);
echo("\n Input float : ".$floatValue);
echo("\n Input String : ".$strValue);
}
main();
Output
Enter an integer : 123
Enter an float : 44.55
Enter an character : P
Enter String : Php code example
Input integer : 123
Input character : P
Input float : 44.55
Input String : Php code example
Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.
New Comment