CP 2
CP 2
EX: NO: 2a
Swapping 2 numbers:
echo enter a b
read a b
echo before swapping
echo a=$a
echo b=$b
echo after swapping
a=`expr $a + $b`
b=`expr $a - $b`
a=`expr $a - $b`
echo a=$a
echo b=$b
OUTPUT:
enter a b
78
before swapping
a=7
b=8
after swapping
a=8
b=7
-------------------------------------------------------------------------------------EX: NO: 2b
STRING CONCATENATION
EX:NO:2c
SIMPLE INTEREST
ARITHMETIC OPERATION
EX:NO:3b
-----------------------------------------------------------------------------------EX: NO: 3C
FINDING THE ODD OR EVEN NUMBER
# Even or Odd no
echo Enter N
read n
n1=`expr $n % 2`
if [ $n1 -eq 0 ]
then echo IT IS EVEN NO.
else
echo IT IT ODD NO
fi
OUTPUT:
Enter N
3
IT IT ODD NO
-------------------------------------------------------------------------------------------EX: NO: 3d
M2 : 100 Pass
M3 : 99 PASS
----------------------------------------------------------TOTAL : 299
AVERAGE : 99
----------------------------------------------------------RESULT : PASS
---------------------------------------------------------------------------------EX:NO:3e
COMMAND LINE SUBSTITUTION
echo COMMAND LINE ARGUMENTS
echo total no. of arguments : $#
if [ $# -ne 2 ]
then
echo Required 2 arguments
else
echo first argument is $1
echo Second argument is $2
fi
OUTPUT:
Sh command
COMMAND LINE ARGUMENTS
total no. of arguments : 0
Required 2 arguments
Sh command hai bye
COMMAND LINE ARGUMENTS
total no. of arguments : 2
first argument is hai
Second argument is bye
----------------------------------------------------------------------------EX: NO: 3f
FINDING BIGGEST OF THREE NUMBERS
#Biggest of three numbers
echo Enter a b c
read a b c
if [ $a -gt $b -a $a -gt $c ]
then echo A is Greater
elif [ $b -gt $c ]
then echo B is Greater
else
echo C is Greater
fi
OUTPUT:
Enter a b c
123
C is Greater
-----------------------------------------------------------------------------------------------EX: NO: 3g
ARITHMETIC OPERATION
ch=1
until [ $ch -eq 5 ]
#Arithmetic Program
echo Enter 2 Nos
read a b
ch=1
until [ $ch -eq 5 ]
do
echo 1.Addition
echo 2.Subtraction
echo 3.Multiplication
echo 4.Division
echo 5.Exit
echo Enter your Choice
read ch
case $ch in
1) echo Addition
echo
ans=`expr $a + $b`;;
2)echo Subtraction
echo
ans=`expr $a - $b`;;
3) echo Multiplication
echo
ans=`expr $a \* $b`;;
4)echo Division
echo
ans=`expr $a / $b
esac
echo $ans
done
Output:
Enter 2 Nos2 6INSERT -1.Addition-2.Subtraction
3.Multiplication
4.Division
5.Exit
Enter your Choice
1
Addition
8
1.Addition
2.Subtraction
3.Multiplication
4.Division
5.Exit
MENU OPERATION
it55 pts/7
Apr 21 15:16 (192.170.170.10)
it43 pts/19
Apr 21 15:17 (192.170.170.5)
menu
1.who
2.date
3.wc
4.cal
5.sort
6.ls
7.exit
Enter ur choice
2
Tue Apr 21 15:19:21 IST 2009
menu
1.who
2.date
3.wc
4.cal
5.sort
6.ls
7.exit
Enter ur choice
3
enter filename
reverse
10 35 151 reverse
menu
1.who
2.date
3.wc
4.cal
5.sort
6.ls
7.exit
Enter ur choice
4
April 2009
Su Mo Tu We Th Fr Sa
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30
menu
1.who
2.date
3.wc
4.cal
5.sort
6.ls
7.exit
Enter ur choice
5
Enter filename
reverse
a=`expr $n % 10`
do
done
echo Enter a number
echo The reversed No is $rev
n=`expr $n / 10`
read n
rev=0
rev=`expr $rev "*" 10 + $a`
while [ $n -ne 0 ]
menu
1.who
2.date
3.wc
4.cal
5.sort
6.ls
7.exit
Enter ur choice
6
arith arithmetic bigthree factorial menu oe reverse student
menu
1.who
2.date
3.wc
4.cal
5.sort
6.ls
7.exit
Enter ur choice
7
-------------------------------------------------------------------------------------EX: NO: 3j
FACTORIAL
#Factorial of the given number
echo Enter N
read n
i=1
f=1
while [ $i -le $n ]
do
f=`expr $f \* $i`
i=`expr $i + 1`
done
echo Factorial of the given number is $f
OUTPUT:
Enter N
6
Factorial of the given number is 720
---------------------------------------------------------------------------------------EX: NO: 3k REVERSING THE GIVEN NUMBER
echo Enter a number
read n
rev=0
while [ $n -ne 0 ]
do
a=`expr $n % 10`
rev=`expr $rev "*" 10 + $a`
n=`expr $n / 10`
done
echo The reversed No is $rev
OUTPUT:
Enter a number
123454
The reversed No is 454321
--------------------------------------------------------------------------------------------EX: NO: 3l
PALANDROME
OUTPUT:
Enter string
mala
THE GIVEN STRING IS NOT A PALINDROME
Enter string
priya
THE GIVEN STRING IS NOT A PALINDROME
EX: NO: 3m
sum=0
for i in 1 2 3 4 5 6 7 8 9 10
do
sum=`expr $sum + $i`
done
echo "the sum of first ten nos is: $sum"
The sum of first ten nos is: 55
EX: NO: 3n
ans=" "
echo "Do u want to enter value for ans(y/Y) ?:"
read ans
while [ $ans = y -o $ans = Y ]
do
echo "enter name : "
read name
echo $name >> names
wish=" "
echo "Do U want to continue ?"
read wish
if [ $wish = y -o $wish = y ]
then
continue
else
echo "the content of the file name is :`cat names`"
exit
fi
done
OUTPUT:
Do u want to enter value for ans(y/Y) ?:
y
enter name :
sumi
Do U want to continue ?
y
enter name :
rubi
Do U want to continue ?
n
the content of the file name is : sumirubi
-----------------------------------------------------------------------------------------EX: NO: 4
#include<stdio.h>
main()
{
int x,y;
int power(int,int);
printf("Enter x and y values...");
scanf("%d%d",&x,&y);
printf("%d to the power of %d is ...%d",x,y,power(x,y));
}
int power(int x,int y)
{
int p=1;
if(y>=0)
while(y--)
p*=x;
else
while(y++)
p/=x;
return(p);
}
OUTPUT:
cc power.c
./a.out
Enter x and y values...3 3
3 to the power of 3 is ...27
EX:NO:5
NESTED FUNCTION
#include<stdio.h>
main()
{
int x,y,z;
int ratio(int,int,int);
printf("enter x,y,z values");
scanf("%d%d%d",&x,&y,&z);
printf("ratio is %d \n",ratio(x,y,z));
}
int ratio(int x,int y,int z)
{
int diff(int,int);
if(diff(y,z))
return(x/(y-z));
else
return(0);
}
int diff(int m,int n)
{
if(m!=n)
return(1);
else
return(0);
}
OUTPUT:
cc nest.c
./a.out
enter x,y,z values 10
enter x,y,z values10 10 5
ratio is 2
--------------------------------------------------------------------------------------------EX: NO: 6
DEMONSTRATION OF GLOBAL VARIABLE
#include<stdio.h>
int a;
main()
{
a=39;
int f1();
int f2();
int f3();
printf("A....%d\n",a);
printf("A....%d\n",f1());
printf("A....%d\n",f2());
printf("A......%d\n",f3());
}
f1()
{
a=a+10;
return(a);
}
f2()
{
a=a+20;
return(a);
}
f3()
{
a=a+30;
return(a);
}
OUTPUT:
cc global.c
./a.out
A....39
A....49
A....69
A......99
--------------------------------------------------------------------------------------------EX: NO: 7
#include<stdio.h>
typedef struct
{
char* name;
int no;
int age;
}record;
main()
{
void fun(record *pt);
static record std = {"LAK",15,25};
printf("%s\t%d\t%d\t",std.name,std.no,std.age);
fun(&std);
printf("\n%s\t%d\t%d",std.name,std.no,std.age);
}
void fun(record *pt)
{
pt->name = "NNNN";
pt->no = 16;
pt->age= 26;
}
OUTPUT:
LAK 15 25
NNNN 16 26
----------------------------------------------------------------------------------------------------------EX:NO:8
#include<stdio.h>
#include<stdlib.h>
main()
{
int *a,*n,size;
printf("enter the size");
scanf("%d",&size);
n=(int *)malloc(size * sizeof(int));
printf("address of the first byte is %u \n",n);
printf("enter the value");
for(a=n;a<n+size;a++)
scanf("%d",a);
printf("printing the value...\n");
for(a=n+size-1;a>=n;a--)
printf("%d is stored in address %u\n",*a,a);
}
enter the size 10
address of the first byte is 5246992
enter the value3456
566
454
2442
907
67
45
40
33
3
printing the value...
3 is stored in address 5247028
33 is stored in address 5247024
40 is stored in address 5247020
45 is stored in address 5247016
--------------------------------------------------------------------------------------EX: NO: 10 a
FILE HANDILING
PROGRAM TO CREATE A FILE
#include<stdio.h>
main()
{
int fd;
int protection_mode;
protection_mode=0644;
if((fd=creat("dept.dat",protection_mode))==-1)
{
perror("error!file not created");
exit(0);
}
else
printf("file is created successfully");
}
OUTPUT:
cc create.c
./a.out
file is created successfully.
#include<stdio.h>
main()
{
int fd;
if((fd=open("oop.dat",0))==-1)
{
perror("cannot open the file file1.dat");
exit(0);
}
else
{
printf("opened successfully");
}
}
OUTPUT:
cc open.c
./a.out
cannot open the file file1.dat: No such file or directory
---------------------------------------------------------------------------------------------EX: NO: 10 c PROGRAMM TO OPEN WITH CREATE
#include<stdio.h>
main()
{
int fd;
if((open("file1.dat",0,0644))==-1)
{
fd=creat("file1.dat",0);
printf("file is created and opened successfully");
}
else
{
printf("successfully created and opened");
}
}
OUTPUT:
cc co.c
./a.out
file is created and opened successfully
----------------------------------------------------------------------------------------EX: NO: 10d
#include<stdio.h>
main()
{
char buffer[10],x_reads;
int fd;
int x_read;
if((fd=open("oop.dat",0))==-1)
{
printf("error cannot open file1.txt");
exit(1);
}
do
{
printf("reading the file\n");
read(fd,buffer,10);
printf("%s\n",buffer);
}
while(x_read==10);
close(fd);
}
OUTPUT:
cc read.c
./a.out
error cannot open file1.txt
----------------------------------------------------------------------------------------------EX:NO:10 e PROGRAMM TO WRITE A FILE
#include<stdio.h>
main()
{
char buffer[10];
int fd;
printf("enter the input\n");
scanf("%s\n",buffer);
if((fd=creat("write.txt",0660))==-1)
{
printf("error cannot create the file");
exit(1);
}
write(fd,buffer,(unsigned)strlen(buffer));
printf("\n");
close(fd);
}
OUTPUT:
cc write.c
./a.out
enter the input
vcew
cat write.txt
vcew