[go: up one dir, main page]

0% found this document useful (0 votes)
60 views19 pages

Operating System Lab

The document contains 10 programs written in C programming language to perform various tasks using Linux commands. The programs include calculating the factorial of a number, finding the greatest among three numbers, searching for a pattern in a file, checking if a number is palindrome, checking if a number is prime, concatenating two strings, drawing a pyramid shape, calculating the average of marks scored in five subjects, performing switch case operation, and adding two matrices. The document also lists 27 common Linux commands and their uses.

Uploaded by

Naval Bihani
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
60 views19 pages

Operating System Lab

The document contains 10 programs written in C programming language to perform various tasks using Linux commands. The programs include calculating the factorial of a number, finding the greatest among three numbers, searching for a pattern in a file, checking if a number is palindrome, checking if a number is prime, concatenating two strings, drawing a pyramid shape, calculating the average of marks scored in five subjects, performing switch case operation, and adding two matrices. The document also lists 27 common Linux commands and their uses.

Uploaded by

Naval Bihani
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 19

1

Operating System Lab

Submitted to : Submitted by:


Mrs Amrita Ticku Deepak (17/cs08)

Deepak kumar
17/cs08
2

Linux Commands
1. who: It shows the names of users that are currently
logged in.

2. man: The man command is used to format and display


the man(manual) pages.

3. mkdir : this linux command is used to create a new


directory.

4. pwd: (print working directory ) write the full path name


of current working directory.

5. date: The ‘date’ command displays the current date.

Deepak kumar
17/cs08
3

6. cal: This command shows current month calender as


output.

7. time: It is used to determine the duration of execution


of particular command.

8. tty: This command is used to print the file name of the


terminal connected to standard input.

9. cat: This command is used to open or create a file.


Syntax= cat > file name (to create a file).
Ctrl +z= to move out from file.

Deepak kumar
17/cs08
4

10. cp : This command is used to copy one file to


another file.
Syntax= cp file1name file2 name.

11. echo : This command act like print , used to print


the statement.

12. pr : This command is used to print a file.


Syntax= pr file name.

13. cd : This command is used to change the current


directory.

14. rm : This command is used to remove a file.

15. rmdir : This command is used to remove a


directory.

Deepak kumar
17/cs08
5

16. history: this command is used to recap all the


commands done on the terminal.

17. clear: It clears the screen and give back ,the clean
screen for use.

18. lp : this command is used for printing purpose


through printer.

19. wc: this command is used for word count in a


particular file.

It comes with various options like: i)wc-l


ii)wc-c
iii)wc-w

Deepak kumar
17/cs08
6

20. uname: This command is used for displaying the


name of operating system.

21. bc : this command is used for mathematical


operation when we want to find out answer of any
mathematical expression. ie 17/2.

22. gzip: this command is used for compressing the


data of a file.

23. gunzip: This command is used for uncompressing


the data file.

24. chmod: used for changing the permissions of a


perticular file.
ex : reading writing and execution.
syntax= chmod 400 filename

Deepak kumar
17/cs08
7

25. mv : This command is used to move one file from


one user to another user.

26. sort : This command is used to perform sorting


operation in any file.
Syntax= sort –n filename\\ for numerical sorting.
Syntax= sort filename\\ for rest of sorting.

27. grep : This command is used for searching a


particular letter/word in a file.
Syntax =grep –w ‘word’ filename.

Deepak kumar
17/cs08
8

Program 1
Aim : Write a program to find out the factorial of a number.

Linux code:
echo “enter the number : ”
read a
fact=1
for ((i=$a;i>0;i--))
do
fact=$((fact*i))
done
echo “Factorial of $a is $fact”

output:

Deepak kumar
17/cs08
9

Program 2
Aim : Write a program to find the greatest among three
numbers.

Linux code:
echo “Enter three numbers :”
read a b c
if [ $a –gt $b ]
then
if [ $a –gt $c ]
then
echo “ $a is greatest ”
else
echo “$c is greatest ”
fi
else
if [ $b –gt $c ]
then
echo “$b is greatest”
else
echo “$c is greatest ”
fi
fi
output :

Deepak kumar
17/cs08
10

Program 3
Aim : write a program to find a pattern in a file.

Linux code:
echo “Enter the file name:”
read file1
echo “enter the pattern to search:”
read pattern
echo “find the $pattern in file”
grep “$pattern” $file1

output:

Deepak kumar
17/cs08
11

Program 4
Aim: write a program to check whether given number is
palindrome or not .

Linux code:
num=545
s=0
rev=""
temp=$num
while[ $num -gt 0 ]
do
s=$(( $num % 10 ))
num=$(( $num / 10 ))
rev=$( echo${rev}${s} )
done
if[ $temp -eq$rev ];
then
echo"Number is palindrome"
else
echo"Number is NOT palindrome"
fi

Output :

Deepak kumar
17/cs08
12

Program 5
Aim : write a program to check whether the number is
prime or not.
Linux code:
echo "Enter a number"
read a
flag=0
i=2
while [ $i -ge $(($a/2)) ]
do
if [ $(($i % 2)) == 0 ]
then
flag=1;
break
fi
done
if [ $flag == 1 ]
then
echo "$a is not a prime"
else
echo "$a is a Prime"
fi

Output :

Deepak kumar
17/cs08
13

Program 6
Aim : write a program to concatenate two strings.

Linux code:
echo Enter first string:
read s1
echo Enter second string:
read s2
s3=$s1$s2
len=`echo $s3 | wc -c`
len=`expr $len - 1`
echo “Concatinated string is $s3 of length $len”

Output:

Deepak kumar
17/cs08
14

Program 7

Aim :write a program to draw pyramid shape in c using linux.

Linux code:

#include<stdio.h>

Void main()

Int I,j,k;

Int space=5;

Int max=5;

For(j=1;j<=max;j++)

For(i=space;i>=0;i--)

Printf(“ ”);

For(k=1;k<=j;k++)

Printf(“*”);

Space--;

Printf(“\n”);

Output:

Deepak kumar
17/cs08
15

Program 8

Aim :write a program in c to calculate the average marks of five subjects of a student.

Linux code:

#include<stdio.h>

Void main()

Int s[10],sum=0,i,avg;

For(i=1;i<=5;i++)

Printf(“\nEnter the marks in subject %d :”,i);

Scanf(“%d”,&s[i]);

Sum+=s[i];

Printf(“\nTotal marks =%d”,sum);

Avg=(sum/5);

Printf(“\nAvg:=%d”,avg);

Printf(“\nPercentage=%d”,avg*100);

Output:

Deepak kumar
17/cs08
16

Program 9

Aim: write a program in c to perform switch case operation.

Linux code:

#include<stdio.h>

Void main()

Int z,a,b;

Printf(“\nEnter first number:\t”);

Scanf(“%d”,&a);

Printf(“\nEnter second number:\t”);

Scanf(“%d”,&b);

Printf(“\n1. To Add\n2.Divide\n3.Multiply\n4.Subtract”);

Printf(“\nEnter your choice:\t”)

Scanf(“%d”,&z);

Switch(z)

Case 1:printf(“\nAddition=%d”,a+b);

Break;

Case 2:printf(“\nDivision =%d”,a/b);

Break;

Case 3:printf(“\nMultiplication =%d”,a*b);

Break;

Case 4:printf(“\nSubtraction =%d”,a-b);

Break;

Output:

Deepak kumar
17/cs08
17

Program 10

Aim: Write a program to perform addition of two matrices.

Linux code:

#include<stdio.h>

void main()

int a[100][10],b[10][10],c[10][10],n,i,j;

printf("Enter the size of matrix lesser than 10=\t");

scanf("%d",&n);

for(i=0;i<n;i++)

for(j=0;j<n;j++)

printf("\nEnter value of A[%d][%d]=\t",i,j);

scanf("%d",&a[i][j]);

for(i=0;i<n;i++)

for(j=0;j<n;j++)

printf("\nEnter value B[%d][%d]=\t",i,j);

scanf("%d",&b[i][j]);

for(i=0;i<n;i++)

for(j=0;j<n;j++)

//printf("Enter value in A matrix i=\t"j)

Deepak kumar
17/cs08
18

c[i][j]=a[i][j]+b[i][j];

printf("--------------------C-MATRIX---------------------\n");

for(i=0;i<n;i++)

for(j=0;j<n;j++)

//printf("Enter value in A matrix i=\t"j)

printf("\t%d\t",c[i][j]=a[i][j]+b[i][j]);

printf("\n");

Output:

Deepak kumar
17/cs08
19

Deepak kumar
17/cs08

You might also like