Tuesday, March 5, 2024

Program 6 B.Sc Computer Science with Data analytics Linux and Shell Programming - Lab Bharathiar University - Write a Shell script to determine whether a given number is a prime number or not.

 


B.Sc Computer Science  with Data analytics

Linux and Shell Programming - Lab  

Bharathiar University 

Program 6- Write a Shell script to determine whether a given number is a prime number or not.

Source Code



# Function to check if a number is prime
is_prime() {
    if [ $1 -le 1 ]; then
        echo "$1 is not a prime number."
        exit 0
    fi
   
    d=2
    while [ $d -lt $1 ]; do
        if [ $(($1 % $d)) -eq 0 ]; then
            echo "$1 is not a prime number."
            exit 0
        fi
        d=$((d + 1))
    done
    echo "$1 is a prime number."
}

# Main program starts here

# Prompt the user to enter the number
    echo "Enter the number: "
    read number
   
# Check if the number is prime
is_prime $number


Sample output:



Program 5 B.Sc Computer Science with Data analytics Linux and Shell Programming - Lab Bharathiar University -Write a shell script (small calculator) that adds, subtracts, multiplies and divides the two given numbers.

 

B.Sc Computer Science  with Data analytics

Linux and Shell Programming - Lab  

Bharathiar University 

Program 5- -Write a shell script (small calculator) that adds, subtracts, multiplies and divides the two given numbers. 

Source Code



# Function to perform addition
addition() {
    echo "$num1 + $num2" | bc
}

# Function to perform subtraction
subtraction() {
    echo "$num1 - $num2" | bc
}

# Function to perform multiplication
multiplication() {
    echo "$num1 * $num2" | bc
}

# Function to perform division
division() {
    if [ "$num2" -eq 0 ]; then
        echo "Error: Division by zero!"
    else
        echo "scale=2; $num1 / $num2" | bc
    fi
}

# Main script starts here

# Prompt the user to enter the first number
echo "Enter the first number: "
    read num1

    # Prompt the user to enter the second number
    echo "Enter the second number: "
    read num2
       
while true; do
    # Prompt the user to select the operation
    echo "Select the operation:"
    echo "1. Addition"
    echo "2. Subtraction"
    echo "3. Multiplication"
    echo "4. Division"
    echo "5. Exit"
    echo "Enter your choice: "
    read choice

       
    case $choice in
        1) echo "Addition: $(addition)";;
        2) echo "Subtraction: $(subtraction)";;
        3) echo "Multiplication: $(multiplication)";;
        4) echo "Division: $(division)";;
        5) echo "Exiting..."; exit;;
        *) echo "Invalid choice";;
    esac
done


SAMPLE OUTPUT:




Thursday, February 29, 2024

Program 1 BCA Madras University BCA Object Oriented Programming using C++ Practical Madras University Program 1 Write a class to represent a complex number which has member functions to do the following

 

BCA

Object Oriented Programming using C++ Practical

Madras University 

Program 1

Write a class to represent a complex number which has member functions to do the following a. Set and show the value of the complex number b. Add, subtract and multiply two complex numbers c. Multiplying the complex number with a scalar value 

SOURCE CODE:
#include<iostream.h>
#include<iomanip.h>
#include<conio.h>
class ComplexNumber {
private:
    double real;
    double imaginary;

public:
ComplexNumber() {
real = 0;
imaginary = 0;
    }

    void set_value(double realPart, double imaginaryPart) {
real = realPart;
imaginary = imaginaryPart;
    }

 void show_value() {
     if(imaginary<0)
cout << real << " - " << -imaginary << "i" << endl;
else
cout << real << " + " << imaginary << "i" << endl;
 }

    void add(ComplexNumber c2) {
double r = real + c2.real;
double i = imaginary + c2.imaginary;
cout<<"Sum of two Complex numbers:"<<endl;
result(r, i);
    }

    void subtract(ComplexNumber c2) {
double r = real - c2.real;
double i = imaginary - c2.imaginary;
cout<<"Difference of two Complex numbers:"<<endl;
result(r, i);
  }

    void multiply(ComplexNumber c2) {
double r = real * c2.real - imaginary * c2.imaginary;
double i = real * c2.imaginary + imaginary * c2.real;
cout<<"Multiplication of two Complex numbers:"<<endl;
result(r, i);
}

    void multiply_scalar(double scalar) {
double r = real * scalar;
double i = imaginary * scalar;
result(r, i);
}

    void result(double real, double imaginary) {
if(imaginary<0)
cout << real << " - " << -imaginary << "i" << endl;
else
cout << real << " + " << imaginary << "i" << endl;

    }
};

int main() {
    ComplexNumber c1,c2;
    double r1,r2,i1,i2,scalar;
    clrscr();
    cout<<"Enter the real and imaginary part of 1st complex number:";
    cin>>r1;
    cin>>i1;
    cout<<"Enter the real and imaginary part of 2nd complex number:";
    cin>>r2;
    cin>>i2;
    c1.set_value(r1,i1);
    c2.set_value(r2,i2);

    // Set and show value
    cout<<"Complex Numbers:"<<endl;
    c1.show_value();
    c2.show_value();

    // Add
    c1.add(c2);
    // Subtract
    c1.subtract(c2);

    // Multiply
    c1.multiply(c2);

    // Multiply with scalar
    cout<<"Enter scalar value to multiply with complex numbers: "<<endl;
    cin>>scalar;
    c1.multiply_scalar(scalar);
    c2.multiply_scalar(scalar);
     getch();
    return 0;
}


SAMPLE OUTPUT:




Wednesday, February 28, 2024

Program 4 B.Sc Computer Science with Data analytics Linux and Shell Programming - Lab Bharathiar University - Write a Shell script to sort number in ascending order

 


B.Sc Computer Science  with Data analytics

Linux and Shell Programming - Lab  

Bharathiar University 

Program 4- Write a Shell script to sort number in ascending order

Source Code


#!/bin/bash

# Prompt the user to enter numbers
echo "Enter numbers separated by spaces:"
read numbers

# Check if input is provided
if [ -z "$numbers" ]; then
   echo "No input provided."
    exit 1
fi

# Sort numbers in ascending order

sorted=$(echo "$numbers" | tr ' ' '\n' | sort -n)
# Print sorted numbers
echo "Sorted numbers in ascending order:"
echo "$sorted"


Sample Output:



Monday, February 5, 2024

Program 3 B.Sc Computer Science with Data analytics Linux and Shell Programming - Lab Bharathiar University - Write a Shell script that accepts a filename, starting and ending line numbers as arguments and displays all the lines between the given line numbers

 

B.Sc Computer Science  with Data analytics

Linux and Shell Programming - Lab  

Bharathiar University 

Program 3- Write a Shell script that accepts a filename, starting and ending line numbers as arguments and displays all the lines between the given line numbers

Source Code

if [ "$#" -ne 3 ]; then

echo "Please Enter <filename> <starting_line> <ending_line> as argument"

exit 1

fi


filename=$1

s=$2

e=$3


sed -n $s,$e\p $filename



Sample Output




Program 12 BCA Madras University BCA Object Oriented Programming using C++ Practical Madras University Program 12 Implement a telephone directory using files

  BCA Object Oriented Programming using C++ Practical Madras University  Program 12  Implement a telephone directory using files SOURCE CODE...