0% found this document useful (0 votes)
37 views

Unix Ass 2

The document contains 4 programming assignments: 1) a shell program to print even numbers between 1 and a given input, 2) check if a number is prime, 3) check if a number is perfect, 4) compute sum of evens and product of odds between 1 and a given input. For each assignment, it includes the source code, execution instructions, and example output.

Uploaded by

moferix892
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)
37 views

Unix Ass 2

The document contains 4 programming assignments: 1) a shell program to print even numbers between 1 and a given input, 2) check if a number is prime, 3) check if a number is perfect, 4) compute sum of evens and product of odds between 1 and a given input. For each assignment, it includes the source code, execution instructions, and example output.

Uploaded by

moferix892
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
You are on page 1/ 4

ASSIGNMENT 2: -

Date of Assignment: 26/09/2023


Date of Submission: 17/10/2023

1. Write a shell program that will accept an integer value as runtime input and print the
even numbers between 1 to that number.
2. Write a shell program that will accept an integer value as run time input and will
check if it is prime or not.
3. Write a shell program that will accept an integer value as run time input and will
check if it is perfect number or not.
4. Write a shell Program that will accept an integer value as command line argument and
will perform the following operations and will print result;
(a) Compute the sum of all even numbers present in between them,
(b) Compute the product of all odd numbers present in between them.
-------------------------------------------------------------------------------
#ASSIGNMENT 2.1
-------------------------------------------------------------------------------
#!/bin/sh
#Ass2-1.sh: A shell program that will accept an integer value as runtime input
and print the even numbers from 1 to that number.

#Author: SOUMYA RANJAN PARIDA


#Date: 26/09/2023

echo -e "Enter a integer number: \c"


read a
if [ $? -ne 0 ]; then
echo "Please Enter Your Value..."
exit 1
fi

c=1
echo "All even values between 1 to $a: "
while [ $c -lt $a ]; do
if [ `expr $c % 2` -eq 0 ]; then
echo -e "$c \c"
fi

c=`expr $c + 1`
done

echo -e "\n"
#END OF PRG
-------------------------------------------------------------------------------
#OUTPUT:
[user1@localhost ~]$ vi Ass2-1.sh
[user1@localhost ~]$ chmod +x Ass2-1.sh
[user1@localhost ~]$ ./Ass2-1.sh
Enter a integer number: 20
All even values between 1 to 20:
2 4 6 8 10 12 14 16 18
-------------------------------------------------------------------------------
#ASSIGNMENT 2.2
-------------------------------------------------------------------------------
#!/bin/sh
#Ass2-2.sh: A shell program that will accept an integer value as run time input
and will check if it is prime or not.

#Author: SOUMYA RANJAN PARIDA


#Date: 26/09/2023

echo -e "Enter a integer number: \c"


read a
if [ $? -ne 0 ]; then
echo "Please Enter Your Value..."
exit 1
fi

c=1
while [ $c -le `expr $a / 2` ]; do
if [ `expr $a % $c` -eq 0 ]; then
temp=1
break
fi

c=`expr $c + 1`
done

if [ $temp -ne 1 ] || [ $a -eq 2 ] || [ $a -eq 3 ]; then


echo "$a is a Prime Number."
elif [ $a -eq 1 ]; then
echo "$a is a Special Number."
else
echo "$a is not a Prime Number."
fi

#END OF PRG
-------------------------------------------------------------------------------
#OUTPUT:
[user1@localhost ~]$ vi Ass2-2.sh
[user1@localhost ~]$ chmod +x Ass2-2.sh
[user1@localhost ~]$ ./Ass2-2.sh
Enter a integer number: 69
69 is not a Prime Number.
-------------------------------------------------------------------------------
-------------------------------------------------------------------------------
#ASSIGNMENT 2.3
-------------------------------------------------------------------------------
#!/bin/sh
#Ass2-3.sh: A shell program that will accept an integer value as run time input
and will check if it is perfect number or not.

#Author: SOUMYA RANJAN PARIDA


#Date: 26/09/2023

echo -e "Enter a integer number: \c"


read a
if [ $? -ne 0 ]; then
echo "Please Enter Your Value..."
exit 1
fi

c=1
res=0
while [ $c -lt $a ]; do
if [ `expr $a % $c` -eq 0 ]; then
res=`expr $res + $c`
fi

c=`expr $c + 1`
done

if [ $a -eq $res ]; then


echo "$a is a Perfect Number."
else
echo "$a is not a Prefect Number."
fi

#END OF PRG
-------------------------------------------------------------------------------
#OUTPUT:
[user1@localhost ~]$ vi Ass2-3.sh
[user1@localhost ~]$ cnmod +x Ass2-3.sh
[user1@localhost ~]$ ./Ass2-3.sh
Enter a integer number: 6
6 is a Perfect Number.
-------------------------------------------------------------------------------
-------------------------------------------------------------------------------
#ASSIGNMENT 2.4
-------------------------------------------------------------------------------
#!/bin/sh
#Ass2-4.sh: A shell Program that will accept an integer value as command line
argument and will perform the following operations and will print result; (a)
Compute the sum of all even numbers present in between them, (b) Compute the
product of all odd numbers present in between them.

#Author: SOUMYA RANJAN PARIDA


#Date: 26/09/2023

if [ $# -ne 1 ]; then
echo "Please Enter Your Value as Command Line Agrument..."
exit 1
fi
echo "The Value Entered: $1"

c=1
even=0
odd=1
while [ $c -lt $1 ]; do
if [ `expr $c % 2` -eq 0 ]; then
even=`expr $even + $c`
else
odd=`expr $odd \* $c`
fi

c=`expr $c + 1`
done

echo "Sum of all Even numbers present between 1 to $1: $even"


echo "Product of all Odd numbers present between 1 to $1: $odd"

#END OF PRG
-------------------------------------------------------------------------------
#OUTPUT:
[user1@localhost ~]$ vi Ass2-4.sh
[user1@localhost ~]$ chmod +x Ass2-4.sh
[user1@localhost ~]$ ./Ass2-4.sh 20
The Value Entered: 20
Sum of all Even numbers present between 1 to 20: 90
Product of all Odd numbers present between 1 to 20: 654729075
-------------------------------------------------------------------------------

You might also like