In this article VI Editor Programs we give the simple vi editor programs with condition based also case and loop based programs with output.
VI Editor Programs:
# Simple program using nano Editor
Step 1: Open terminal and create file with Nano
nano hello.sh
Step 2: Write the shell script
#!/bin/bash
echo “Hello, World!”
Step 3: Save and exit Nano
- Press CTRL + O → Enter (to save).
- Press CTRL + X (to exit Nano).
Step 4: Give execute permission
chmod +x hello.sh
Step 5: Run the script
./hello.sh
Hello, World!
Addition of two numbers.
#!/bin/bash
# Program to add two numbers
echo “Enter first number: “
read a
echo “Enter second number: “
read b
sum=$((a + b))
echo “The sum is: $sum”
- Area of Rectangle.
#!/bin/bash
# Program to calculate area of rectangle
echo “Enter length: “
read l
echo “Enter breadth: “
read b
area=$((l * b))
echo “Area of rectangle = $area”
- Swap two numbers
#!/bin/bash
# Program to swap two numbers
echo “Enter first number (a): “
read a
echo “Enter second number (b): “
read b
echo “Before swapping: a=$a, b=$b”
# Swapping logic
temp=$a
a=$b
b=$temp
echo “After swapping: a=$a, b=$b”
#4 To find ODD/EVEN number
#!/bin/bash
echo “Enter the number: ”
read n
if((n%2==0)); then
echo “$n is a Even number”
else
echo “$n is Odd Number”
fi.
- Find largest number among two numbers
#!/bin/bash
# Program to find largest number among two
echo “Enter first number: “
read a
echo “Enter second number: “
read b
if ((a > b)); then
echo “Largest number is: $a”
elif ((b>a)); then
echo “Largest number is: $b”
else
echo “Both numbers are equal”
fi
- Find largest number among 3 numbers
#!/bin/bash
# Program to find largest number among three numbers
echo “Enter first number: “
read a
echo “Enter second number: “
read b
echo “Enter third number: “
read c
if ((a>b)) && ((a>c)); then
echo “Largest number is: $a”
elif ((b>>c)); then
echo “Largest number is: $b”
else
echo “Largest number is: $c”
fi
-
Display first 10 numbers
#!/bin/bash
# Program to display first 10 numbers
echo “First 10 naturals numbers are:”
for ((i=1; i<=10; i++))
do
echo $i
done
- Sum of digit
#!/bin/bash
# Program to find sum of digits of a number
echo “Enter a number: “
read num
sum=0
dig=0
while((num>0))
do
dig=$((num % 10)) # extract last digit
sum=$((sum + digit)) # add to sum
num=$((num / 10)) # remove last digit
done
echo “Sum of digits of $num = $sum”
- Factorial of given number
#!/bin/bash
# Program to find factorial of a given number
# Program to find factorial of a number
echo “Enter a number: “
read num
factorial=1
temp=$num # original number को save किया
if (( num < 0 )); then
echo “Factorial is not defined for negative numbers.”
else
while (( num > 1 ))
do
factorial=$(( factorial * num ))
num=$(( num – 1 ))
done
echo “Factorial of $temp = $factorial”
Fi
To find prime number
#!/bin/bash
# Program to check whether a number is prime or not
# Program to check if a number is prime
echo “Enter a number: “
read num
if (( num <= 1 )); then
echo “$num is not a prime number.”
else
is_prime=1 # assume prime
for (( i=2; i*i<=num; i++ ))
do
if (( num % i == 0 )); then
is_prime=0
break
fi
done
if (( is_prime == 1 )); then
echo “$num is a prime number.”
else
echo “$num is not a prime number.”
fi
Fi
Leap year
#!/bin/bash
# Program to check leap year
# Program to check if a year is a leap year
echo “Enter a year: “
read year
if (( year % 4 != 0 )); then
echo “$year is not a leap year.”
elif (( year % 100 != 0 )); then
echo “$year is a leap year.”
elif (( year % 400 == 0 )); then
echo “$year is a leap year.”
else
echo “$year is not a leap year.”
Fi
Switch Case statement:
#!/bin/bash
echo “Enter two numbers: “
read a b
echo “Choose an operation:”
echo “1. Addition”
echo “2. Subtraction”
echo “3. Multiplication”
echo “4. Division”
echo “Enter your choice: ”
read choice
case $choice in
1)
result=$((a + b))
echo “Result = $result”
;;
2)
result=$((a – b))
echo “Result = $result”
;;
3)
result=$((a * b))
echo “Result = $result”
;;
4)
if [ $b -ne 0 ]; then
result=$((a / b))
echo “Result = $result”
else
echo “Error: Division by zero!”
fi
;;
*)
echo “Invalid choice!”
;;
esac
POP- Introduction to Programming Using ‘C’
OOP – Object Oriented Programming
DBMS – Database Management System
RDBMS – Relational Database Management System