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.
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);
· }
· }
#!/bin/Bash
echo "Enter the two numbers to be added:"
read n1
read n2
answer=$(($n1+$n2))
echo $answer
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
#include<iostream.h> #include<conio.h> void main() { int a, b, c; clrscr(); cout<<"enter the two numbers"; cin>>a; cin>b; c=a+b; cout<<"Addition of two numbers="<<c; getch(); }
It is used for addition - to add two numbers. Also, to concatenate two Strings (texts) - that is, to make a longer text from two shorter ones.
package AddNumbers; public class AddNumbers { public static void main(String[] args) { int Int; int a = 4; int b=5 ; int sum = a + b; System.out.println("Addition of two numbers!"); System.out.println("Sum: " + sum); } }
#include<stdio.h> main() { int num1; int num2; int a; printf("Enter any two numbers :"); scanf("%d%d",&num1,&num2); for(a=num1;num1<=num2;a++) { if ( a % 2 == 1); { printf("%d",a); } } getch(); }
write ashell script to add awo matrix using array.
#!/bin/Bash echo "Enter the two numbers to be Multiplied:" read n1 read n2 answer=`expr $n1 \* $n2` echo $answer
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
a=10; b=20; c=`expr $a + $b`; printf "$c";
#!/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
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 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"
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 ..........
Add two numbers.
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.
You must add either two odd numbers or two even numbers.
Add the two numbers and divide that by two.