ADVERTISEMENT
ADVERTISEMENT

Linux shell script program to multiply two numbers

Here, we are going to learn how to multiply two numbers in Linux shell script?
Submitted by Nidhi, on February 14, 2021

Problem Solution:

Here, we will create a shell script program to read values for variables from the user and then perform multiplication operation and print the result on the console screen.

Program/Source Code:

The source code to create a Linux shell script program to multiply two numbers is given below. The given program is compiled and executed successfully on Ubuntu 20.04.

#!/bin/bash

# Program name: "multiply.sh"
# Linux shell script program to multiply two numbers.

echo "Enter num1: "
read num1
echo "Enter num2: "
read num2

multiply=`expr $num1 \* $num2`
echo "Multiplication is: $multiply"

Now, we will save the shell script program with the "multiply.sh" name.

Output:

$ sh multiply.sh 
Enter num1: 
10
Enter num2: 
5
Multiplication is: 50

Explanation:

In the above program, we read values of two variables from the user and then multiply both numbers and store the result into the result variable.  After that, we printed the result on the console screen.

multiply=`expr $num1 \* $num2`

In the above statement, we have to use the "\*" operator to perform multiplication operations.

Linux shell script programs »

ADVERTISEMENT



ADVERTISEMENT



Comments and Discussions


ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.