command to perform all the arithmetic operations using basic calculator command in unix
Answers
Answer: Arithmetic operations are the most common in any kind of programming language. Unix or linux operating system provides the bc command and expr command for doing arithmetic calculations. You can use these commands in bash or shell script also for evaluating arithmetic expressions.
Here we will see only about the bc command. The bc command evaluates expressions similar to the c programming language. The bc command supports the following features.
Arithmetic operators
Increment and decrement operators
Assignment operators
Comparision or Relational Operators
Logical or Boolean operators
Math Functions
Conditional statements
Iterative statements
Functions
Explanation: 1. Arithmetic Operators
Examples:
Input : $ echo "12+5" | bc
Output : 17
Input : $ echo "10^2" | bc
Output : 100
How to store the result of complete operation in variable?
Example:
Input:
$ x=`echo "12+5" | bc`
$ echo $x
Output:17
Explanation: Stores the result of first line of input in variable x and then display variable x as $x.
2. Assignment Operators
The list of assignments operators supported are:
var = value : Assign the vale to the variable
var += value : similar to var = var + value
var -= value : similar to var = var – value
var *= value : similar to var = var * value
var /= value : similar to var = var / value
var ^= value : similar to var = var ^ value
var %= value : similar to var = var % value
Examples:
Input: $ echo "var=10;var" | bc
Output: 10
Explanation: Assign 10 to the variable and print the value on the terminal.
Input: $ echo "var=10;var^=2;var" | bc
Output: 100
Explanation: Squares the value of the variable and print the value on the terminal.
How to store the result of complete operation in variable?
Example:
Input:
$ x=`echo "var=500;var%=7;var" | bc`
$ echo $x
Output:3
Explanation: Stores the result of 500 modulo 7 i.e. remainder of 500/7 in variable x and then display variable x as $x.
The Arithmetic operations are most common in any kind of the programming language. Unix or linux operating system it provides the bc command and expr command for doing the arithmetic calculations. we can use these commands in bash or in the shell script for evaluating arithmetic expressions.
Arithmetic operators :
Increment and decrement operators :
Assignment operators :
Comparision or the Relational Operators
Logical or Boolean operators
Math Functions
Conditional statements
Iterative statements
Functions :
Explanation: 1. Arithmetic Operators
Examples:
Input : $ echo "12+5" | bc
Output : 17
Input : $ echo "10^2" | bc
Output : 100
How to store the result of complete operation in variable?
Example:
Input:
$ x=`echo "12+5" | bc`
$ echo $x
Output:17
Explanation: Stores the result of first line of input in variable x and then display variable x as $x.
2. Assignment Operators
List of assignments operators supported :
var = value : Assign the vale to the variable
var += value : similar to var = var + value
var -= value : similar to var = var – value
var *= value : similar to var = var * value
var /= value : similar to var = var / value
var ^= value : similar to var = var ^ value
var %= value : similar to var = var % value
Examples:
Input: $ echo "var=10;var" | bc
Output: 10
Explanation: Assign 10 to variable and print value on terminal.
Input: $ echo "var=10;var^=2;var" | bc
Output: 100
Explanation: Square's the value of variable & print the value on it's terminal.
Example:
Input:
$ x=`echo "var=500;var%=7;var" | bc`
$ echo $x
Output:3
Explanation:
It stores result of 500 modulo 7 that is remainder of 500/7 in the variable x & then display variable x as $x.
#SPJ3