Ex 3 Dhanu Ios
Ex 3 Dhanu Ios
1.
Aim:
Algorithm:
SI = P×R×T/ 100
CI=P×(1+100/R)t P
Step 6: Display the results for Simple Interest and Compound Interest.
Program:
#!/bin/bash
Output:
Result:
Thus shell program is written for computing simple interest and compound
interest and the expected result is obtained.
2.
Aim:
Algorithm:
Program:
#!/bin/bash
read -p "Enter first number (num1): " num1
read -p "Enter second number (num2): " num2
echo "Before swapping: num1 = $num1, num2 = $num2"
temp=$num1
num1=$num2
num2=$temp
echo "After swapping: num1 = $num1, num2 = $num2"
Output:
Result:
The program swaps their values and displays the result, where the first number becomes the second
and the second number becomes the first and the expected output is displayed.
3.
Aim:
Algorithm:
Program:
#!/bin/bash
read -p "Enter the radius of the circle: " radius
pi=3.14159
area=$(echo "$pi * $radius * $radius" | bc)
circumference=$(echo "2 * $pi * $radius" | bc)
echo "Area of the circle: $area"
echo "Circumference of the circle: $circumference"
Output:
Result:
The program successfully calculates the area and circumference of a circle based on the given radius
using standard mathematical formulas and the expected output is obtained.
4. To find the roots of an quadratic equation.
Aim:
Algorithm:
Step 1: Start the program.
Step 2: Ask the user to enter values for a, b, and c.
Step 3: Calculate the discriminant using the formula:
D=b24ac
Step 4: Based on the discriminant value:
● If D > 0: Calculate two distinct real roots:
Root1=2ab+DandRoot2=2abD
If D = 0: Calculate one real root:
If D < 0: Display complex roots:
RealPart=2abandImaginaryPart=2aD
Step 5: Display the calculated roots.
Step 6: End the program.
Program:
#!/bin/bash
read -p "Enter coefficient a: " a
read -p "Enter coefficient b: " b
read -p "Enter coefficient c: " c
D=$(echo "$b * $b - 4 * $a * $c" | bc)
if (( $(echo "$D > 0" | bc -l) )); then
root1=$(echo "scale=2; (-$b + sqrt($D)) / (2 * $a)" | bc -l)
root2=$(echo "scale=2; (-$b - sqrt($D)) / (2 * $a)" | bc -l)
echo "Roots are real and distinct."
echo "Root 1 = $root1"
echo "Root 2 = $root2"
elif (( $(echo "$D == 0" | bc -l) )); then
root=$(echo "scale=2; -$b / (2 * $a)" | bc -l)
echo "Roots are real and equal."
echo "Root = $root"
else
realPart=$(echo "scale=2; -$b / (2 * $a)" | bc -l)
imagPart=$(echo "scale=2; sqrt(-$D) / (2 * $a)" | bc -l)
echo "Roots are complex and imaginary."
echo "Root 1 = $realPart + ${imagPart}i"
echo "Root 2 = $realPart - ${imagPart}i"
fi
Output:
Result:
Thus shell program is written for finding roots of an quadratic equation and the expected result is
obtained.
5. Given number positive or negative.
Aim:
Algorithm:
Program:
#!/bin/bash
read -p "Enter a number: " num
if [ $num -gt 0 ]; then
echo "The number is positive."
elif [ $num -lt 0 ]; then
echo "The number is negative."
else
echo "The number is zero."
fi
Output:
Result:
The program takes a number as input and checks whether it is positive, negative, or zero using
conditional statements. It then prints the appropriate message based on the input value and the expected
output is displayed.
6. Greatest among three numbers from the given inputs.
Aim:
Algorithm:
Program:
#!/bin/bash
read -p "Enter the first number: " num1
read -p "Enter the second number: " num2
read -p "Enter the third number: " num3
if [ $num1 -gt $num2 ] && [ $num1 -gt $num3 ]; then
echo "$num1 is the greatest number."
elif [ $num2 -gt $num1 ] && [ $num2 -gt $num3 ]; then
echo "$num2 is the greatest number."
else
echo "$num3 is the greatest number."
fi
Output:
Result:
The program successfully determines the greatest among three numbers using conditional statements.
It compares each number step by step to find the largest one and the output is displayed.
7. Sum of numbers between 50 and 100 that are divisible by 3 but not by 5
Aim:
Algorithm:
#!/bin/bash
sum=0
for (( num=50; num<=100; num++ ))
do
# Step 4: Check conditions (divisible by 3 but not by 5)
if [ $((num % 3)) -eq 0 ] && [ $((num % 5)) -ne 0 ]; then
sum=$((sum + num))
fi
done
echo "The sum of numbers between 50 and 100 that are divisible by 3 but not by 5 is: $sum"
Output:
Result:
The program efficiently calculates the sum of numbers between 50 and 100 that are divisible by 3
but not by 5 using a loop and conditional checks. It iterates through the range and accumulates the valid
numbers into sum.
8. To check and display 10 leap years
Aim:
Algorithm:
Step 1: Start the program.
Step 2: Declare variables year and count (initialize count to 0).
Step 3: Set year to the current year or any starting year.
Step 4: Use a while loop that runs until count reaches 10.
Step 5: For each year:
●
If the year is divisible by 4 AND (not divisible by 100 OR divisible by 400), then: → Display
the year as a leap year. → Increment the count by 1.
Step 6: Increment year by 1 in each loop iteration.
Step 7: End the program.
Program:
#!/bin/bash
year=$(date +%Y) # Starting from the current year
count=0 # Counter to track 10 leap years
while [ $count -lt 10 ]
do
if [ $((year % 4)) -eq 0 ] && ([ $((year % 100)) -ne 0 ] || [ $((year % 400)) -eq 0 ]); then
echo "$year is a leap year"
count=$((count + 1)) # Increment count when a leap year is found
fi
year=$((year + 1)) # Move to the next year
done
Output:
Result:
The shell program is written to correctly identify and print the next 10 leap years using a loop and
leap year conditions.
9.
Aim:
Algorithm:
Program:
#!/bin/bash
read -p "Enter the student's marks: " marks
if [ $marks -ge 90 ]; then
echo "Grade A"
elif [ $marks -ge 80 ]; then
echo "Grade B"
elif [ $marks -ge 70 ]; then
echo "Grade C"
elif [ $marks -ge 60 ]; then
echo "Grade D"
else
echo "Grade F"
fi
Output:
Result:
The program successfully determines and displays the student's grade based on the input marks using
a simple if-elif structure.
10.
Aim:
Algorithm:
Step 1: Start the program.
Step 2: Declare variables num, reverse, and temp.
Step 3: Accept the number as input and assign it to both num and temp.
Step 4: Initialize reverse to 0.
Step 5: Use a while loop to reverse the digits of the number:
● Extract the last digit using num % 10.
● Append this digit to reverse by multiplying reverse by 10 and adding the digit.
● Remove the last digit from num using integer division (num = num / 10).
Step 6: After the loop ends, compare temp (original number) with reverse:
● If they are equal Display "The number is a palindrome".
● Else → Display "The number is not a palindrome".
Step 7: End the program.
Program:
#!/bin/bash
read -p "Enter a number: " num
reverse=0
temp=$num # Store original number for comparison
while [ $num -gt 0 ]
do
digit=$((num % 10)) # Extract last digit
reverse=$((reverse * 10 + digit)) # Append digit to reverse
num=$((num / 10)) # Remove last digit
done
if [ $temp -eq $reverse ]; then
echo "$temp is a palindrome."
else
echo "$temp is not a palindrome."
Output:
Result:
The program efficiently checks whether a given number is a palindrome by reversing it and
comparing it with the original number. It uses a loop and arithmetic operations for the reversal process.
11.
Aim:
Algorithm:
Program:
#!/bin/bash
read -p "Enter a number: " num
factorial=1
for (( i=1; i<=num; i++ ))
do
factorial=$((factorial * i))
done
echo "The factorial of $num is: $factorial"
Output:
Result:
The program calculates the factorial of a given number using a loop and multiplication. It correctly
handles positive numbers, prints 1 for 0, and displays an error message for negative numbers.
12.
Aim:
Algorithm:
Program:
#!/bin/bash
read -p "Enter a number: " num
sum=0
while [ $num -gt 0 ]
do
digit=$((num % 10)) # Extract the last digit
sum=$((sum + digit)) # Add the digit to sum
num=$((num / 10)) # Remove the last digit
done
echo "The sum of the digits is: $sum"
Output:
Result:
The program calculates the sum of digits of a given number by extracting each digit using the
modulus operator and adding it to a sum variable. The process continues until all digits are processed. This
method ensures an efficient and accurate solution using C programming.
13.
Aim:
Algorithm:
Step 1: Start the program.
Step 2: Declare variables num and flag.
Step 3: Accept the number as input.
Step 4: Initialize flag to 1 (assuming the number is prime).
Step 5: If num is less than or equal to 1, set flag to 0 (not prime).
Step 6: Use a for loop from 2 to num/2 to check divisibility:
● If num is divisible by any value in this range, set flag to 0 (not prime) and exit the loop.
Step 7:
● If flag is 1 Display "The number is prime".
● Else → Display "The number is not prime".
Step 8: End the program.
Program:
#!/bin/bash
read -p "Enter a number: " num
flag=1
if [ $num -le 1 ]; then
flag=0
else
for (( i=2; i<=num/2; i++ ))
do
if [ $((num % i)) -eq 0 ]; then
flag=0 # Number is divisible, so it's not prime
break
fi
done
Output:
Result:
This approach ensures an accurate and efficient prime-checking mechanism in C programming and
the expected output is obtained.
14.
Aim:
Algorithm:
Program:
#!/bin/bash
read -p "Enter a number: " num
Result:
The shell script successfully determines whether a given number is odd or even by computing the
remainder when divided by 2. If the remainder is 0, it is even,otherwise, it is odd.
15.
Aim:
Algorithm:
Step 1: Start the program.
Step 2: Declare variables num1, num2, num3, num4, sum, and average.
Step 3: Accept four integers as input.
Step 4: Calculate the sum using the formula:
sum=num1+num2+num3+num4\text{sum} = \text{num1} + \text{num2} + \text{num3} + \
text{num4}sum=num1+num2+num3+num4
Step 5: Calculate the average using the formula:
average=sum4\text{average} = \frac{\text{sum}}{4}average=4sum
Step 6: Display the calculated sum and average.
Step 7: End the program.
Program:
#!/bin/bash
read -p "Enter the first number: " num1
read -p "Enter the second number: " num2
read -p "Enter the third number: " num3
read -p "Enter the fourth number: " num4
sum=$((num1 + num2 + num3 + num4))
average=$(echo "scale=2; $sum / 4" | bc)
echo "The sum of the four numbers is: $sum"
echo "The average of the four numbers is: $average"
Output:
Result:
This shell script successfully calculates the sum and average of four integers entered by the user. It
demonstrates basic arithmetic operations in shell scripting.
16.
Aim:
Algorithm:
Step 1: Start the program.
Step 2: Display a menu with the following options:
● 1 Display the current date and time.
● 2 Display the list of files in the current directory.
● 3 Display the current working directory.
● 4 Display the logged-in users.
● 5 → Exit the program.
Step 3: Accept the user’s choice.
Step 4: Use a case statement to execute the corresponding command:
● Case 1: Display the current date and time using date.
● Case 2: Display the list of files using ls.
● Case 3: Display the current directory using pwd.
● Case 4: Display the logged-in users using who.
● Case 5: Exit the program.
Step 5: If an invalid choice is entered, display an error message.
Step 6: End the program.
Program:
#!/bin/bash
echo "Choose an option:"
echo "1. Display current date and time"
echo "2. Display list of files in the current directory"
echo "3. Display current working directory"
echo "4. Display logged-in users"
echo "5. Exit"
read -p "Enter your choice (1-5): " choice
case $choice in
1) echo "Current date and time: $(date)";;
2) echo "List of files in the current directory:"
ls
;;
3) echo "Current working directory: $(pwd)"
;;
4) echo "Logged-in users:"
who
;;
5) echo "Exiting the program. Goodbye!"
exit 0
;;
6) echo "Invalid choice! Please select a valid option."
;;
esac
Output:
Result:
This shell script demonstrates the use of the case statement to execute various UNIX commands
based on user input and the expected output is obtained.