Upload
hydrushya-abbas
View
220
Download
0
Embed Size (px)
Citation preview
8/2/2019 Output Unix Program-C
1/38
Employee Details
Source Code:
#include
main()
{
int empno;
char empname[25], dept[25];
FILE *ptr;
ptr=fopen("emp1.txt","w");
printf("enter name,no,dept");
scanf("%s%d%s",empname,&empno,dept);
fclose(ptr);
ptr=fopen("emp1.txt","r");
fscanf(ptr,"%s%d%s",empname,&empno,dept);
printf("emp name : %s\n",empname);
printf("emp no : %d\n",empno);
printf("dept: %s\n",dept);
}
Output:
"employeee.c" 17L, 349C written
[it priya@localhost it priya]$ cc employeee.c
[it priya @localhost it priya]$ ./a.out
enter name,no,dept
dss
301
Marketing
emp name : dss
emp no : 301
dept: Marketing
[it priya @localhost it priya]$
8/2/2019 Output Unix Program-C
2/38
Swap two number using Pointers
Source Code:
#include
void swap(int *,int *);main()
{
int a=10, b=20;
printf("before swapping : \n a=%d \n b=%d \n",a,b);
swap(&a,&b);
}
void swap(int *x,int *y)
{
int z;
z=*x;
*x=*y;
*y=z;
printf("after swapping : \n a=%d \n b=%d\n", *x,*y);
}
Output:
"two.c" 17L, 243C written
[it priya @localhost it priya]$ cc two.c
[it priya @localhost it priya]$ ./a.outbefore swapping :
a=10
b=20
after swapping :
a=20
b=10
8/2/2019 Output Unix Program-C
3/38
Student Details Using pointers
Source Code:
#include
main()
{
int regno,m1,m2,m3,total;
float avg;
char name[25];
int *p1,*p2,*p3;
p1=&m1;
p2=&m2;
p3=&m3;
printf("\n Enter student name");
scanf("%s",name);
puts("Enter regno");
scanf("%d",®no);
printf("Enter the marks for 3 subjects");
scanf("%d\n%d\n%d",&m1,&m2,&m3);
total=*p1+*p2+*p3;
avg=total/3;
printf("\n Student details \n");
printf("Name :%s\n",name:);
printf("Reg no:%d\n",regno:);
printf("Subject mark 1:%d\n",*p1);
printf("Subject mark 2:%d\n",*p2);
printf("Subject mark 3:%d\n",*p3);
printf("Total:%d\n",total);
printf("Average:%f\n",avg);
}
8/2/2019 Output Unix Program-C
4/38
Output:
[it priya @localhost it priya]$ cc js4.c
[it priya @localhost it priya]$ ./a.out
Enter student name:dishanth
Enter regno:
54646
Enter the marks for 3 subjects23
23
23
Student details
Name :arun
Reg no:54646
Subject mark 1:23
Subject mark 2:23
Subject mark 3:23
Total:69
Average:23.000000
8/2/2019 Output Unix Program-C
5/38
Menu Driven Arithmetic Operations using Functions
Source Code:
#include
main()
{
int ch,a,b,c;
printf("\n Perform Arithemetic operations");
printf("\n Enter the values of a and b \n");
scanf("%d\n%d",&a,&b);
while(1)
{
printf("\n 1.Addition \n2. Subtraction \n3.Multiplication \n4. Division\n");
printf("\n Enter your choice");
scanf("\n%d",&ch);
switch(ch)
{
case 1:
add(a,b);
break;
case 2:
sub(a,b);
break;
case 3:
mul(a,b);
break;
case 4:
div(a,b);
break;
default:
printf("\n Wrong value");
exit(0);
}}
}
int add(int p, int q)
{
int r;
r=p+q;
8/2/2019 Output Unix Program-C
6/38
printf("\n Answer is %d\n",r);
}
int sub(int p, int q)
{
int r;
r=p-q;
printf("\n Answer is %d\n",r);
}
int mul(int p, int q)
{
int r;
r=p*q;
printf("\n Answer is %d\n",r);
8/2/2019 Output Unix Program-C
7/38
Output:
[it priya @localhost it priya]$ cc functions.c
[it priya @localhost it priya]$ ./a.out
Perform Arithemetic operations
Enter the values of a and b
5 6
1.Addition
2. Subtraction
3.Multiplication
4. Division
Enter your choice1
Answer is 11
1.Addition
2. Subtraction
3.Multiplication
4. Division
Enter your choice2
Answer is -1
1.Addition
2. Subtraction
3.Multiplication
4. Division
Enter your choice3
Answer is 30
1.Addition
2. Subtraction
3.Multiplication
4. Division
8/2/2019 Output Unix Program-C
8/38
Tower of Hanoi
Source Code:
#include
int main()
{
void hanoi(int, char, char, char);
int n;
printf("\n Enter number of disks:\n");
scanf("%d",&n);
hanoi(n,'L','R','C');
return 0;
}
void hanoi(int n, char from, char to, char temp)
{
if(n>0)
{
hanoi(n-1,from,temp,to);
printf("Move disk %d from %c %c\n",n,from,to);
hanoi(n-1,temp,to,from);
}
}
Output:
"hanoi.c" 20L, 325C written
[it priya @localhost it priya]$ cc hanoi.c
[it priya @localhost it priya]$ ./a.out
Enter number of disks:
3
Move disk 1 from L R
Move disk 2 from L C
Move disk 1 from R C
Move disk 3 from L RMove disk 1 from C L
Move disk 2 from C R
Move disk 1 from L R
8/2/2019 Output Unix Program-C
9/38
Memory Allocation
Source Code:
#include
int main()
{
char *p;
p=(char*)malloc(6);
strcpy(p,"MADRAS");
printf("Memory contains %s\n",p);
p=(char*)realloc(p,7);
strcpy(p,"CHENNAI");
printf("\nMemory now contains %s\n",p);
free(p);
return 0;
}
Output:
"js1.c" 14L, 220C written
[it priya @localhost it priya]$ cc js1.c
[it priya @localhost it priya]$ ./a.out
Memory controls MADRAS
Memory now contains CHENNAI
8/2/2019 Output Unix Program-C
10/38
Electricity Bill
Source Code:
#include
int main()
{
int unit,bill;
float amt;
char name[50];
printf("Enter name");
scanf("%s",name);
printf("Enter the bill no");
scanf("%d",&bill);
printf("Enter the total no of units");
scanf("%d",&unit);
if(unit>300)
{
amt=unit*4;
}
else
{
amt=unit*4.50;
}
printf("\n Name: %s",name);
printf("\n Bill no :%d",bill);
printf("\n Units used :%d",unit);
printf("\n Net amt: %f",amt);
Output:
[it priya @localhost it priya]$ cc js3.c
[it priya @localhost it priya]$ ./a.out
Enter name:ramu
Enter the bill no:5656Enter the total no of units:45
Name: ramu
Bill no: 5656
Units used :45
8/2/2019 Output Unix Program-C
11/38
Read And Write The Content Of Files
Source Code:
#include
int main()
{char c;
FILE*fp;
printf("\nEnter the contents of file.....");
fp=fopen("data.txt","w");
while((c=getc(stdin))!='0')
fputc(c,fp);
fclose(fp);
fp=fopen("data.txt","r");
printf("\n File contents are \n");
do
{
c=fgetc(fp);
putchar(c);
}while(c!=EOF);
fclose(fp);
return 0;
}
"read.c" 20L, 303C written
Output:
[it priya @localhost it priya]$ cc read.c
[it priya @localhost it priya]$ ./a.out
Enter the contents of file.....
hi friend
we are it guys
in DACE
0
File contents are
hi friendwe are it guys
in DACE
[it priya @localhost it priya]$
8/2/2019 Output Unix Program-C
12/38
Matrix MultiplicationSource Code:
#include
main()
{
int a[10][10],b[10][10],c[10][10],i,j,k;
int r1,r2,c1,c2;
printf("Enter the size of matrix A");
scanf("%d%d",&r1,&c1);
printf("\nEnter the size of matrix B:");
scanf("%d%d",&r2,&c2);
if(c1 != r2)
printf("\n Matrix multiplication is not possible");
else
{
printf("\nEnter the elements of Matrix A");
for(i=0;i
8/2/2019 Output Unix Program-C
13/38
}
printf("\nResultant matrix ");
"mult.c" 53L, 716C written
Output:
[it priya @localhost it priya]$ cc mult.c
[it priya @localhost it priya]$ ./a.out
Enter the size of matrix A 3 3
Enter the size of matrix B:3 3
Enter the elements of Matrix A
2 3 4
5 5 6
4 5 1
Enter the elements of matrix B
2 3 4
5 6 7
1 8 9
Resultant matrix
23 23 23
93 93 93
60 60 60
[it priya @localhost it priya]$
8/2/2019 Output Unix Program-C
14/38
Factorial Of A Given Number Using Recursion
Source Code:
#include
main()
{
int f,n;
printf("\n Enter the value for finding the factorial\n");
scanf("%d",&n);
f=fact(n);
printf("\n Factorial of %d is %d\n",n,f);
}
int fact (int a)
{
int n;
if(a==1 || a==0)
return 1;
else
n=a*fact(a-1);
return n;
}
Output:
[it priya @localhost it priya]$ cc fact.c
[it priya @localhost it priya]$ ./a.out
Enter the value for finding the factorial
5
Factorial of 5 is 120
[it priya @localhost it priya]$
8/2/2019 Output Unix Program-C
15/38
Dynamic Memory Allocation
Source Code:
#include
#include
main()
{
char *a;
int i;
a=(char *)calloc(5,1);
printf("\n Storage address of memory \n");
printf("\n Block Address: %u \n",a);
printf("\n Enter the string having 5 characters\n");
for(i=1;i
8/2/2019 Output Unix Program-C
16/38
SHELL
PROGRAM
8/2/2019 Output Unix Program-C
17/38
C
PROGRAM
8/2/2019 Output Unix Program-C
18/38
8/2/2019 Output Unix Program-C
19/38
8/2/2019 Output Unix Program-C
20/38
8/2/2019 Output Unix Program-C
21/38
11.REVERSE OF A NUMBER
echo "enter the number:"
read n
u=0
while test $n -ne 0
do
u=`expr $n % 10`
n=`expr $n / 10`
echo $u
done
8/2/2019 Output Unix Program-C
22/38
7.SQUARE OF SET OF NUMBERS
echo "enter the starting number"
read c
echo "enter the limit number"
read b
s=0
while test $c -le $b
do
s=`expr $c \* $c`
echo "$c * $c=$s"
c=`expr $c + 1`
done
8/2/2019 Output Unix Program-C
23/38
3.AREA & CIRCUMFERENCE OF CIRCLE
echo "enter the radius of the circle"
read r
area=`expr 3 \* $r \* $r`
circumference=`expr 2 \* 3 \* $r`
echo "area= $area"
echo "circumference= $circumference"
8/2/2019 Output Unix Program-C
24/38
2.BIGGEST OF THREE NUMBERS
echo "enter the three numbers"
read a
read b
read c
if test $a-gt $b -a $a -gt $c
then
echo " A is greater"
elif test $b -gt $c -a $b -gt $c
then
echo " B is greater"
else
echo "c is greater"
fi
8/2/2019 Output Unix Program-C
25/38
4.EVEN OR ODD
echo "enter the number"
read n
if test `expr $n % 2` -eq 0
then
echo "$n is even"
else
echo "$n is odd"
fi
8/2/2019 Output Unix Program-C
26/38
5.LEAP YEAR OR NOT
echo "enter the number"
read n
if test `expr $n % 4` -eq 0
then
echo " leap year"
else
echo "not a leap year"
fi
REVERSE OF NUMBERS
echo "enter the number"
read n
u=0
while test $n -ne 0
do
u=`expr $n % 10`
n=`expr $n / 10`
echo "$u"
done
8/2/2019 Output Unix Program-C
27/38
1.AREA OF TRIANGLE
echo "enter the base and height"
read b
read h
c=`expr $b \* $h`
area=`expr $c / 2`
echo "area of the triangle is $area"
8/2/2019 Output Unix Program-C
28/38
6.USAGE OF DATE & TIME
u=`date +%H`
i=`date +%T`
echo "$i"
if test $u -lt 12
then
echo "good morning"
elif test $u -gt 12 -a $u -lt 16
then
echo "good afternoon"
elif test $u -gt 16 -a $u -lt 18
then
echo "good evening"
else
echo "good night"
fi
8/2/2019 Output Unix Program-C
29/38
8.SWAPPING OF TWO NUMBERS
echo "enter two number"
read a
read b
echo "the value of $a and $b"
i=0
i=$a
a=$b
b=$i
echo "the swapped values of $a and $b"
8/2/2019 Output Unix Program-C
30/38
9.GENERATION OF NUMBERS USING WHILE LOOP
echo "enter the limit"
read n
i=0
while test $i -lt $n
do
i=`expr $i + 1`
echo "$i"
done
8/2/2019 Output Unix Program-C
31/38
10.FIBONACCI SERIES
echo "enter the number"
read n
echo "the result is"
c=0
a=0
b=1
echo "$a"
echo "$b"
n=`expr $n - 2`
while test $n -ne 0
do
c=`expr $a + $b`
echo "$c"
a=$b
b=$c
n=`expr $n - 1`
done
8/2/2019 Output Unix Program-C
32/38
8/2/2019 Output Unix Program-C
33/38
14.SUM OF SERIES
i=1
for i in 1 2 3 4 5
do
s=`expr $s + $i`
done
echo " THE SUM OF series IS $s"
8/2/2019 Output Unix Program-C
34/38
8/2/2019 Output Unix Program-C
35/38
15.SUM OF EVEN SERIES
i=1
for i in 2 1 4 5 3
do
s=`expr $i % 2`
if test $s -eq 0
then
sum=`expr $sum + $i`
fi
done
echo "sum of the series is $sum"
8/2/2019 Output Unix Program-C
36/38
17.GENERATION OF PAY BILL
echo "employee name"
read name
echo "employee id"
read a
echo "enter the basic"
read basic
echo "enter hra"
read hra
da=`expr $basic / 10`
pf=`expr $basic / 20`
gross=`expr $basic \+ $da \+ $hra`
net=`expr $gross - $pf`
echo "employee name:$name"
echo "employee id:$a"
echo "gross pay:$gross"
echo "net pay:$net"
8/2/2019 Output Unix Program-C
37/38
18.ARITHMETIC OPERATION USING CASE
echo "enter the first number"
read a
echo "enter the second number"
read b
echo "1.addition"
echo "2.subtraction"
echo "3.multiplication"
echo "4.division"
echo "enter the option"
read option
case $option in
1)echo "addition result =`expr $a \+ $b`";;
2)echo "subtraction result =`expr $a \- $b`";;
3)echo "multiplication result =`expr $a \* $b`";;
4)echo "division result =`expr $a \/ $b`";;
5)echo "invalid choice";;
esac
8/2/2019 Output Unix Program-C
38/38
13.FACTORIAL OF A GIVEN NUMBER
echo ENTER THE NUMBER:
read a
i=1
while test $agt 1
do
i=`expr $a \* $i`
a=`expr $a -1`
done
echo THE FACTORIAL OF THE NUMBER IS $i