Explore the syntax and methods used for I/O operations across different programming languages.
#include <stdio.h>
int main() {
int num1, num2, result;
printf("Enter first number: ");
scanf("%d", &num1);
printf("Enter second number: ");
scanf("%d", &num2);
result = num1 * num2;
printf("Multiplication result: %d\n", result);
return 0;
}
Enter first number: 5 Enter second number: 4 Multiplication result: 20
#include <stdio.h>
int main() {
int num1 = 5; // Direct assignment
int num2 = 4; // Direct assignment
int result;
result = num1 * num2;
printf("First number: %d\n", num1);
printf("Second number: %d\n", num2);
printf("Multiplication result: %d\n", result);
return 0;
}
First number: 5 Second number: 4 Multiplication result: 20
# Python program to multiply two numbers with user input
num1 = int(input("Enter first number: "))
num2 = int(input("Enter second number: "))
result = num1 * num2
print("Multiplication result:", result)
Enter first number: 5 Enter second number: 4 Multiplication result: 20
# Python program with direct assignment
num1 = 5 # Direct assignment
num2 = 4 # Direct assignment
result = num1 * num2
print("First number:", num1)
print("Second number:", num2)
print("Multiplication result:", result)
First number: 5 Second number: 4 Multiplication result: 20
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter first number: ");
int num1 = sc.nextInt();
System.out.print("Enter second number: ");
int num2 = sc.nextInt();
int result = num1 * num2;
System.out.println("Multiplication result: " + result);
sc.close();
}
}
Enter first number: 5 Enter second number: 4 Multiplication result: 20
public class MultiplyNumbers {
public static void main(String[] args) {
int num1 = 5; // Direct assignment
int num2 = 4; // Direct assignment
int result = num1 * num2;
System.out.println("First number: " + num1);
System.out.println("Second number: " + num2);
System.out.println("Multiplication result: " + result);
}
}
First number: 5 Second number: 4 Multiplication result: 20
import Foundation
print("Enter first number: ", terminator: "")
if let input1 = readLine(), let num1 = Int(input1) {
print("Enter second number: ", terminator: "")
if let input2 = readLine(), let num2 = Int(input2) {
let result = num1 * num2
print("Multiplication result: \(result)")
}
}
Enter first number: 5 Enter second number: 4 Multiplication result: 20
import Foundation
let num1 = 5 // Direct assignment
let num2 = 4 // Direct assignment
let result = num1 * num2
print("First number: \(num1)")
print("Second number: \(num2)")
print("Multiplication result: \(result)")
First number: 5 Second number: 4 Multiplication result: 20
// JavaScript with HTML input elements
function multiply() {
const num1 = Number(document.getElementById("num1").value);
const num2 = Number(document.getElementById("num2").value);
const result = num1 * num2;
// Output to console
console.log(`Multiplication result: ${result}`);
// Output to alert
alert(`Multiplication result: ${result}`);
// Output to page
document.getElementById("output").innerHTML =
`Multiplication result: ${result}`;
}
Enter first number: 5 Enter second number: 4 Multiplication result: 20
// JavaScript with prompt dialogs
function multiplyUsingPrompt() {
const num1 = Number(prompt("Enter first number:"));
const num2 = Number(prompt("Enter second number:"));
const result = num1 * num2;
document.getElementById("resultPrompt").innerHTML =
`Multiplication result: ${result}`;
}
Enter first number: 5 Enter second number: 4 Multiplication result: 20
// JavaScript with direct assignment
function multiplyAndAssign() {
const num1 = 5; // Direct assignment
const num2 = 4; // Direct assignment
const result = num1 * num2;
// Output to alert
alert(`Multiplication result: ${result}`);
// Output to page
document.getElementById("outputAssign").innerHTML =
`First number: ${num1}` + "<br>" +
`Second number: ${num2}` + "<br>" +
`Multiplication result: ${result}`;
}
First number: 5 Second number: 4 Multiplication result: 20