Thursday, 29 June 2017

SIMPLE COMMAND LINE INTERFACE ADDITION CALCULATOR IN 10 DIFFERENT PROGRAMMING LANGUAGES

Below is the code in 10 different programming languages (Java, C++, Python, Ruby, C#, Lua, Go, C, JavaScript and PHP) of an addition calculator that runs from Command Line Interface (CLI). In all the languages except JavaScript, the user will provide the operands.

JAVA

import java.util.Scanner;

public class Addition{
 public static void main(String args[]){
  //declare variables
  int firstNumber, secondNumber, sum;
 
  //declare Scanner object for getting input values
  Scanner input = new Scanner(System.in);
 
  //get the first number
  System.out.print("Enter First Number: ");
  firstNumber = input.nextInt();
 
  //get the second number
  System.out.print("Enter Second Number: ");
  secondNumber = input.nextInt();
 
  //compute the result
  sum = firstNumber + secondNumber;
 
  //display the result
  System.out.println("the sum of " + firstNumber + " and "
       + secondNumber + " is " + sum);
 }

}

C++

#include <iostream>

using namespace std;

int main(){
 //declare variables
 int firstNumber, secondNumber, sum;

 //get the first nyumber
 cout << "Enter First Number: ";
 cin >> firstNumber;

 //get the second number
 cout << "Enter Second Number: ";
 cin >> secondNumber;

 //compute the result
 sum = firstNumber + secondNumber;

 //display the result
 cout << "the sum of " << firstNumber << " and " << secondNumber << " is " << sum << endl;

 return 0;

}

PYTHON

#get the first nyumber
firstNumber = input("Enter First Number: ")

#get the second nyumber
secondNumber = input("Enter Second Number: ");

#wrap the numbers to integer and compute the result
total = int(firstNumber) + int(secondNumber)

#display the result
print("the sum of ", firstNumber, " and ", secondNumber, " is ", total);

RUBY

print "Enter First Number: "
firstNumber = gets.chomp #get the first number and remove the newline character

print "Enter Second Number: "
secondNumber = gets.chomp #get the second number and remove the newline character

#compute the result after converting the user inputed numbers to integer
sum = firstNumber.to_i + secondNumber.to_i

#display the result
print "the sum of " + firstNumber.to_s + " and " + secondNumber.to_s + " is " + sum.to_s + "\n"

C#

using System;

class Addition{
 static void Main(){
  //declare string to hold user inputs
  string input;
  //declare integer variavles to hold values
  int firstNumber, secondNumber, sum;
 
  //get the first number
  Console.Write("Enter First Number: ");
  input = Console.ReadLine(); //get user input
  firstNumber = int.Parse(input); //convert user input to integer
 
  //get the second number
  Console.Write("Enter Second Number: ");
  input = Console.ReadLine(); //get user inout
  secondNumber = int.Parse(input); // convert user input to integer
 
  //compute the result
  sum = firstNumber + secondNumber;
 
  //display the result
  Console.WriteLine("the sum of " + firstNumber + " and " + secondNumber
       + " is " + sum);
 }
}

LUA

--get first number
io.write("Enter first number: ")
firstNumber = io.read("*n")

--get second number 
io.write("Enter second Number: ")
secondNumber = io.read("*n")

--calculate the sum
sum = firstNumber + secondNumber
io.write("The sum of ", firstNumber, " and ", secondNumber, " is ", sum, "\n")

GO

package main

import "fmt"

func main(){
 var firstNumber int
 var secondNumber int
 var sum int

 fmt.Print("Enter First Number: ")
 fmt.Scanf("%d", &firstNumber)

 fmt.Print("Enter Second Number: ")
 fmt.Scanf("%d", &secondNumber)

 sum = firstNumber + secondNumber

 fmt.Println("the sum of ", firstNumber, " and ", secondNumber, " is ", sum)
}

C

#include <stdio.h>

int main(){
 //declare variables
 int firstNumber, secondNumber, sum;

 //get the first number
 printf("Enter First Number: ");
 scanf("%d", &firstNumber);

 //get the second number
 printf("Enter Second Number: ");
 scanf("%d", &secondNumber);

 //compute the result
 sum = firstNumber + secondNumber;

 //display the result
 printf("the sum of %d and %d is %d", firstNumber, secondNumber, sum);

 return 0;
}

JAVASCRIPT

var firstNumber, secondNumber, sum;

firstNumber = 10;
secondNumber = 22;

sum = firstNumber + secondNumber;

console.log("the sum of " + firstNumber + " and " + secondNumber + " is " + sum);

PHP

<?php
$fp = fopen("php://stdin","r");

//declare variables
$firstNumber = $secondNumber = $sum = 0;

//get the first number
echo "Enter First Number: ";
$firstNumber = rtrim(fgets($fp));

//get the second number 
echo "Enter Second Number: ";
$secondNumber = rtrim(fgets($fp));

//compute the result
$sum = $firstNumber + $secondNumber;

//display the result 
print("the sum of ".$firstNumber." and ".$secondNumber." is ".$sum."\n");


?>