C Programming Laboratory [13MCA16] Laboratory Manual ~ Ganesh Hegde /hegde.ganesh7 /GaneshSirsi [email protected] 2 C Programming Laboratory [13MCA16] C Programming Laboratory Sub. Code : 13MCA16 Hrs/Week : 4 Total Hours : 42 100 IA Marks : 50 Exam Hours : 03 Exam Marks : 1. a. Convert degrees into Fahrenheit and vice versa b. Calculate the salary of an employee given his basic pay, HRA = 10% of basic pay, TA=5% of his basic pay and deductions IT = 2.5% of his basic pay 2. a. Check whether the given number is perfect number positive divisors, that is, the sum of its positive divisors excluding the number itself. Example - The first perfect number is 6, because 1, 2, and 3 are its proper positive divisors, and 1 + 2 + 3 = 6. b. Solve quadratic equations for the given values of a,b,c. 3 a. Generate all Amstrong numbers upto n. Defn: An Armstrong number of three digits is an integer such that the sum of the cubes of its digits is equal to the number itself. Example - 371 is an Armstrong number, since 3**3 + 7**3 + 1**3 = 371 b. Convert a decimal number to a hexadecimal number 4. a. Write a menu driven C program to a. Insert an element into an array b. Delete an element from the array (first occurrence) 5. a. Write a Menu Driven C Program to a. Accept a string from the user b. Encode the string. c. Decode the string Apply the following procedure to encode it. 1. Convert each character in a string to its ASCI value. 2. Add an integer value to it and display the encoded string 3. Decode the string using reverse procedure and display. 6. Write a C program to multiply two matrices that satisfy the constraint of matrix Multiplication 7. Write a C program to find the saddle point of a matrix. Defn: Given a RxC Matrix, A, i.e. R rows and C columns we define a Saddle-Point as Saddle_Pt (A) for a row I and column j is that A(i,j) that is the minimum of Row i and the maximum of Col j. [email protected] 2 3 C Programming Laboratory [13MCA16] 8. Write a C program to implement a magic square of size n. Defn: A magic square is an arrangement of numbers (usually integers) in a square grid, where the numbers in each row, and in each column, and the numbers that run diagonally in both directions, all add up to the same number. 9. Write a Menu driven C program to a. Accept two numbers n and m b. Sum of all integers ranging from n to m c. Sum of all odd integers ranging from n to m d. Sum of all even integers ranging from n to m Display an error message if n > m. Create functions for each of the options 10. Write a Menu Driven C Program to implement the following using recursion a. Factorial of a number b. Fibonacci series 11. Create a structure Complex Number having real and imaginary part as properties. Write functions to add and subtract the two complex numbers. 12. Define a structure called student having the properties of student_id, student name and branch of the student with a sub structure of marks of 3 subjects. Write a Menu Driven C Program to a. Add new student detail b. Delete a student detail c. Display all student details d. Display the average marks scored by the students 13. a. Write a C Program to remove all white spaces and newline characters from a file. b. Find whether a given word exists in the file. If it exists display the location of the word 14. Write a C program to copy one file content to another file without using inbuilt functions Note: All the programs compiled and executed with visual studio developer command prompt mode. [email protected] 3 4 C Programming Laboratory [13MCA16] 1. A. CONVERT DEGREES INTO FAHRENHEIT AND VICE VERSA SOURCE CODE: #include<stdio.h> #include<conio.h> void main(){ float cel,fahr; int d; clrscr(); while(1){ printf("\nEnter the Choice\n"); printf("\n1.Degree to Fahrenheit\n2.Fahrenheit to Degree\n3.Exit\n"); scanf("%d",&d); switch(d){ case 1: printf("\nEnter temp in Celsius : "); scanf("%f",&cel); fahr = (1.8 * cel) + 32; printf("\nTemperature in Fahrenheit : %f ",fahr); break; case 2: printf("\nEnter temp in Fahrenheit : "); scanf("%f",&fahr); cel = (100.0 / 180.0) * (fahr - 32); printf("\nTemperature in Degree : %f ",cel); break; case 3: exit(0); } } } OUTPUT: [email protected] 4 5 C Programming Laboratory [13MCA16] 1. . CALCULATE THE SALARY OF AN EMPLOYEE GIVEN HIS BASIC PAY, HRA = 10% OF BASIC PAY, TA=5% OF HIS BASIC PAY AND DEDUCTIONS IT = 2.5% OF HIS BASIC PAY SOURCE CODE: #include<stdio.h> #include<conio.h> void main(){ double bpay; printf("\nEnter the basic pay of employee :"); scanf("%lf",&bpay); printf("Salary==>%lf",(bpay+(bpay*0.10)+(bpay*0.05)+(bpay*0.025))); } OUTPUT: [email protected] 5 6 C Programming Laboratory [13MCA16] 2. A. CHECK WHETHER THE GIVEN NUMBER IS PERFECT NUMBER POSITIVE DIVISORS, THAT IS, THE SUM OF ITS POSITIVE DIVISORS EXCLUDING THE NUMBER ITSELF. EXAMPLE - THE FIRST PERFECT NUMBER IS 6, BECAUSE 1, 2, AND 3 ARE ITS PROPER POSITIVE DIVISORS, AND 1 + 2 + 3 = 6 SOURCE CODE: #include<stdio.h> void main() { int n,i=1,sum=0; printf("\nEnter a number: "); scanf("%d",&n); while(i<n){ if(n%i==0) sum+=i; i++; } if(sum==n) printf("\n%d is a Perfect Number",i); else printf("\n%d is Non Perfect Number",i); } OUTPUT: [email protected] 6 7 C Programming Laboratory [13MCA16] 2. B. SOLVE QUADRATIC EQUATIONS FOR THE GIVEN VALUES OF A,B,C. SOURCE CODE: #include<stdio.h> #include<conio.h> #include<math.h> #include<process.h> void main() { float a,b,c,root1,root2; float realp,imagp,disc; //clrscr(); printf("\n\t\t******** ROOTS OF QUADRATIC EQUATION *******\n"); printf("Enter the values to a,b and c :- "); scanf("%f%f%f",&a,&b,&c); if(a==0||b==0||c==0) { printf("Error:Roots cannot be determined\n"); exit(1); } else { disc=b*b-4.0*a*c; if(disc<0) { printf("Imaginary Roots\n"); realp=-b/(2.0*a); imagp=sqrt(abs(disc))/(2.0*a); printf("Root1 = %f +i %f\n",realp,imagp); printf("Root2 = %f -i %f\n",realp,imagp); } else if(disc == 0) { printf("Roots are real and equal\n"); root1=-b/(2.0*a); root2=root1; printf("Root1 = %f\n",root1); printf("Root2 = %f\n",root2); } else if(disc >0) { printf("Roots are real and distinct\n"); root1=(-b+sqrt(disc))/(2.0*a); root2=(-b-sqrt(disc))/(2.0*a); printf("Root1 = %f\n",root1); printf("Root2 = %f\n",root2); } } //getch(); } [email protected] 7 8 C Programming Laboratory [13MCA16] OUTPUT: [email protected] 8 9 C Programming Laboratory [13MCA16] 3. A. GENERATE ALL AMSTRONG NUMBERS UPTO N. DEFN: AN ARMSTRONG NUMBER OF THREE DIGITS IS AN INTEGER SUCH THAT THE SUM OF THE CUBES OF ITS DIGITS IS EQUAL TO THE NUMBER ITSELF. EXAMPLE - 371 IS AN ARMSTRONG NUMBER, SINCE 3**3 + 7**3 + 1**3 = 371 SOURCE CODE: #include <stdio.h> void main(){ int r; long n = 0, i, sum = 0, temp; printf("Enter an integer upto which you want to find armstrong numbers\n"); scanf("%ld",&n); printf("Following armstrong numbers are found from 1 to %ld\n",n); for( i = 1 ; i <= n ; i++ ) { temp = i; while( temp != 0 ) { r = temp%10; sum = sum + r*r*r; temp = temp/10; } if ( i == sum ) printf("%ld\n", i); sum = 0; } } OUTPUT: [email protected] 9 10 C Programming Laboratory [13MCA16] 3. B. CONVERT A DECIMAL NUMBER TO A HEXADECIMAL NUMBER SOURCE CODE: #include<stdio.h> int main(){ long int dec,r,q; int i=1,j,temp; char hexadec[100]; printf("Enter any decimal number: "); scanf("%ld",&dec); q = dec; while(q!=0){ temp = q % 16; if( temp < 10) temp =temp + 48; else temp = temp + 55; hexadec[i++]= temp; q = q / 16; } printf("Equivalent hexadecimal value of decimal number %d: ",dec); for(j = i -1 ;j> 0;j--) printf("%c",hexadec[j]); return 0; } OUTPUT: [email protected] 10 11 C Programming Laboratory [13MCA16] 4. A. WRITE A MENU DRIVEN C PROGRAM TO A. INSERT AN ELEMENT INTO AN ARRAY B. DELETE AN ELEMENT FROM THE ARRAY (FIRST OCCURRENCE) SOURCE CODE: int a[100]; void main() { int n,r,i; printf("Please enter the size of the array"); scanf("%d",&n); printf("\nPlease enter Elements of the array\n"); for (i= 0; i< n;i++) scanf("%d",&a[i]); printf("Please enter the number to be removed: "); scanf("%d",&r); i= remove(n,r); if (i!= -1) { printf("%d is found at %d position and removed\n",r,i); printf("Contents of the array After deletion\n"); for (i= 0; i< n-1;i++) printf("%d\n",a[i]); } else printf("Item Not Found!!!"); } int remove(int n, int r) { int i,c,j; for (i= 0; i < n; i++) if (a[i] == r){ j=i; for ( c = i ; c < n - 1 ; c++ ) a[c] = a[c+1]; return i; } return -1; } [email protected] 11 12 C Programming Laboratory [13MCA16] OUTPUT: [email protected] 12 13 C Programming Laboratory [13MCA16] 5. A. WRITE A MENU DRIVEN C PROGRAM TO A. ACCEPT A STRING FROM THE USER B. ENCODE THE STRING. C. DECODE THE STRING APPLY THE FOLLOWING PROCEDURE TO ENCODE IT. 1. CONVERT EACH CHARACTER IN A STRING TO ITS ASCI VALUE. 2. ADD AN INTEGER VALUE TO IT AND DISPLAY THE ENCODED STRING 3. DECODE THE STRING USING REVERSE PROCEDURE AND DISPLAY. SOURCE CODE: #include <stdio.h> void main() { char name[100],i; printf("Enter String: "); scanf("%s",name); for(i=0;i<strlen(name);i++) { name[i]=name[i]+5; } for(i=0;i<strlen(name);i++) { printf("String after encoding(ASCII+5)\n"); for(i=0;i<strlen(name);i++) printf("%d\n",name[i]); } for(i=0;i<strlen(name);i++) { name[i]=name[i]-5; } printf("String after Decoding\n"); printf("%s",name); } OUTPUT: [email protected] 13 14 C Programming Laboratory [13MCA16] 6. WRITE A C PROGRAM TO MULTIPLY TWO MATRICES THAT SATISFY THE CONSTRAINT OF MATRIX MULTIPLICATION SOURCE CODE: #include<stdio.h> #include<conio.h> void main() { int i,j,k,a[10][10],b[10][10],c[10][10]; int r1,r2,c1,c2; //clrscr(); printf("Enter 1st matrix size\n"); scanf("%d%d",&r1,&c1); printf("Enter 2nd matrix size\n"); scanf("%d%d",&r2,&c2); if(c1!=r2) printf("Matrix multiplication is not possible\n"); else { printf("Enter elements to 1st matrix\n"); for(i=0;i<r1;i++) for(j=0;j<c1;j++) scanf("%d",&a[i][j]); printf("Enter elements to 2nd matrix\n"); 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<c1;j++) { c[i][j]=0; for(k=0;k<c1;k++) c[i][j]=c[i][j]+a[i][k]*b[k][j]; } printf("Product of 2 matrix is\n"); for(i=0;i<r1;i++){ for(j=0;j<c1;j++) printf("%d\t",c[i][j]); printf("\n"); } } //getch(); } [email protected] 14 15 C Programming Laboratory [13MCA16] OUTPUT: [email protected] 15 16 C Programming Laboratory [13MCA16] 7. WRITE A C PROGRAM TO FIND THE SADDLE POINT OF A MATRIX. DEFN: GIVEN A RXC MATRIX, A, I.E. R ROWS AND C COLUMNS WE DEFINE A SADDLE-POINT AS SADDLE_PT (A) FOR A ROW I AND COLUMN J IS THAT A(I,J) THAT IS THE MINIMUM OF ROW I AND THE MAXIMUM OF COL J. SOURCE CODE: #include<stdio.h> #include<conio.h> main() { int a[10][10],i,j,k,n,min,max,col,m; //clrscr(); printf("enter order m,n of mxn matrix : "); scanf("%d %d",&m,&n); printf("enter elements row-wise\n"); for(i=0;i<m;i++) { for(j=0;j<n;j++) { scanf("%d",&a[i][j]); } } for(i=0;i<m;i++) { min=a[i][0]; for(j=0;j<n;j++) { if(a[i][j]<=min) { min=a[i][j]; col=j; } } max=a[0][col]; for(k=0;k<m;k++) { if(a[k][col]>=max) { max=a[k][col]; } } if(max==min) printf("saddle pt.at (%d,%d)",i+1,col+1); } //getch(); } [email protected] 16 17 C Programming Laboratory [13MCA16] OUTPUT: [email protected] 17 18 C Programming Laboratory [13MCA16] 8. WRITE A C PROGRAM TO IMPLEMENT A MAGIC SQUARE OF SIZE N. DEFN: A MAGIC SQUARE IS AN ARRANGEMENT OF NUMBERS (USUALLY INTEGERS) IN A SQUARE GRID, WHERE THE NUMBERS IN EACH ROW, AND IN EACH COLUMN, AND THE NUMBERS THAT RUN DIAGONALLY IN BOTH DIRECTIONS, ALL ADD UP TO THE SAME NUMBER. SOURCE CODE: #include <stdio.h> int main() { int n,row,col,next_row,next_col,n,i,j,start; int magic[99][99]; int max ; printf("Enter size of magic square: "); scanf("%d", &n); start = (n / 2); max = n * n; magic[0][start] = 1; for (i = 2, row = 0, col = start; i < max + 1; i++) { if ((row - 1) < 0) { next_row = n - 1; } else { next_row = row - 1; } if ((col + 1) > (n - 1)) { next_col = 0; } else { next_col = col + 1; } if (magic[next_row][next_col] > 0) { if (row > (n - 1)) { next_row = 0; } else { next_row = row + 1; next_col = col; } } row = next_row; col = next_col; magic[row][col] = i; } for (i = 0; i < n; i++) { for (j = 0; j < n; j++) { printf("%4d", magic[i][j]); } printf("\n"); } return 0; } [email protected] 18 19 C Programming Laboratory [13MCA16] OUTPUT: [email protected] 19 20 C Programming Laboratory [13MCA16] 9. WRITE A MENU DRIVEN C PROGRAM TO A. ACCEPT TWO NUMBERS N AND M B. SUM OF ALL INTEGERS RANGING FROM N TO M C. SUM OF ALL ODD INTEGERS RANGING FROM N TO M D. SUM OF ALL EVEN INTEGERS RANGING FROM N TO M DISPLAY AN ERROR MESSAGE IF N > M. CREATE FUNCTIONS FOR EACH OF THE OPTIONS SOURCE CODE: #include<stdio.h> #include<conio.h> void main() { int sum=0,i,m,n,ch; while(1){ printf("1.Sum of All numbers \n2.Sum of all even numbers\n3.Sum of all odd numbers \n4.exit\n"); printf("Enter your choice :"); scanf("%d",&ch); switch(ch){ case 1: sum=0; printf("\nEnter n and m value\n"); scanf("%d %d",&n,&m); if(m<n) printf("\n m value should be greater than n\n"); for(i=n;i<=m;i++) sum=sum+i; printf("\nSum of %d to %d is %d\n",n,m,sum); break; case 2: sum=0; printf("\nEnter n and m value \n"); scanf("%d %d",&n,&m); if(m<n) printf("\n m value should be greater than n\n"); for(i=n;i<=m;i++) if(i%2==0) sum=sum+i; printf("\nSum of even numbers from %d to %d is %d\n",n,m,sum); break; case 3: sum=0; printf("\nEnter n and m value \n"); scanf("%d %d",&n,&m); if(m<n) printf("\n m value should be greater than n\n"); for(i=n;i<=m;i++) if(i%2!=0) sum=sum+i; printf("\nSum of odd numbers from %d to %d is %d\n",n,m,sum); break; case 4: exit(0); [email protected] 20 21 C Programming Laboratory [13MCA16] default: printf("Invalid option\n"); break; } } } OUTPUT: [email protected] 21 22 C Programming Laboratory [13MCA16] 10. WRITE A MENU DRIVEN C PROGRAM TO IMPLEMENT THE FOLLOWING USING RECURSION A. FACTORIAL OF A NUMBER B. FIBONACCI SERIES SOURCE CODE: int fact(int n){ if(n==1) return 1; return n*fact(n-1); } int fibo(int n){ if(n == 0 || n == 1) return n; return fibo(n-1) + fibo(n-2); } void main() { int i,j=0,m,n,ch; while(1){ m=0; printf("\n1.FACTORIAL OF A NUMBER \n2. FIBONACCI SERIES \n3.exit\n"); printf("Enter your choice :"); scanf("%d",&ch); switch(ch){ case 1: printf("\nEnter n :"); scanf("%d",&n); m=fact(n); printf("factorial of a given number is :%d",m ); break; case 2: j=0; printf("\nEnter n :"); scanf("%d",&n); printf("fibonanci series of a given number is:"); for ( i = 0 ; i < n ; i++ ){ printf("%d\n", fibo(j)); j++; } break; case 3: exit(0); default: printf("Invalid option\n"); break; } } } [email protected] 22 23 C Programming Laboratory [13MCA16] OUTPUT: [email protected] 23 24 C Programming Laboratory [13MCA16] 11. CREATE A STRUCTURE COMPLEX NUMBER HAVING REAL AND IMAGINARY PART AS PROPERTIES. WRITE FUNCTIONS TO ADD AND SUBTRACT THE TWO COMPLEX NUMBERS. SOURCE CODE: #include <stdio.h> struct complex { int real, img; }; struct complex a, b, c; void readcom(){ printf("Enter a and b where a + ib is the first complex number.\n"); printf("a = "); scanf("%d", &a.real); printf("b = "); scanf("%d", &a.img); printf("Enter c and d where c + id is the second complex number.\n"); printf("c = "); scanf("%d", &b.real); printf("d = "); scanf("%d", &b.img); } void addcom(){ c.real = a.real + b.real; c.img = a.img + b.img; if ( c.img >= 0 ) printf("Sum of two complex numbers = %d + %di\n",c.real,c.img); else printf("Sum of two complex numbers = %d %di\n",c.real,c.img); } void subcom(){ c.real = a.real - b.real; c.img = a.img - b.img; if ( c.img >= 0 ) printf("\nDifference of two complex numbers = %d + %di",c.real,c.img); else printf("\nDifference of two complex numbers = %d %di",c.real,c.img); } void main() { readcom(); addcom(); subcom(); } [email protected] 24 25 C Programming Laboratory [13MCA16] OUTPUT: [email protected] 25 26 C Programming Laboratory [13MCA16] 12. DEFINE A STRUCTURE CALLED STUDENT HAVING THE PROPERTIES OF STUDENT_ID, STUDENT NAME AND BRANCH OF THE STUDENT WITH A SUB STRUCTURE OF MARKS OF 3 SUBJECTS. WRITE A MENU DRIVEN C PROGRAM TO A. ADD NEW STUDENT DETAIL B. DELETE A STUDENT DETAIL C. DISPLAY ALL STUDENT DETAILS D. DISPLAY THE AVERAGE MARKS SCORED BY THE STUDENTS SOURCE CODE: #include <stdio.h> #include<math.h> typedef struct Student { char name[20]; char branch[20]; char id[20]; int m1,m2,m3; float avg; }Student; int main(void){ char id[10]; FILE *fp,*ft; char another,choice; Student s; char fname[20]; char lname[20]; long int recsize; fp=fopen("s.DAT","rb+"); if(fp==NULL) { fp=fopen( "s.DAT","wb+"); if(fp==NULL) { printf("Can't Open File"); exit(); } } recsize=sizeof(s); while(1) { printf("\n1.Add Records\n2.Delete Record \n3.ListRecords\n4.Diaply Average Marks\n5.Exit"); printf("\nEnter your choice :"); fflush(stdin); scanf("%c",&choice); switch(choice) { case'1': fseek(fp,0,SEEK_END); [email protected] 26 27 C Programming Laboratory [13MCA16] printf("\nEnter the id,name branch : "); scanf("%s %s %s",s.id,s.name,s.branch); printf("\nEnter the marks1 marks 2, marks 3 :"); scanf("%d %d %d",&s.m1,&s.m2,&s.m3); s.avg=(s.m1+s.m2+s.m3)/3.0; fwrite(&s,recsize,1,fp); break; case '2': printf("Enter the id of the Student to be deleted : "); scanf("%s",&id); ft=fopen("TEMP.DAT","wb"); rewind(fp); while(fread(&s,recsize,1,fp)==1) { if(strcmp(s.id,id)!=0) fwrite(&s,recsize,1,ft); } fclose(fp); fclose(ft); remove("s.DAT"); rename("TEMP.DAT","s.DAT"); fp=fopen("s.DAT","rb+"); break; case '3': rewind(fp); while(fread(&s,recsize,1,fp)==1) printf("\n %s %s %s %d %d %d",s.name,s.id,s.branch,s.m1,s.m2,s.m3); break; case '4' : rewind(fp); while(fread(&s,recsize,1,fp)==1) printf("\n %s %s %s %d %d %d %f",s.name,s.id,s.branch,s.m1,s.m2,s.m3,s.avg); break; case '5': fclose(fp); exit(); } } } [email protected] 27 28 C Programming Laboratory [13MCA16] OUTPUT: [email protected] 28 29 C Programming Laboratory [13MCA16] 13. A. WRITE A C PROGRAM TO REMOVE ALL WHITE SPACES AND NEWLINE CHARACTERS FROM A FILE SOURCE CODE: #include <stdio.h> #include <stdlib.h> int main(){ FILE *fp,*fp1; char ch; fp=fopen("g.txt","r"); fp1=fopen("g2.txt","w"); if(fp==NULL) { printf("Some problem in opening the file"); exit(0); } else { while((ch=fgetc(fp))!=EOF) { if(ch==' '|| ch=='\n') { } else { fprintf(fp1,"%c",ch); } } } fclose(fp); return 0; } OUTPUT: [email protected] 29 30 C Programming Laboratory [13MCA16] 13. B. FIND WHETHER A GIVEN WORD EXISTS IN THE FILE. IF IT EXISTS DISPLAY THE LOCATION OF THE WORD SOURCE CODE: #include <stdio.h> #include <stdlib.h> int main(){ FILE *fp; int line_num = 1; int find_result = 0; char temp[512]; char str[10]; printf("Enter a String"); scanf("%s",str); fp=fopen("g.txt","r"); while(fgets(temp, 512, fp) != NULL) { if((strstr(temp, str)) != NULL) { printf("A match found on line: %d\n", line_num); printf("\n%s\n", temp); find_result++; } line_num++; } if(find_result == 0) { printf("\nSorry, couldn't find a match.\n"); } if(fp) { fclose(fp); } return(0); } OUTPUT: [email protected] 30 31 C Programming Laboratory [13MCA16] 14. WRITE A C PROGRAM TO COPY ONE FILE CONTENT TO ANOTHER FILE WITHOUT USING INBUILT FUNCTIONS SOURCE CODE: #include<stdio.h> #include<conio.h> void main() { FILE *fp1,*fp2; char ch,fname1[20],fname2[20]; printf("\nEnter source file name :"); gets(fname1); printf("\nEnter dest file name :"); gets(fname2); fp1=fopen(fname1,"r"); fp2=fopen(fname2,"w"); if(fp1==NULL||fp2==NULL) { printf("unable to open"); exit(0); } do { ch=fgetc(fp1); fputc(ch,fp2); } while(ch!=EOF); fcloseall(); printf("\n Files copied Successfully"); getch(); } OUTPUT: [email protected] 31 32 C Programming Laboratory [13MCA16] For More Study Materials and E-Books Mail me : [email protected] (Please mention your name and college in body of the email) [email protected] 32
© Copyright 2024 Paperzz