(Q1.)
Write a script to calculate the factorial of a number entered by the user .
$vi factorial.sh
echo
"Enter the number : "
read
a
fact=1
while((a>0))
do
fact=$(($fact
* a))
a=$(($a
- 1))
done
echo
"The factorial of the given number is " $fact
For Output : $sh factorial.sh
Enter the number : 5
The factorial of the given
number is 120
*********************************************************************************
(Q2.) Write a script to check whether the
number entered by user is palindrome or not.
$vi palindrome.sh
echo
"Enter the number :"
read
num
n=$num
sum=0
while((num>0))
do
rem=$(($num%10))
num=$(($num/10))
sum=$(($sum*10+$rem))
done
if(($sum==$n))
then
echo
"The given number is a palindrome"
else
echo
"The given number is not a palindrome"
fi
For Output : $sh palindrome.sh
Enter the number : 221122
The given number is a palindrome
*********************************************************************************
(Q3.) Write a script to check whether the number
entered by the user is Armstrong or not.
$vi armstrong.sh
echo
"Enter the number : "
read
num
sum=0
n=$num
while((num>0))
do
rem=$(($num%10))
num=$(($num/10))
sum=$(($sum
+ $rem*$rem*$rem))
done
if(($sum==$n))
then
echo
"The number is armstrong"
else
echo
"The number is not armstrong"
fi
For Output : $sh armstrong.sh
Enter the number : 153
The number is armstrong
*********************************************************************************
(Q4.) Write a script to generate a Fibonacci
series where number of terms will be entered by the user .
$vi fibonacci.sh
echo
"Enter the number of terms in fibonacci series : "
read
a
fo=0
f1=1
echo
"The series is shown below :"
echo
-n $fo " " $f1 " "
for((i=1;
i<$a-1; i++))
do
sum=$(($fo+$f1))
fo=$f1
f1=$sum
echo
-n $sum " "
done
For Output : $sh fibonacci.sh
Enter the number of terms
in fibonacci series : 8
The series is shown below :
0 1
1 2 3
5 8 13
*********************************************************************************
(Q5.) Write a script to check whether the
number entered by the user is prime or not .
$vi prime.sh
echo
"Enter the number : "
read
n
c=0
for((i=2;
i<=$n/2; i++))
do
if(($n%i==0))
then
c=1
fi
done
if(($c==o))
then
echo
"The number is prime"
else
echo
"The number is not prime"
fi
For Output : $sh prime.sh
Enter the number : 7
The number is prime
*********************************************************************************
(Q6.) Write a script to generate the following
pattern where maximum limit is entered by the user .
1
1 2
1 2 3
1 2 3 4 ...
$vi pattern.sh
echo
"Enter the maximum limit : "
read
n
for((i=1;
i<$n+2; i++))
do
for((j=1;
j<i; j++))
do
echo
-n $j
done
echo
" "
done
For Output : $sh pattern.sh
Enter the maximum limit : 4
1
1 2
1 2 3
1 2 3 4
*********************************************************************************
(Q7.)
Write a script to generate the multiplication table of the number entered by
the user.
$vi table.sh
echo
"Enter the number : "
read
n
for((i=1;
i<11; i++))
do
echo
$n " * " $i " = " $(($n*$i))
done
For Output : $sh table.sh
Enter the number : 7
7 * 1 = 7
7 * 2 = 14
7 * 3 = 21
7 * 4 = 28
7 * 5 = 35
7 * 6 = 42
7 * 7 = 49
7 * 8 = 56
7 * 9 = 63
7 * 10 = 70
*********************************************************************************
(Q8.)
Write a script to check whether the filename entered by the user exists or not.
If it exists then check whether it is empty or not.
$vi filecheck.sh
echo
"Enter the file name : "
read
a
if
test -e $a
then
echo
"File Exists"
if
test -s $a
then
echo
"File is not empty"
else
echo
"File is empty"
fi
else
echo
"File does not exist"
fi
For Output : $sh filecheck.sh
Enter the filename : frndz
File Exists
File is not empty
*********************************************************************************
(Q9.) Write a script to check whether the string
entered by the user exists in the filename entered by the user.
$vi strchk.sh
echo
"Enter String :"
read
a
echo
"Enter filename :"
read
b
if
grep -c $a $b
then
echo
"The given string is found ."
else
echo
"The given string is not found ."
fi
For Output : $sh strchk.sh
Enter string : make
Enter filename : frndz
The given string is found .
*********************************************************************************
(Q10.)
Write a script to run a C program .
$vi cprog.c
#include<stdio.h>
void
main()
{int
a;
printf("Enter
a number : ");
scanf("%d",&a);
if(a%2==0)
printf("The
number is even");
else
printf("The
number is odd");
}
To compile the program : $cc cprog.c
To run the program :
./a.out
Output : Enter
a number : 28
The number is even
*********************************************************************************
No comments:
Post a Comment