You are on page 1of 54

UNIVERSAL ENGINEERING COLLEGE

Vallivattom(P.O), Thrissur-680123.

EN109(P) Computer Programming in C Lab Manual

EX. No. : 1(A) AIM:

STUDY OF MS-DOS COMMANDS

To study the simple OS interface MS DOS commands. DIRECTORY RELATED COMMANDS: 1. DIR: This command is used to display the files and directories of the current drive and directory. Syntax: DIR [drive:][path][filename] DIR/P: To display files and directories page wise. DIR/W: To display files and directories width wise ( horizontally ). DIR/AD: To display directories only. DIR/AH: To display hidden files only. DIR/AR: To display read only files. DIR/ON: To display files in sorted order name wise. DIR/OS: To display files in sorted order size wise. EXAMPLE: C:\> DIR 2. MD [MAKE DIRECTORY]: This command is used to create a new folder or directory. Syntax: MD [drive:] [path] Where drive is the floppy \ hard disk drive on which you want to create a new directory. EXAMPLE: C:\> MD DRSJS C:\> 3. CD [CHANGE DIRECTORY]: This command is used to change current directory to another directory. Syntax: CD [drive:][path]

CD.. : This command is used to go to previous directory. CD\ : This command is used to root directory. EXAMPLE: C:\> CD DRSJS C:\DRSJS> C:\> CD.. C:\> 4. RD [REMOVE DIRECTORY]: This command is used to remove the directory. SYNTAX: RD [directory] Where directory is the name of the directory which we want to remove. EXAMPLE: C:\> RD DRSJS C:\> FILE RELATED COMMANDS: 1. COPY CON SYNTAX EXAMPLE:C:\> copy con college Sri Manakula Vinayagar Engineering College. (Approved by AICTE New Delhi & Affiliated to Pondicherry University) Ctrl Z or F6 to save the file. 2. TYPE: This command is used to display the contents of the file. SYNTAX: TYPE [File Name] Example: C:\> type address : : COPY CON [File Name] This command is used to create a new file.

3. REN: This command is used to change the file name. SYNTAX: Example: C:\>REN circle area 4. DEL: This command is used to delete the file. SYNTAX: Example:C:\>DEL area DEL [File Name] REN [Old File Name] [New Name]

RESULT: Thus the MS DOS directory related and file related commands have been studied and executed.

EX. No. : 1(B)

STUDY OF TURBO C

AIM: To study the TURBO C IDE, compile and execute a simple C program. DESCRIPTION: The TURBO C development environment offers the following main menu commands. File Edit Search Run Compile Debug Options Windows Help

At the main menu, either press an arrow key to move to the desired option and then press enter key or press the highlighted capital letters of the option. You can also press alt key and the first letter of the option, which you desire to open. FILE (ALT-F): This menu offers choices for loading existing files, creating new files and saving files. This menu contains New, Open, Save, Save as, Save all, Change dir, Print, Dos Shell and Quit. File ->New: This option is used to create a new program. File ->Open (F3): This option is used to open an existing files. File ->Save (F2): This option is used to save the program.

File ->Save as: This option is used to save the program in another name. File ->Change dir: This option is used to change the directory of existing folder to another directory. File ->Print: This option is used to print the existing programs. File ->Dos shell: This option is used to come out from the c editor to dos shell. To return to Turbo C, type exit and press enter and the DOS prompt. File ->Quit: This option is used to exit from C editor. EDIT: (ALT E): This command takes you to the edit window. In this menu, it is used to modify the programs. In this menu we can copy, cut and paste the programs. SEARCH (ALT S): This menu lets you to find particular part of the program, replace the particular part of program and move to particular line number. RUN (ALT-R): This menu lets you to compile, link and run our program and start and debugging sessions. RUN->RUN (CTRL+F9): This option is used to compile, link and run our program. If we compile and link with the option debug source debugging on, your program will run in debug mode. This command will stop execution at the first break point or it will run the program all the way through. After compilation, to view the result press Alt-F5. COMPILE (ALT-F9): This menu allows to invoke compile to obj, Make Exe file, Link Exe file, etc., Alt-F9 is used to compile and create an object file. F9 command is used to build a execution file. DEBUG: This menus options let you look at and change variable values, go to the declaration of any function in the source code and look at the call stack. Break/Watch option is used to insert, remove and go breakpoint in your program and set delete and edit watch expressions. PROJECT (ALT-P): This menu is used to define and manage project.

OPTIONS (ALT-O): This menu allows configuring the Turbo C Editor. WINDOWS (ALT-W): This menu is used to Cascade all files, move programs and close all programs. STEPS FOR PROGRAM WRITING: 1. Invoke Turbo C editor using StartRunc:tc\bin\tc 2. Type the source program. 3. Save the program using F2 command. (filename.c) 4. Compile and run the program using Alt F9. 5. To view the result use Alt F5. SAMPLE 1: Write a program to print simple interest. /* program to find simple interest */ #include<stdio.h> void main() { float p,n,r,si; clrscr(); printf(Enter principle, Number of years and Rate of Interest :\n); scanf(%f%f%f,&p,&n,&r); si=(p*n*r/100); printf(The Simple Interest is : %2.2f: ,si); getch(); } OUTPUT:Enter principle, Number of years and Rate of Interest: 5000 5 10 The Simple Interest is: 2500.00

SAMPLE 2: Write a program to generate Sales slip. /************** SALES SLIP ***************/ #include <stdio.h> int main() { float purchase, tax_amt, total, rate=0.125; clrscr(); printf("\n Amount of purchase: "); scanf("%f",&purchase); tax_amt = purchase * rate; total = purchase + tax_amt; printf("\n Purchase is : %0.2f ",purchase); printf("\n Tax : %0.2f",tax_amt); printf("\n Total : %0.2f",total); getch(); } OUTPUT 1: Amount of purchase: 500 Purchase is: 500.00 Tax : 62.50 Total : 562.50 OUTPUT 2: Amount of purchase: 1773 Purchase is: 1773.00 Tax : 221.62 Total : 1994.62

RESULT:

Thus the options in the TURBO C IDE have been studied and a simple C program is compiled and executed. EX. No. : 2 AIM: To create Student Progress Report as the main document and create 45 reports for 45 students. Use mail merge to create reports for 42 students among the 45. PROCEDURE: 1. Open your letter in Word and then bring up the Mail Merge Sidebar Wizard by going to Tools > Letters and Mailings > Mail Merge. 2. The sidebar will appear on the right side of the window, next to your document. 3. Check the letter option from the document type and click Next below the wizard. 4. Leave the Use the current document checked and click on Next: Select recipients to move on. 5. This step is used to select the data source for the recipient list. Check the Type a new list option to create a new data source list or leave the checked option Use existing list and then click Next. A Select Data Source dialog box will appear. Choose the data source from the existing list and then click open. 6. Mail merge recipient dialog box will appear choose the recipients, you want to send letter from the list and then click ok. 7. The next part is inserting recipient information from the data source into the Word document. 8. To do this, first select the place in the document where you want to insert a field from the data source and click on More items. A dialog box will pop-up, giving you a list of all of the fields that are possible to insert. 9. Select the field that you want to insert and click Insert. It will replace the selected section of text with field_name. MS-WORD MAIL MERGE

10. Once you have entered all of your merged fields into the correct places and checked the formatting around them, click on Next: Preview your letters. 11. You can preview your letters to make sure that everything will look correct once the records from the data source are merged. You can check different recipients letters by using the arrows, look for a specific record by searching the merge fields, or even exclude one from the merge. 12. Once your merge is set up correctly you can complete it by clicking next complete the merge. 13. Next you can choose to either print the letters or view the merge as individual letters. Either option will bring up the dialog box. You can choose to merge all of the records in the data source, just the Current record that you are displaying in the preview, or a certain section of continuous records. 14. Selecting Edit individual letters will open up a new Word document (usually called Letters 1). In this document, each page will contain the form letter with one of the records merged into it. 15. You can then edit individual letters if you want to. For example if you want to add a message to a specific recipient you can do it in this step. 16. Thus the document is merged successfully with the selected recipient.

10

EX. No. : 3 AIM:

EXCEL CHARTS

To try with different types of graphs (bar, XY, Pie) for the following data, Test results of a student in semester I Test Marks T1 100 T2 100 PT1 54 T3 95 T4 88 PT2 98

PROCEDURE: 1. Enter the data in the worksheet as shown in question 2. Choose Chart option from the Insert menu, or choose the Chart Wizard button from the standard toolbar 3. Shows the chart wizard Step1 of 4 Chart Type Dialog Box. 4. Choose the appropriate chart type from the chart type list box then click next. 5. Shows the chart wizard step 2 of 4 Chart Source Data Dialog Box then click Next. 6. Shows the chart wizard step 3 of 4 Chart Options Dialog Box, here you can give the Chart Titles i.e., X and Y-Axis Titles etc, and then click Next. 7. Shows the chart wizard step 4 of 4 Chart Location Dialog Box then click Finish. 8. Now the chart is displayed in your active worksheet, as shown below.

11

SNAPSHOTS

Figure 1: XY chart

Figure 2: Bar chart

12

Figure 3: Pie chart

13

EX. No. : 4 AIM:

GREATEST OF THREE NUMBERS

To write a C program to find the largest of three numbers using conditional and IF statement. ALGORITHM 1. Start 2. Read three numbers A, B, C 3. Compare whether A is greater than B and C using conditional operator 4. If it is equal then print A is greater 5. If step 3 is false check whether B is greater than A and C using conditional operator. 6. If it is true print B is greater 7. If step 5 is false print C is greater 8. Stop

PROGRAM /***** GREATEST OF THREE NUMBERS USING IF AND CONDITIONAL OPERATOR *****/ #include<stdio.h> #include<conio.h> void main() { clrscr(); int a,b,c; printf("enter 3 numbers"); scanf("%d %d %d",&a,&b,&c); printf("\n Using conditional"); printf("\n");

14

(a>b&&a>c)?printf("A is larger"):(b>a&&b>c)?printf("B is larger"):printf("C is larger"); printf("\n Using IF \n"); if(a>b && a>c) printf("A is largest"); else if(b>a&&b>c) printf("B is largest"); else printf("C is largest"); getch(); }

OUTPUT Enter 3 numbers 2 5 6 Using conditional C is larger Using IF C is largest

RESULT Thus the largest of three numbers using conditional and if statement was successfully executed.

15

EX. No. : 5 AIM

SWAPPING OF TWO NUMBERS

To write a C program to swap two numbers using temporary variable and without using temporary variable. ALGORITHM 1. Start 2. Read two numbers A, B 3. Copy A to TEMP 4. Copy B to A 5. Copy TEMP to B 6. Print A, B 7. Calculate A=A+B, B=A-B, A=A-B 8. Display A, B 9. Stop PROGRAM /***************** SWAPPING OF TWO NUMBERS *****************/ #include<stdio.h> #include<conio.h> void main() { clrscr(); int a,b,temp; printf("Enter Two numbers"); scanf("%d %d",&a,&b); printf("\nSwapping Using Temporary Variable\n"); printf("\n Before Swap A=%d B=%d",a,b); temp=a; a=b; 16

b=temp; printf("\nAfter Swap A=%d B=%d",a,b); printf("\nSwapping Without Using Temporary Variable\n"); printf("\n Before Swap A=%d B=%d",a,b); a=a+b; b=a-b; a=a-b; printf("\nAfter Swap A=%d B=%d",a,b); getch(); }

OUTPUT Enter Two numbers 3 2 Swapping Using Temporary Variable Before Swap A=3 B=2 After Swap A=2 B=3 Swapping Without Using Temporary Variable Before Swap A=2 B=3 After Swap A=3 B=2

RESULT Thus swapping of two numbers using temporary variable and without using temporary variable was done and successfully executed.

17

EX. No. : 6 AIM:

QUADRATIC EQUATION

To write a c program to solve Quadratic Equation for various possible inputs. ALGORITHM: 1. Start the program. 2. Get the values of A,B,C for a quadratic equation of the form ax2+bx+c=0. 3. Compute discriminate value d as D=B2-4AC. 4. Check for D is D=0 then go to step 5, otherwise go to step 8. 5. Compute the value of the roots (i.e) E = -B/2A. 6. Display that the roots are Real and Unequal. 7. Check for D, if D>0 then go to step 9, otherwise go to 1. 8. Display that The roots are real and Unequal. 9. Compute the value of the roots ( i.e ) E=-B+sqrt(D)/2A and F=-B-sqrt(D)/2A. 10. Display the value of the roots and move to 5. 11. Check for D, if D<0 then go to 2, otherwise go to 5. 12. Compute the value of the roots (i.e) E=-B/2A and F=-B-sqrt(D)/2A. 13. Display the values of the roots and go to 5. 14. Stop. PROGRAM: /* * * PROGRAM TO FIND THE ROOTS OF A QUADRATIC EQUATION * * * / #include<stdio.h> #include<conio.h> #include<math.h> void main() { int a,b,c,d;

18

float r1,r2,e,f; clrscr(); printf("\nEnter the co-efficient of x^2:\t"); scanf("%d",&a); printf("\nEnter the co-efficient of x:\t"); scanf("%d",&b); printf("\nEnter the constant value:\t"); scanf("%d",&c); printf("\n\nSOLUTION:\n***********"); d=(b*b)-(4*a*c); if(d==0) { r1=-((float)b/(float)(2*a)); printf("\n The roots are real and equal"); printf("\n The roots are:\n\t root1=root2=%f",r1); } else { if(d>0) { r1=(-b+sqrt(abs(d)))/(2*a); r2=(-b-sqrt(abs(d)))/(2*a); printf("\nThe roots are real and unequal"); printf("\nThe roots are:\n\t root1=%f\t root2=%f",r1,r2); } else { e=(float)-b/(float)(2*a); f=(sqrt(abs(d)))/(2*a); printf("\nThe roots are imaginary"); printf("\n\nThe roots are:\n root1=%.2f+i%.2f",e,f);

19

printf("\troot2=%7.2f-i%.2f",e,f); } } getch(); } OUTPUT 1: Enter the co-efficient of x^2 : 2 Enter the co-efficient of x Enter the constant value SOLUTION: The roots are real and equal The roots are root1 = root2 = -1.00 OUTPUT 2: Enter the co-efficient of x^2 : 2 Enter the co-efficient of x Enter the constant value SOLUTION: The roots are imaginary The roots are root1 = -0.25 + i1.39 OUTPUT 3: Enter the co-efficient of x^2 : 1 Enter the co-efficient of x Enter the constant value :4 :2 root2 = -0.25 + i1.39 :1 :4 :4 :2

20

SOLUTION: The roots are real and unequal The roots are root1 = -0.59 root2 = -3.41

RESULT: Thus a C program to solve the quadratic equation was compiled and executed successfully.

21

EX. No. : 7 AIM

COUNTING NUMBER OF VOWELS AND DIGITS IN A STRING

To write a C program to count the number of vowels and digits in a given string using switch case statement. ALGORITHM 1. Start 2. Read a string CH 3. Loop until CH is equal to new line 4. Check whether the entered character is vowel or a digit using switch case 5. If it is vowel increment VOWEL 6. If it is digit increment DIGIT 7. End loop 8. Print VOWEL, DIGIT 9. Stop PROGRAM /**** COUNTING NUMBER OF VOWELS AND DIGITS IN A STRING USING SWITCH CASE #include<stdio.h> #include<conio.h> void main() { clrscr(); char ch; int digits=0,vowels=0; printf("Enter a string with digits\n"); while((ch=getchar())!='\n') { ****/

22

switch(ch) { case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': digits++; break; case 'a':case 'A': case 'e':case 'E': case 'i':case 'I': case 'o':case 'O': case 'u':case 'U': vowels++; break; } } printf("\n No. of vowels in the given string %d",vowels); printf("\n No. of digits in the given string %d",digits); getch(); }

23

OUTPUT Enter a string with digits COMPTER PROGRAMMING 123 No. of vowels in the given string 5 No. of digits in the given string 3

RESULT Thus a C program to count the number of vowels and digits using switch case was compiled and executed successfully.

24

EX. No. : 8(A) AIM:

GENERATION OF PRIME NUMBERS

To write a C program to generate all prime numbers in a given range. ALGORITHM: 1: Start the program. 2: Get the range of prime numbers N 3: Check if N= =1 if equal go to step 4 4: Print number is not valid to generate prime. 5: By using for loop check (i<=N). 6: Assign c=0 check the process j<=i using for loop. 5: Check for the condition (i%j==0) then c=c+1. 6: Check for the condition whether c=2, then display the value. 7: Stop PROGRAM: /* * * * * * * * * PRIME NUMBERS * * * * * * * * * * */ #include<stdio.h> #include<conio.h> void main() { clrscr(); int n,p,i,j,c; printf("Enter the limit"); scanf("%d",&n); printf("\n The prime numbers are"); for (i=1; i<=n; i++) { c=0;

25

for(j=1;j<=n;j++) { if((i%j)==0) { c=c+1; } } if(c= =2) { printf("\n%d",i); } } getch(); } OUTPUT : Enter the limit: 10 The prime numbers are: 2 3 5 7

RESULT: Thus the C program to generate prime numbers in the given range was compiled and executed successfully.

26

EX. No. : 8 (B) AIM:

FIBONACCI SERIES

To write a c program to generate the Fibonacci series. ALGORITHM: 1. Start 2. Initialize FIB1 = 0, FIB2 = 1, FIB3, NUMBERS = 2, COUNTER = 2 3. Read NUMBERS 4. If NUMBERS<3 stop the program 5. Print FIB2 6. Loop until COUNTER<=NUMBERS 7. Increment COUNTER by 1 8. FIB3=FIB1+FIB2 9. Print FIB3 10. Copy FIB2 to FIB1 11. Copy FIB3 to FIB2 12. End loop 13. Stop PROGRAM: /*************FIBONACCI SERIES ***************/ #include <stdio.h> #include<conio.h> #include<process.h> void main() { clrscr();

27

int fib1 = 0,fib2 = 1,fib3,numbers = 2,counter = 2; printf("How many Fibonacci number you need ? : "); scanf("%d",&numbers); if (numbers < 3) exit(0); printf("%d ",fib2); do { counter++; fib3 = fib1 + fib2; printf("\n%d",fib3); fib1 = fib2; fib2 = fib3; } while (counter <= numbers); getch(); } OUTPUT: How many Fibonacci number you need? : 5 1 1 2 3 5

RESULT Thus the Fibonacci series for a given range was compiled and executed successfully.

28

EX. No. : 9 AIM

COSINE SERIES USING LOOP

To write a C program to find the cosine series for a given range and an angle. ALGORITHM 1. Start 2. Read ANGLE, N 3. Calculate X=ANGLE*3.14/180 4. Initialize TERM=1, SUM=1, Y=1 5. Loop i=1 to n 6. Y= Y*(2*i*(2*i-1)); 7. TERM=(-TERM*X*X)/Y) 8. Increment SUM to TERM 9. End loop 10. Print ANGLE, X, SUM 11. Stop

PROGRAM /************** COSINE SERIES USING FOR LOOP *************/ #include<stdio.h> #include<conio.h> void main() { int i,n,y=1; float x,angle,term,sum; clrscr(); printf("Enter the angle and the number of terms\n"); scanf("%f %d",&angle,&n); x=angle*3.141592/180; term=1; sum=1; for(i=1;i<n;i++) {

29

y=y*(2*i*(2*i-1)); term=(-term*x*x)/y; sum+=term; } printf("Entered angle = %.4f\n",angle); printf("Its radian value =%.3f\n",x); printf("Value of Cosine series= %.3f\n",sum); getch(); }

OUTPUT Enter the angle and the number of terms 45 5 Entered angle = 45.00 Its radian value = 0.785 Value of Cosine series = 0.707

RESULT Thus the cosine series for the given range and angle was compiled and executed successfully.

30

EX. No. : 10 AIM:

MATRIX OPERATIONS

To write a C program to perform the matrix operation (Addition, Transpose and Multiplication). ALGORITHM 1. Start 2. Read the order of matrix M,N 3. Read the value of two matrices A,B 4. Set loop i=0 to m and j=0 to N 5. Calculate c[i][j]=a[i][j]+b[i][j] 6. End loop 7. Print c[i][j] 8. Initialize c[i][j]=0 9. Set loop i=0 to M j=0 to N and k=0 to N 10. Calculate c[i][j]=c[i][j]+a[i][k]*b[k][j] 11. End loop 12. Print c[i][j] 13. Set loop i=0 to M and j=0 to N 14. Assign t[i][j]=a[j][i] 15. End loop 16. Print t[i][j] 17. Stop

31

PROGRAM
/*** MATRIX OPERATIONS - ADDITION, TRANSPOSE & MULTIPLICATION ***/

#include<stdio.h> #include<conio.h> void main() { int n,i,j,a[3][3],b[3][3],c[3][3],m,k; clrscr(); printf("Enter the order of two Matrices"); scanf("%d %d",&m,&n); printf("\n Enter the Matrix A"); for(i=0;i<m;i++) { for(j=0;j<n;j++) { scanf("%d",&a[i][j]); } printf("\n"); } printf("\n Enter the Matrix B"); for(i=0;i<m;i++) { for(j=0;j<n;j++) { scanf("%d",&b[i][j]); } printf("\n"); } printf("Matrix addition"); for(i=0;i<m;i++) { for(j=0;j<n;j++) { c[i][j]=a[i][j]+b[i][j]; } printf("\n"); } for(i=0;i<m;i++) { 32

for(j=0;j<n;j++) { printf("\t%d",c[i][j]); } printf("\n"); } printf("Matrix Multiplication\n"); for(i=0;i<m;i++) { for(j=0;j<n;j++) { c[i][j]=0; for(k=0;k<n;k++) { c[i][j]=c[i][j]+a[i][k]*b[k][j]; } } } for(i=0;i<m;i++) { for(j=0;j<n;j++) { printf("\t%d",c[i][j]); } printf("\n"); } printf("Transpose of matrix A\n"); for(i=0;i<m;i++) { for(j=0;j<n;j++) { printf("\t%d",a[j][i]); } printf("\n"); } getch(); }

33

OUTPUT Enter the order of two Matrices 3 3 Enter the Matrix A 414 444 414 Enter the Matrix B 222 222 222 Matrix addition 636 666 636 Matrix Multiplication 18 18 18 24 24 24 18 18 18 Transpose of matrix A 444 141 444

RESULT Thus the C program to perform the matrix operations was compiled and successfully executed.

34

EX. No. : 11 AIM

SINE SERIES USING RECURSIVE FUNCTION

To write a C program to perform the sine series using recursive function. ALGORITHM 1. Start 2. Read X,N 3. Calculate X=3.14/180 4. Assign SUM=X 5. Cal and print function sine(N,X) 6. Stop Function sine(n,x) 1. Initialize i=1, Y=1 as static 2. If N==1 return sum 3. If N>0 4. Y= Y*(2*i)*(2*i+1); 5. Calculate TERM=-TERM*X*X/Y 6. Increment I to 1 7. SUM=SUM+TERM 8. TERM=sine(--n,x) 9. return SUM

35

PROGRAM /************** SINE SERIES USING RECURSIVE FUNCTION *************/ #include<stdio.h> #include<conio.h> #include<stdlib.h> #define PI 3.14285 float sum; void main() { int n; float x; double sine(int,float); clrscr(); printf("\n Enter the angle: "); scanf("%f",&x); x*=(PI/180.0); printf("Enter the no. of terms to be summed:"); scanf("%d",&n); sum=x; printf("Sum of the series :%.4f\n",sine(n,x)); getch(); } double sine (int n, float x) { static float term; static float i=1; static int y=1; if(n==1) return sum; if(i==1) term=x; if(n>0) { y=y*(2*i)*(2*i+1); term=-term*x*x/y; i++; sum+=term; term=sine (--n,x); } if(n<0) { printf("ERROR:Give positive value for n\n"); exit(0);

36

} return sum; } OUTPUT Enter the angle: 30 Enter the no. of terms to be summed: 5 Sum of the series: 0.4999

RESULT Thus the C program to perform the sine series using recursive function was compiled and executed successfully.

37

EX. No. : 12 REMOVING DUPLICATE STRING FROM A GIVEN SENTENCE AIM: To write a C program to remove the duplicate string from the given sentence. ALGORITHM 1. Start 2. Read the sentence in bin variable from the user 3. Separate the words and store it in temp variable which is two dimension array 4. Compare the consecutive words in temp upto the word count 5. If it is not equal print the word else do not print that word 6. Repeat 4 and 5 until all the words in temp are compared 7. Stop

PROGRAM /**** REMOVE DUPLICATE STRING FROM A GIVEN SENTENCE ****/ #include <stdio.h> #include <string.h> #include <ctype.h> #include <conio.h> void main(void) { char temp[10][20]; char bin[100]; int index=0; int word=0,len=0; int loop,chain; clrscr(); printf("\nEnter the sentence: "); gets(bin); while(bin[index]!=NULL) { if(isalpha(bin[index]) || isdigit(bin[index])) { temp[word][len]=bin[index]; len++;

38

} else if(isalpha(bin[index-1]) || isdigit(bin[index-1])) { temp[word][len]=NULL; word++;len=0; } index++; } temp[word][len]=bin[index]; word++; printf("\nThe output data is: "); for(index=0;index<word;index++) if(strcmp(temp[index],temp[index+1])!=0) printf("%s ",temp[index]); getch(); }

OUTPUT Enter the sentence: Hi Hi have a nice nice day The output data is: Hi have a nice day

RESULT Thus a C program to remove the duplicate string from the given sentence was compiled and executed successfully.

39

EX. No. : 13 AIM

ARRAY OF STRUCTURES

To write a C program to create an array of structures for a list of items with the following details Item_Code 102 102 102 101 101 101 101 Item_Name Paste-Colgate Paste-Pepsodent Paste-Close-up Soap-Cinthol Soap-Lux Soap-Hammam Soap-Dove

Arrange the set of items in ascending order of its Item_Code and descending order of its Item_Name as given below Item_Code 101 101 101 101 102 102 102 Item_Name Soap-Lux Soap-Hamam Soap-Dove Soap-Cinthol Paste-Pepsodent Paste-Colgate Paste-Close-up

40

ALGORITHM 1. Start 2. Define structure item with the members Item_code : integer Item_name : character end item 3. Read the given details in item_code and item_name 4. Set loop i=0 to 6 and j=1 to 6 5. Check if a[i].item_code<a[j].item_code then assign 6. Temp=a[i].item_code 7. A[i].item_code=a[j].item_code 8. A[j].item_code=temp 9. End loop i,j 10. Set loop i=0 to 6 and j=1 to 6 11. Check if a[i].item_name is less then a[j].item_code 12. Then assign a[i].item_name to temp 13. Assign a[j].item_name to a[i].item_name 14. Assign temp to a[j].item_name 15. End loop 16. Print item_code and item_name using loop 17. Stop

41

PROGRAM /******************* ARRAY OF STRUCTURES ******************/ #include<stdio.h> #include<conio.h> #include<string.h> void main() { clrscr(); int i,j,temp; char temp1[30]; struct item { int item_code; char item_name[30]; }a[7]; printf("Enter the item code and item name"); for(i=0;i<7;i++) { scanf("%d %s",&a[i].item_code,&a[i].item_name); } for(i=0;i<7;i++) { for(j=i+1;j<7;j++) { if(a[i].item_code>a[j].item_code) { temp=a[i].item_code; a[i].item_code=a[j].item_code; a[j].item_code=temp; } } } for(i=0;i<7;i++) { for(j=i+1;j<7;j++) { if(strcmp(a[i].item_name,a[j].item_name)<0) { strcpy(temp1,a[i].item_name); strcpy(a[i].item_name,a[j].item_name); strcpy(a[j].item_name,temp1); } }

42

} printf("Item_Code \t\t\t Item_Name"); for(i=0;i<7;i++) { printf("\n"); printf("%d \t\t %s",a[i].item_code,a[i].item_name); } getch(); }

OUTPUT Enter the item code and item name 102 Paste-Colgate 102 Paste-Pepsodent 102 Paste-Close-up 101 Soap-Cinthol 101 Soap-Lux 101 Soap-Hamam 101 Soap-Dove Item_Code 101 101 101 101 102 102 102 Item_Name Soap-Lux Soap-Hamam Soap-Dove Soap-Cinthol Paste-Pepsodent Paste-Colgate Paste-Close-up

RESULT Thus the list of items using array of structures was successfully compiled and executed.

43

EX. No. : 14 AIM

USER-DEFINED DATA TYPE USING STRUCTURE

To write a C program to create a user defined data type using structure to accept a list of N numbers of students with their names and roll numbers and arrange them in an alphabetical order and write the details in the file. ALGORITHM 1. Start 2. Create a user-defined structure tag with the members name: character rno: integer 3. Open the file details .txt in writing mode 4. Read no for the number of student details 5. Read all the student details in name, rno 6. Before sorting print the student details and same time write it in the file details.txt 7. Using function sort(), sort the names upto no count 8. After sorting print the student details and same time write it in the file 9. Stop PROGRAM /********* USER-DEFINED DATA TYPE USING STRUCTURE ********/ #include<stdio.h> #include<string.h> #include<conio.h> struct tag { char name[10]; int rno; }; typedef struct tag node; node s[10],t[10]; void main() { FILE *fp; int no,i; 44

fp=fopen("details.txt","w"); clrscr(); fflush(stdin); printf("\n Enter the Number of students:"); scanf("%d",&no); for(i=0;i<no;i++) { printf("\n Enter the name of student %d=",i+1); fflush(stdin); gets(s[i].name); printf("\n Enter the roll number="); scanf("%d",&s[i].rno); t[i]=s[i]; } printf("\n Before Sorting...\n"); fprintf(fp," Before Sorting...\n"); fprintf(fp,"\nRoll_No Student_Name\n"); for(i=0;i<no;i++) { printf("%3d %10s\n",t[i].rno,t[i].name); fprintf(fp,"%d \t %s\n",t[i].rno,t[i].name); } printf(" After Sorting...\n"); fprintf(fp,"\n\n After Sorting...\n"); fprintf(fp,"\nRoll_No Student_Name\n"); sort(no); for(i=0;i<no;i++) { printf("%3d %10s\n",s[i].rno,s[i].name); fprintf(fp,"%d \t %s\n", s[i].rno,s[i].name); } printf("\nThe data are written successfully to the file"); fclose(fp); getch(); } sort(int no) { int i,j; node temp; for(i=0;i<no;i++) { for(j=i+1;j<no;j++) { if(strcmp(s[i].name,s[j].name) > 0)

45

{ temp=s[i]; s[i]=s[j]; s[j]=temp; } } } }

OUTPUT Enter the Number of students: 2 Enter the name of student 1=Sundar Enter the roll number=30 Enter the name of student 3=Reshma Enter the roll number=25 Before Sorting... 30 25 25 30 Sundar Reshma Reshma Sundar

After Sorting...

The data are written successfully to the file

RESULT Thus the C program to accept a list of N numbers of students with their names and roll numbers are arranged in an alphabetical order and successfully written to the file using user defined data type using structure.

46

EX. No. : 15 AIM

SORTING OF NAMES USING ARRAY OF POINTERS

To write a C program to sort the given names using array of pointers. ALGORITHM 1. Start 2. Read n for number of names to be sorted 3. Read the names 4. Set loop i=0 to n and j=1 to n 5. Compare the first name with second name if is greater than 0 6. Interchange the first name and the second name using third variable 7. Repeat until all the names are sorted 8. Print the sorted names 9. Stop PROGRAM /****** SORTING OF NAMES USING ARRAY OF POINTERS ******/ #include<stdio.h> #include<conio.h> #include<string.h> #include<stdlib.h> #define MAX 10 void main() { clrscr(); char *names[MAX],temp[15],*temp1; int n,i,j,len; printf("Enter the no. of names to be sorted"); scanf("%d",&n); *names=NULL; fflush(stdin); printf("enter the names one by one\n"); for(i=0;i<n;i++) { gets(temp); len=strlen(temp); *(names+i)=(char*)malloc(len+1); 47

strcpy(*(names+i),temp); } for(i=0;i<n;i++) for(j=i+1;j<n;j++) { if(strcmp(*(names+i),*(names+j))>0) { temp1=*(names+i); *(names+i)=*(names+j); *(names+j)=temp1; } } printf("\n the sorted list of names:\n"); for(i=0;i<n;i++) printf("%s\n",*(names+i)); getch(); } OUTPUT Enter the no. of names to be sorted: 5 Enter the names one by one: Selva Deebiga Murugan Marie Lakshmi The sorted list of names: Deebiga Lakshmi Marie Murugan Selva

RESULT Thus the C program to sort the given names using array of pointers was compiled and executed successfully.

48

EX. No. : 16 AIM

FUNCTIONS WITH STATIC DATA TYPE

To write a C program using functions with static data type. ALGORITHM 1. Start 2. Call function f1() 3. Print after first call 4. Call function f1() 5. Print after second call 6. Call function f1() 7. Print after third call 8. Stop Function f1() 1. Initialize k= 0 as static and j=10 2. Print the value of k and j 3. Calculate k=k+10 PROGRAM /********* FUNCTIONS WITH STATIC DATA TYPE*********/ #include <stdio.h> int g = 10; main() { void f1(); clrscr(); f1(); printf(" after first call \n"); f1(); printf(" after second call \n"); f1(); printf(" after third call \n"); getch(); }

49

void f1() { static int k=0; int j = 10; printf("\nValue of k = %d and j = %d",k,j); k=k+10; }

OUTPUT Value of k = 0 and j = 10 after first call Value of k = 10 and j = 10 after second call Value of k = 20 and j = 10 after third call

RESULT Thus the C program using functions with static data type was compiled and executed successfully.

50

EX. No. : 17(A) AIM

IMPLEMENTING DEL COMMANDS USING FILE

To write a C program to implement the DEL command using file. ALGORITHM 1. Start 2. Pass the command line argument to the parameter argc and argv in main function 3. If argc is not equal to 2 4. Print parameter missing 5. Remove the entered file using remove function 6. If it is removed correctly print removed filename 7. Else print error 8. Stop PROGRAM /********************* DEL COMMAND USING FILE *******************/ #include <stdio.h> #include<conio.h> int main(int argc,char *argv[]) { if(argc!=2) printf("Parameter missing"); if (remove(argv[1]) == 0) printf("Removed %s \n",argv[1]); else perror(" ERROR"); return 0; }

51

OUTPUT C:\TURBOC2> DELFILE details.txt Removed details.txt

RESULT Thus the C program to implement the DEL command using file was compiled and executed successfully.

52

EX. No. : 17(B) AIM

COPY COMMAND USING FILE

To write a C program to implement the COPY command using file. ALGORITHM 1. Start 2. Pass the command line argument to the parameter argc and argv in main function 3. Check if argc not equal to 3 4. If true print parameter missing 5. Open the first file in reading mode and second file in append mode 6. Set loop to read the characters from the first file 7. Copy the character to second file 8. Repeat until EOF is reached 9. Print file is successfully copied 10. Stop

PROGRAM /***************** COPY COMMAND USING FILE *****************/ #include<stdio.h> #include<conio.h> void main(int argc,char *argv[]) { FILE *fp1,*fp2; int i,ch; clrscr(); if(argc!=3) { printf("Program parameter missing"); exit(0); } fp1=fopen(argv[1],"r"); fp2=fopen(argv[2],"a");

53

while((ch=getc(fp1))!=EOF) putc(ch,fp2); printf("file %s is successfully copied",argv[1]); getch(); }

OUTPUT C:\TURBOC2> CREATE result.txt update.txt file result.txt is successfully copied

RESULT Thus the C program to implement the COPY command using file was compiled and executed successfully.

54

You might also like