0% found this document useful (0 votes)
9 views6 pages

Bash Programs for Number Operations

The document contains several programs written in shell script for basic mathematical operations. It includes functionalities to check if a number is even or odd, find the greatest of three numbers, calculate the factorial of a number, generate a Fibonacci series, and perform basic arithmetic operations. Each program provides user prompts and outputs results based on user input.

Uploaded by

Alvin Saji John
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views6 pages

Bash Programs for Number Operations

The document contains several programs written in shell script for basic mathematical operations. It includes functionalities to check if a number is even or odd, find the greatest of three numbers, calculate the factorial of a number, generate a Fibonacci series, and perform basic arithmetic operations. Each program provides user prompts and outputs results based on user input.

Uploaded by

Alvin Saji John
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

‭PROGRAM‬

e‭ cho "enter the number"‬


‭read n‬
‭if [ `expr $n % 2 ` -eq 0 ]‬
‭then‬
‭echo "$n is even number"‬
‭else‬
‭echo‬
‭"$n is odd number"‬
‭fi‬

‭ UTPUT‬
O
‭enter the number‬
‭12‬
‭12 is even number‬

e‭ nter the numbers‬


‭33‬
‭33 is odd number‬
‭PROGRAM‬

e‭ cho "enter the 3 numbers"‬


‭read n1‬
‭read n2‬
‭read n3‬
‭if [ $n1 -gt $n2 ] && [ $n1 -gt $n3 ]‬
‭then‬
‭echo "$n1 is the greatest"‬
‭elif [ $n2 -gt $n3 ]‬
‭then‬
‭echo "$n2 is the greatest"‬
‭else‬
‭echo‬
‭"$n3 is the greatest"‬
‭fi‬

‭ UTPUT‬
O
‭enter the 3 numbers‬
‭10‬
‭5‬
‭40‬
‭40 is the greatest‬
‭ ROGRAM‬
P
‭echo "Enter the no to check: "‬
‭read n‬
‭fact=1‬
‭if [ $n -eq 0 ]‬
‭then‬
‭echo "factorial is 1"‬
‭else‬
‭while [ $n -gt 0 ]‬
‭do‬
‭fact=`expr $fact \* $n`‬
‭n=`expr $n - 1`‬
‭done‬
‭echo "$fact is factorial"‬
‭fi‬

‭ UTPUT‬
O
‭Enter the no to check:‬
‭5‬
‭120 is factorial‬
‭ ROGRAM‬
P
‭echo "Enter the no of terms: "‬
‭read n‬
‭a=0‬
‭b=1‬
‭count=0‬
‭echo "Fibonacci series: "‬
‭while [ $count -lt $n ]‬
‭do‬
‭echo "$a "‬
‭a=`expr $a + $b`‬
‭b=`expr $a - $b`‬
‭count=` expr $count + 1 `‬
‭done‬

‭ UTPUT‬
O
‭Enter the no of terms:‬
‭5‬
‭Fibonacci series:‬
‭0‬
‭1‬
‭1‬
‭2‬
‭3‬
‭ ROGRAM‬
P
‭echo "Enter first number: "‬
‭read num1‬
‭echo "Enter second number: "‬
‭read num2‬
‭echo "Enter operation (+, -, *, /): "‬
‭read operation‬
‭if [ "$operation" == "+" ];‬
‭then result=$((num1 + num2))‬
‭elif [ "$operation" == "-" ];‬
‭then‬
‭result=$((num1 - num2))‬
‭elif [ "$operation" == "*" ];‬
‭then‬
‭result=$((num1 * num2))‬
‭elif [ "$operation" == "/" ];‬
‭then‬
‭if [ "$num2" -eq 0 ];‬
‭then‬
‭echo "Error: Division by zero!"‬
‭exit 1‬
‭else‬
‭result=$((num1 / num2))‬
‭fi‬
‭else‬
‭echo "Invalid operation!"‬
‭exit 1‬
‭fi‬
‭echo "Result: $result"‬

‭ UTPUT‬
O
‭Enter first number:‬
‭10‬
‭Enter second number:‬
‭20‬
‭ nter operation (+, -, *, /):‬
E
‭+‬
‭Result: 30‬

‭ nter first number:‬


E
‭50‬
‭Enter second number:‬
‭20‬
‭Enter operation (+, -, *, /):‬
‭-Result: 30‬

‭ nter first number:‬


E
‭20‬
‭Enter second number:‬
‭40‬
‭Enter operation (+, -, *, /):‬
‭*‬
‭Result: 800‬

‭ nter first number:‬


E
‭60‬
‭Enter second number:‬
‭20‬
‭Enter operation (+, -, *, /):‬
‭/‬
‭Result: 3‬

You might also like