Sponsored Content
Homework and Emergencies Homework & Coursework Questions Creating a calculator with condition Post 303007716 by mugiboya on Monday 20th of November 2017 04:05:59 PM
Old 11-20-2017
Creating a calculator with condition

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted!

1. The problem statement, all variables and given/known data:

Create a calculator application in your home folder called itncacl that will perform the following applications:

Add two numbers
Subtract two numbers
Multiply two numbers
Divide two numbers
You cannot divide by zero, so create a condition that checks for and prevents a that condition.

2. Relevant commands, code, scripts, algorithms:



3. The attempts at a solution (include all code and scripts):
Code:
#!/bin/bash

clear


echo "Enter the first number:"
read n1

echo "Choose an operation:"
echo "1. add"
echo "2. subtract"
echo "3. multiply"
echo "4. divide"
read opr

echo "Enter the second number:"
read n2

if [ $opr = "1" ]
   then
      echo $((n1+n2))
elif [ $opr = "2" ]
   then
      echo $((n1-n2))
elif [ $opr = "3" ]
   then
      echo $((n1*n2))
elif [ $opr = "4" ]
   then
       echo $((n1/n2))
fi
exit 0

My only question is how do I add to my code so that when I try to divide by zero it will show Cannot divide by zero or invalid? Thanks!

4. Complete Name of School (University), City (State), Country, Name of Professor, and Course Number (Link to Course):
Northern Virginia Comm College, VA, USA, Saleh, ITN171

Note: Without school/professor/course information, you will be banned if you post here! You must complete the entire template (not just parts of it).

Last edited by Scott; 11-20-2017 at 05:07 PM.. Reason: Please use code tags
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

calculator program..

Hey can anyone tell me the korn script code to implement an interactive integer calculator using the shell's built in arithemetic expression evaluation (2 Replies)
Discussion started by: sahithi_khushi
2 Replies

2. Shell Programming and Scripting

Calculator

I am pretty new to the Unix word, and have created a working calculator script. I have one problem. It doesn't use any decimals, it rounds off to the nearest whole number. 1 #!/bin/ksh 2 while true; do 3 echo -n "Enter the first integer: "; read IN1 4 test... (2 Replies)
Discussion started by: ironhead3fan
2 Replies

3. UNIX for Dummies Questions & Answers

calculator

hi, im new to the unix system and scripting and was wondering if anyone could help me with this problem iv been havin... i want the system to: 1. ask me for a number 2. ask me for a command to use on that number (* + - /) 3. ask me for another number 4. then ask me for another command, if the... (2 Replies)
Discussion started by: jdougy
2 Replies

4. Shell Programming and Scripting

redirecting output and creating condition for while loop.

I have 2 questions. 1) Is there a means of directing output to a file (">") while making it still output to the console? I have a script that calls another lengthy script. 2) I can direct the output of the lengthy script and grep it for the words "good" or "bad" to know if I need the run... (2 Replies)
Discussion started by: mrwatkin
2 Replies

5. Shell Programming and Scripting

Help with calculator code

Hi Guys, I found this code in net.. it is working fine.. But can anybody explain me the sed statement used in the code.. echo "Enter the expression:\c" read express eval echo "$express"|sed 's/^/'$precision' \ /'|bc -l|\ sed -n '1,${ /syntax/!{ } ... (2 Replies)
Discussion started by: mac4rfree
2 Replies

6. Shell Programming and Scripting

redirect stdout echo command in condition A run in condition B

hi, I have some problems in my simple script about the redirect echo stdout command inside a condition. Why is the echo command inside the elif still execute in the else command Here are my simple script After check on the two diff output the echo stdout redirect is present in two diff... (3 Replies)
Discussion started by: jao_madn
3 Replies

7. Homework & Coursework Questions

Simple Calculator

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known/data: Script a simple calculator. In the command line enter the script file /home/etc/mycalc or /home/etc/mycalc 1 +... (6 Replies)
Discussion started by: herb bertz
6 Replies

8. Shell Programming and Scripting

If condition return 0 even when it fails to satisfy te condition

HI My doubt may be basic one but I need to get it clarified.. When i use "if" condition that checks for many AND, OR logical conditions like if ]; then return 0 fi Even the if condition fails it returns as zero.. Any clue.. But if i add else condition like if ]; ... (2 Replies)
Discussion started by: Priya Amaresh
2 Replies

9. Shell Programming and Scripting

Creating a condition on a bash script

I wrote a code to find codons in a DNA string. The only problem I have is how do I make the code only work for a file with DNA. This means the file only has the characters a,c,g,t and no white space characters. (3 Replies)
Discussion started by: germany1517
3 Replies

10. UNIX for Beginners Questions & Answers

Creating a calculator with condition

So have I got this : #!/bin/bash clear echo "Enter the first number:" read n1 echo "Choose an operation:" echo "1. add" echo "2. subtract" echo "3. multiply" echo "4. divide" read opr echo "Enter the second number:" read n2 (1 Reply)
Discussion started by: mugiboya
1 Replies
DEBCONF-MERGETEMPLATE(1)					      Debconf						  DEBCONF-MERGETEMPLATE(1)

NAME
debconf-mergetemplate - merge together multiple debconf template files SYNOPSIS
debconf-mergetemplate [options] [templates.ll ...] templates DESCRIPTION
Note: This utility is deprecated. You should switch to using po-debconf's po2debconf program. This program is useful if you have multiple debconf templates files which you want to merge together into one big file. All the specified files will be read in, merged, and output to standard output. This can be especially useful if you are dealing with translated template files. In this case, you might have your main template file, plus several other files provided by the translators. These files will have translated fields in them, and maybe the translators left in the english versions of the fields they translated, for their reference. So, you want to merge together all the translated templates files with your main templates file. Any fields that are unique to the translated files need to be added in to the correct templates, but any fields they have in common should be superseded by the fields in the main file (which might be more up-to-date). This program handles that case properly, just list each of the translated templates files, and then your main templates file last. OPTIONS
--outdated Merge in even outdated translations. The default is to drop them with a warning message. --drop-old-templates If a translation has an entire template that is not in the master file (and thus is probably an old template), drop that entire template. SEE ALSO
debconf-getlang(1) AUTHOR
Joey Hess <joeyh@debian.org> 2012-09-10 DEBCONF-MERGETEMPLATE(1)
All times are GMT -4. The time now is 08:29 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy