Sei sulla pagina 1di 38

Employee Details

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]$

Swap two number using Pointers


Source Code: #include<stdio.h> 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.out before swapping : a=10 b=20 after swapping : a=20 b=10

Student Details Using pointers


Source Code: #include <stdio.h> 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",&regno); 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); }

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

Menu Driven Arithmetic Operations using Functions


Source Code: #include<stdio.h> 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;

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

Read And Write The Content Of Files


Source Code: #include<stdio.h> 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 friend we are it guys in DACE [it priya @localhost it priya]$

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]$

Factorial Of A Given Number Using Recursion


Source Code: #include<stdio.h> 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]$

Dynamic Memory Allocation


Source Code: #include<stdio.h> #include<memory.h> 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<=5;i++) { *a++=getchar(); printf("\n The character %d is %c \n",i,*(a-1)); printf("It is stored in the address %u\n",(a-1)); } }

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

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

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"

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

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

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

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

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"

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

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"

16.SUM OF ODD SERIES

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"

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"

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"

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

13.FACTORIAL OF A GIVEN NUMBER

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

Potrebbero piacerti anche