Unix Shell Programming

                           Unix Shell Programming


Shell Programming
1.program for ARMSTRONG number
Armstrong Number
echo  enter the number
read  n
temp=$n
sum=0
while  [  $n - gt  0  ]
do
r=`expr  $n  %  10`
sum=`expr  $sum  +  \(  $r  \* $r  \*  $r  \)`
n=`expr  $n /  10`
done
if  [  $temp  -eq  $sum ]
then
echo  $temp is armstrong
else
echo  $temp is not armstrong
fi
exit 0

Output 1:
[cse3b520@CSE ~]$ sh a.sh
enter the number
153
153 is armstrong


2.program for FACTORIAL
echo enter a number
read n
sum=1
while  [  $n  -gt  1  ]
do
sum=`expr  $sum  \*  $n`
n=`expr  $n  -  1`
done
echo  $sum
exit 0

Output 1:
[cse3b520@CSE ~]$ sh n.sh
enter a number
6
720

3.Program for LARGEST NUMBER
if  [  $1  -gt  $2  ]
then
x=$1
elif  [  $2  -gt  $3 ]
then
x=$2
else
x=$3
fi
echo  largest number is   $x
exit  0

Output 1:
[cse3b520@CSE ~]$ sh g.sh 12 34 45
largest number is 45


4.Program for PERFECT NUMBER
echo enter a number
read no
i=1
ans=0
while  [  $i  -le  `expr  $no  /  2` ]
do
if  [  `expr  $no  %  $i`  -eq  0  ]
then
ans=`expr  $ans  +  $i`
fi
i=`expr  $i  +  1`
done
if  [  $no  -eq  $ans ]
then  echo  $no is perfect
else
echo  $no is not perfect
fi

Output 1:
 [cse3b520@CSE ~]$ sh p.sh
enter a number
34
34 is not perfect

5. Program for REVERSE
echo enter a num
read num
rev=0
while [ $num  -gt  0  ]
do
k=`expr  $num  \%  10`
l=`expr   $rev  \*  10`
rev=`expr  $l  +  $k`
num=`expr  $num  /  10`
done
echo reverse number is $rev
exit 0

Output 1:
 [cse3b520@CSE ~]$ sh r.sh
enter a num
12345
reverse number is 54321

6. Program for EVEN OR ODD
echo  enter a number
read n
i=1
while  [  $i  -le  $n  ]
do
echo enter term
read  x
if  [  `expr   $x  \%  2`  -eq  0  ]
then
echo  $x is even
else
echo  $x is odd
fi
i=`expr  $i  +  1`
done

Output 1:
[cse3b520@CSE ~]$ sh eo.sh
enter a number
4
enter term
3
3 is odd
enter term
2
2 is even
enter term
7
7 is odd
enter term
8
8 is even



7. Program for FIBONACCI
echo enter the number of terms
read n
f1=0
f2=1
i=1
echo  $f1
echo  $f2
while [  $i  -le  $n  ]
do
f3=`expr  $f1  +  $f2`
f1=$f2
f2=$f3
i=`expr  $i  +  1`
echo  $f3
done
exit 0
Output 1:
[cse3b520@CSE ~]$ sh fb.sh
enter the number of terms
5
0
1
1
2
3
5
8
8. Program for PALINDROME
echo enter a num
read num
rev=0
temp=$num
while  [ $num  -gt  0  ]
do
k=`expr  $num  \%  10`
l=`expr  $rev  \*  10`
rev=`expr  $l  +  $k`
num=`expr  $num  /  10`
done
if  [  $rev  -eq  $temp  ]
then
echo  $temp is a palindrome
else
echo  $temp is not a palindrome
fi
exit 0
Output 1:
[cse3b520@CSE ~]$ sh fbb.sh
enter a num
234
234 is not a palindrome

9. Program for prime or not prime
echo enter the number
read n
count=0
i=1
while  [ $i   -le  $n ]
do
if  [  `expr  $n  %  $i`  -eq  0  ]
then
count=`expr  $count  +  1`
fi
i=`expr  $i  +  1`
done
if  [  $count  -eq  2  ]
then
echo   $n is prime
else
echo   $n is not prime
fi


Output 1:
 [cse3b520@CSE ~]$ sh p1.sh
enter the number
3
3 is prime

10. Program for SUM OF N NUMBERS
echo enter the number
read n
i=1
sum=0
while  [  $i   -le  $n ]
do
sum=`expr  $sum  +  $i`
i=`expr  $i  +  1`
done
echo sum of numbers : $sum
exit 0


Output 1:
 [cse3b520@CSE ~]$ sh sum.sh
enter the number
20
sum of numbers : 210

netaji gandi Thursday, July 11, 2019
UNIX and Shell Scripting Programming Training



1.Introduction to Unix
Download

2.Simple Commands
Download

3.Generic Concepts
Download

4.Editors Under Unix
Download

5.File Searching Utilities
Download

6.File Filters
Download

7.Security and Permissions
Download

8.Making New Commands by joining commands
Download

9. Awk command
Download

10.Backup and disk management commands
Download

11.Process in Linux
Download

12.Network related commands
Download

13.Shell Programming
Download

14.C Shell
Download

15. Korn Shell
Download

netaji gandi Wednesday, July 10, 2019
Python Programing Lab EXERCISE - 4(a)
Aim: Write a program to find the sum of all primes below two million.

Description: 


                  A Prime number is a natural number greater than 1 that has no positive divisors other than 1 and itself. A natural number greater than 1 and that is not a prime number is called a composite number. Here, We are using a for loop and a break statement.
break: break statement terminates the loop and resumes execution at the next statement, just like the traditional break statement in C. The break statement can be used in both while and for loops. If we are using break statement inside a nested loop, it stops the execution of the innermost loop and start executing the next line of code after the block.

Algorithm:


Step1: Start 

Step2: Initialize sum = 0 and i = 2
Step3: Repeat Steps 4 to 13 while i < 2000000 
Step4: Initialize c = 0 
Step5: Check whether i is greater than 2 and i is divisible by 2 or not. If yes, goto Step6 else, goto                                                                                                                                                               Step7 
Step6: Set c = 1 and goto Step12 
Step7: Initialize j = 3 
Step8: Repeat Steps 9 to 11 while j <= int(i**0.5) 
Step9: Check whether i is divisible by j or not. If yes, goto Step10. Otherwise, goto Step11 
Step10: Set c = 1 and goto Step12 
Step11: Increment j by 2 
Step12: Check whether c is 0 or not. If yes, goto Step13 
Step13: Add i to the sum and store the result in sum 
Step14: Display sum 
Step15: Stop

Program:

print("To find sum of prime no's")
sum=0
for i in range(2,2000000):
    flag=0
    for j in range(2,i):
       if i%j==0:
          flag=1
       if flag==0:
           print(i)
           sum=sum+i
print sum





netaji gandi Tuesday, July 9, 2019

Java Programming Lab

☕ Java Programming Lab Select a laboratory session to view programs and documentation Week 01 ...