answersLogoWhite

0

Shell script to add two numbers?

Updated: 8/10/2023
User Avatar

Wiki User

12y ago

Best Answer

echo "enter the value of a"

read a

echo "enter the value of b"

read b

c=`expr $a + $b`

echo "the sum of a and b is $c"

Or, alternatively, one could also...

#!/bin/sh

A=5

B=10

C=`echo "$A + $B"|bc -l`

echo "$A + $B = $C"

The above examples illustrate the use of back ticks to fork a child process in shell, however the first example uses the 'expr' command to perform the arithmetic while the second example uses the 'bc' command to perform the calculation. As is nearly always the case in Unix or Linux, there are multiple methods of arriving at the same correct solution.

While the wonderful and all powerful Perl language is perhaps, technically, not a shell, it is theoretically possible to accomplish this goal in Perl as well. Let me see now, to make this example as educational as possible, I'll use a couple of subscripts of the array @array to store our values and an off-the-shelf printf function to display the results of our calculations...

perl -e '@array = (5, 10); printf("%s + %s = %s\n", $array[1], $array[0], ($array[0]+$array[1]));'

As the purpose of this site is to teach, I briefly entertained and then discarded a notion to shoot for maximum obfuscation. Larry, Randy and Nathan will be proud of me I hope. :)

Naturally, there are numerous other ways to accomplish this goal in Linux.

User Avatar

Wiki User

11y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

11y ago

public class TwoNumbers {

· public static void main(String args[]){

·

· int first_number = Integer.parseInt(args[0]);

· int second_number = Integer.parseInt(args[1]);

· int addition = first_number + second_number;

·

· if(args.length>2){

· }

· System.out.println("The sum is :"+ addition);

· }

· }

This answer is:
User Avatar

User Avatar

Wiki User

12y ago

#!/bin/Bash

echo "Enter the two numbers to be added:"

read n1

read n2

answer=$(($n1+$n2))

echo $answer

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Shell script to add two numbers?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering
Related questions

How do you add two matrices using Linux shell script?

write ashell script to add awo matrix using array.


Shell script for multiplying two numbers?

#!/bin/Bash echo "Enter the two numbers to be Multiplied:" read n1 read n2 answer=`expr $n1 \* $n2` echo $answer


What are the two ways you can execute a shell script when you do not have execute access permission for the file containing the script?

To do this you will need at least read permission; if you don't have that then you will not be able to execute the script at all. One way is to copy the script to your directory and add the execute permission. The second way is to call the correct shell interpreter program directly, as in: ksh /some/file/shell


Write a shell script for addition of two numbers using expr?

a=10; b=20; c=`expr $a + $b`; printf "$c";


How do you write shell script that will add two nos which are supplied?

#!/bin/sh # # add_2_numbers.sh # # Script provided by Alvin Abrahams - 07905 631 695. # # Seeking role as Unix Systems Administrator - Immediately available # # Assuming the two numbers are supplied to the script as parameters... # # The the variable add_sum will contain the result which is then displayed as below. # add_sum=`echo $1+$2|bc` echo ${add_sum} exit # Execution of script below. # ./add_2_numbers 3 4 7


In Shell script. How do I ask for and accept two numbers and then calculate the sum of the supplied numbers and display the sum.?

It depends on the script language you are using. In the Korn shell, you can say: echo -n "Enter the first number: " read first echo -n "Enter the second number: " read second let third=$first+$second echo The answer is $third


Shell script to find LCM of the given two numbers?

Shell program to find LCM and hcf of two no.s tput clear echo "Enter first no" read a echo "Enter 2nd no" read b p= 'expr $a \* $b' while [$b -ne 0] do r= 'expr $a % $b' a=$b b=$r done LCM = 'expr $p / $a' echo "LCM = $LCM" echo "Hcf = $a"


What is all of the Fibonacci numbers in nature?

Its indefinite: Start with 0,1 add last two numbers = 1 add to sequence = 0,1,1 add last two numbers = 2 add to sequence = 0,1,1,2 add last two numbers = 3 add to sequence = 0,1,1,2,3 add last two numbers = 5 add to sequence = 0,1,1,2,3,5 add last two numbers ......... add to sequence ..........


What does add operator do?

Add two numbers.


What number is halfway between 300 and 600?

Add the two numbers, and divide the result by 2.Add the two numbers, and divide the result by 2.Add the two numbers, and divide the result by 2.Add the two numbers, and divide the result by 2.


What numbers must you add to make an even number?

You must add either two odd numbers or two even numbers.


Input two numbers and get the average?

Add the two numbers and divide that by two.