Académique Documents
Professionnel Documents
Culture Documents
Source Code: #include<stdio.h> 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]$
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
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);
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 56 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
Tower of Hanoi
Source Code: #include<stdio.h> 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 R Move disk 1 from C L Move disk 2 from C R Move disk 1 from L R
Memory Allocation
Source Code: #include<stdio.h> 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
Electricity Bill
Source Code: #include<stdio.h> 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:5656 Enter the total no of units:45 Name: ramu Bill no: 5656 Units used :45
Matrix Multiplication
Source Code: #include<stdio.h> 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<r1;i++) { for(j=0;j<c1;j++) { scanf("%d",&a[i][j]); } } printf("\n Enter the elements of matrix B"); for(i=0;i<r2;i++) { for(j=0;j<c2;j++) { scanf("%d",&b[i][j]); } } for(i=0;i<r1;i++) { for(j=0;j<c2;j++) { c[i][j]=0; for(k=0;k<c1;k++) { c[i][j]+=a[i][k]*b[k][i]; } }
} 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]$
Output: [it priya @localhost it priya]$ cc dynamic.c [it priya @localhost it priya]$ ./a.out Storage address of memory Block Address: 134518592 Enter the string having 5 characters priya The character 1 is p It is stored in the address 134518592 The character 2 is r It is stored in the address 134518593 The character 3 is i It is stored in the address 134518594 The character 4 is y It is stored in the address 134518595 The character 5 is a It is stored in the address 134518596 [it priya @localhost it priya]$
SHELL PROGRAM
C PROGRAM
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
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
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"
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
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
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
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"
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
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"
echo "enter the limit" read n i=0 while test $i -lt $n do i=`expr $i + 1` echo "$i" done
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
12.ARMSTRONG NUMBER
echo "enter the number" read n i=0 k=`expr $n` while test $n -ne 0 do u=`expr $n % 10` i=`expr $i + $u \* $u \* $u` n=`expr $n / 10` done if test $k -eq $i then echo "$k is an Armstrong" else echo "$k is not an Armstrong"
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"
i=0 for i in 2 1 4 5 3 do s=`expr $i % 2` if test $s -ne 0 then sum=`expr $sum + $i` fi done echo "sum of the series is $sum"
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"
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"
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
echo ENTER THE NUMBER: read a i=1 while test $a gt 1 do i=`expr $a \* $i` a=`expr $a -1` done echo THE FACTORIAL OF THE NUMBER IS $i