0% found this document useful (0 votes)
6 views

c Programming Lab

The document outlines various C programming exercises from Dr. M.G.R. Educational and Research Institute, including algorithms and code for finding the biggest of three numbers, checking for prime numbers, determining palindromes, calculating the greatest common divisor, generating Fibonacci series, performing matrix addition, calculating factorials using recursion, and processing student marks using structures. Each exercise includes an aim, algorithm, and sample code. The document serves as a practical guide for students learning C programming.

Uploaded by

Pat Rick
Copyright
© © All Rights Reserved
Available Formats
Download as PDF or read online on Scribd
0% found this document useful (0 votes)
6 views

c Programming Lab

The document outlines various C programming exercises from Dr. M.G.R. Educational and Research Institute, including algorithms and code for finding the biggest of three numbers, checking for prime numbers, determining palindromes, calculating the greatest common divisor, generating Fibonacci series, performing matrix addition, calculating factorials using recursion, and processing student marks using structures. Each exercise includes an aim, algorithm, and sample code. The document serves as a practical guide for students learning C programming.

Uploaded by

Pat Rick
Copyright
© © All Rights Reserved
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 32
By Dr. M.G.R. ‘ ae EDUCATIONAL AND RESEARCH INSTITUTE \ oe (Deemed to be University) Maduravoyal, Chennal - 600.095, Tamilnae {iv 04004, 201% Ceried nsitatlon) Ex. No. 1 ~~ BIGGEST OF THREE NUMBERS DATE; Aim: To find the biggest of three numbers using C. Algorithm : 1. Start 2. Accept three numbers and store it in a,b and c. 3. First Check whether ‘a’ is greater than b and c. i) Ifyes Print “ ais Big” ii) Otherwise check ‘b’ is greater than ‘c. If Yes Print “b is Big” else Print “cis Big” 4, Stop. @ scanned with OKEN Scanner \ Dr. M.G.R. EDUCATIONAL AND RESEARCH INSTITUTE (Deemed to be University Maduravoyal, Chennai 600 09 nadu. India, sist 1 ried natin) PROGRAM : BIGGEST OF THREE NUMBERS #include #include void main() { int a,b,c; clrscr(); printf("Enter the value of a,b and c:"); scanf("%6d%d%d", 8a, &b, 8c}; if((a>b)&&(a>c)) printf("A is big"); else if(b>c) printf("B is big"); else printf("C is big"); getch(); i @ scanned with OKEN Scanner “os 6 Dr. M.G.R.. ae a G * a | ‘@ RESEARCH INSTITUTE “ie 2 EDUCATIONAL AND RESEARCH INST e |» © | » OUTPUT : BIGGEST OF THREE NUMBERS vv ° v v vv @ scanned with OKEN Scanner a ne Dr. M.G.R. TE es FARCH INSTITU Q, EDUCATIONAL AND RESET eomed to i (perros 600 095, Tamilnadu. India. Ex. No.:2 PRIME NUMBER DATE: Aim: To find whether the given number is Prime number or not using c Language Algorithm : 1. Start 2. Accept a number and store it in the variable n. 3. Assign a flag to zero. 4. Divide the given number from 2 to n-1 using for loop. (i) Ifany number divides the given number then set the flag as 1 and comes out of loop. 5. Finally, Check whether the flag is 1 then Print “The given number is not Prime” otherwise Print “The given number is Prime”. 6. Stop. @ scanned with OKEN Scanner Dr. M.G.R. NAL AND RESEARCH INSTITUTE (£ med to be Unive! ah 3 1 GE, EDUCATIONAL / Chena -c0009 Maduravoyal. C an tso 9001; 2015 Cotte PROGRAM : PRIME NUMBER #include #include void main() { int r,n,flag,i; clrscr(); printf("Enter the value of n:"); scanf("96d",&n); if(flag==1) printf("The given number is not prime"); else printf("The given number is prime"); getch(); @ scanned with OKEN Scanner ee pr ee INSTITUT 5 ATIONAL AND RESE: rote (A) G2, EDUCATIONAL (a SZ ed to be University) aa acu. India. @ scanned with OKEN Scanner Dr. M.G.R. “EDUCATIONAL AND RESEARCH INSTITUTE (& (Deemed to be University) Maduravoyal, Chennai nadu. India, {(unI8a 9001 2015 Certified a) Result : @ scanned with OKEN Scanner ee Dr. M.G.R. &", EDUCATIONAL AND RESEARCH INSTITUTE \ (Deemed to be University) Maduravoyal, Chennai - 600 095. Tamilnadu, India. {(AwISO 9001 : 2015 Certified Institution) f Ex. No. : 3 | PALINDROME DATE: Aim: To find whether the given string is palindrome or not using c. Algorithm : Start. . Accept a string and store it in the variable ‘name’. . Using the library function strcpy, copy the string name to rev. . Using the library function strrev, reverse the string rev. . Using the library function strcmp , compare the original string name with the reverse the string rev. (i) If both the strings are equal then Print “The given string is Palindrome”, Otherwise Print “The given string is Not Palindrome”. 6. Stop. wpUne @ scanned with OKEN Scanner Dr. M.G.R. EDUCATIONAL AND RESEARCH INSTITUTE (Deemed to be University) Maduravoyal, Chennai - 600 095, Tamilnadu. India (in 150 9001 : 2015 Certified Institution) (? PROGRAM :PALINDROME #include #include Hinclude void main() { int x; char name[25],rev[25]; elrser(); printf("Enter the string gets(name); strepy(rev,name); strrev(rev); x=stremp(name, rev); if(x==0) printf("The given string is Palindrome"); else printf("The given string is not palindrome"); getch(); } @ scanned with OKEN Scanner a Dr. M.G.R. re SQ EDUCATIONAL AND RE EARCH INSTITUTE \ sce : (Deemed to be University) t Maduravoyal, Chennai - 600 095, Tamilnadu. India {an iso 9001 2018 certified Institution) OUTPUT : PALINDROME rT Psu ord So Sino e given string is not palindrome_ @ scanned with OKEN Scanner a Dr. M.G.R. EDUCATIONAL AND RESEARCH INSTITUTE & y (Deemed to be University) Maduravoyal, Chennai 600 095. Tamilnadu, Indl. Yuin yt 215d avo) | Result : @ scanned with OKEN Scanner . M.G.R. if Pine RESEARCH INSTITUTE eons med to be University) Chennai - 600 0: ‘so got; 2015 Certified GcD Aim: To find the Greatest Commo! n Divisor of given two numbers using ¢- Algorithm : 1, Start. 2. Accept two Numbers m and n. 3. Find the smallest value among m and nand store it ing. 4. Call the recursive function ged using the statement: gcd (m,n,g)- 5. Print the gcd. 6. Stop. Recursive function : gcd(mi,nLet 1. Start 2. i) If both the input values are divided by g1 then the GCD value is g1, Return the value ‘g1’ to the calling function. ii)Otherwise call the recursive function gcd(m1,n1,g1-1) @ scanned with OKEN Scanner en Dr. .G.R. ‘ ) EDUCATIONAL AND RESEARCH INSTITUTE ( DEEM BE UNIVERSITY a PROGRAM: #include #include int ged (intint,int); void main() { int am,n,g; clrscr(); printf("Greatest Common Divisor’); printf("\nEnter the First Number:”); scanf("%d",&m); printf("Enter the Second Number:"); scanf("%d", &n); if(m<=n) g=m; else g=n; a=gced (m,ng); printf("Greatest common Divisor is : %d",a); getch(); } int ged (int m1,int n1,int g1) { if((m1%g1==0)&&(n1%g1==0)) return(g1); else ged (m1,n1,g1-1); @ scanned with OKEN Scanner 2 Dr. M.G.R. 3 \@= EDUCATIONAL AND RESEARCH INSTITUTE \ = (Deemed to be University) Maduravoyal, Chennai - 600 095, Tamilnadu. India (@m150 90012015 Certified asitution) Tee Number aces er a aes \ @ scanned with OKEN Scanner Maduravoyal, Chennai - 600 095, . Dr. M.G.R. EDUCATIONAL AND RESEARCH INSTITUTE (Deemed to be University) To find a Fibonacci Series Using C Program. Algorithm : 1. Start 2. Accept the value for n 3. Call the recursive function: fib(!) 4. Print the Fibonacci value 5. Stop. Recursive functio 1. Start 2. i)If the value of num1 is 0 then return 0. ii)If the value of num1 is 1 then return 1, iii) Otherwise call the recursive function: fib (num1-1)+fib(num1-2) 4. Return the Fibonacci value — }. Tamilnadu. India. RNG {dn 50 90017015 cred testo) % No. :5 FIBONACCI SERIES DATE: : __ Aim: | @ scanned with OKEN Scanner Dr. .G.R. “EDUCATIONAL AND RESEARCH INSTITUTE DEEMED TO BE UNIVERSITY oa ae PROGRAM: #include #include int fib(int num1); void main() { int numa; clrser(); printf("Fibonacci Series"); printf("Enter the number of terms:"); scanf("%d",&num); printf(‘The Fibonacci Series is :"); for(i=0;icnum;i++) { a=fib(i); printf("%d",a); } getch(); } int fib(int num1) { if(num1==0) return(0); else if(num1==1) return(1); else return(fib(num1-1)+fib(num1-2)); @ scanned with OKEN Scanner Dr. M.G.R. ran , EDUCATIONAL AND RESEARCH INSTITUTE tan ) (Deemed to be University) fet Maduravoyal, Chennai - 600 095. Tamilnadu. India. (An I80 9001 :2015 Certified stitution) OUTPUT : FIBONACCI SERIES we @ scanned with OKEN Scanner GR. Ce SEARCH INSTITUTE es, be University) 00 095. Tamilnadu. Ind 5. ified. Hon) ___— Ex. No MATRIX ADDITION DATE Aim: To add the given two matrices using ¢. Algorithm : Start . Accept matrix row and column and store it in r,c. . Accept matrix A . Accept matrix B . Add both the matrices and store the resultant matrix in D. . Print the resultant matrix D. . Stop. NOWURWNH @ scanned with OKEN Scanner oi Dr. M.G.R. @_- EDUCATIONAL AND RESEARCH INSTITUTE (Deemed to be University) PROGRAM : MATRIX ADDITION #include #include void main() { int r,c,i,j,a[10}[10],b[10][10},d[10}[10]; clrser(); printf("Matrix Addition"); printf("\nEnter the rows and cols of the matrices:\n"); scanf("%d%d",&r,&c); printf("Enter the first matrix:"); scanf("%d", &alilli); printf("Enter the Second matrix: printf("Matrix Addition\n"); for(i=1ji<=r;it+) for(j=1;j<=c;j++) tn printf("%d",dLi]Ei)); oe) eo @ scanned with OKEN Scanner ir Dr. M.G.R. Gy EDUCATIONAL AND RESEARCH INSTITUTE (Deemed to be University) Maduravoyal, Chennai - 600 095, Tamilnadu. India. {ims 9004; 2045 certified ison) a QUTPUT : MATRIX ADDITION @ scanned with OKEN Scanner Dr. M.G.R. - EDUCATIONAL AND RESEARCH INSTITUTE ( aa {Deemed to be University) [Bx Nose tint ar Seca tt 7 a FACTORIAL USING RECURSION \ Aim: ‘To write a C Program to find the factorial for a given number using recursion. Algorithm : 1) Start 2) Accept value for n 3) Call the factorial function fact fun(n) 4) Print the factorial value 5) Stop Recursive function fact fun(into n1| 1 Start 2 a) If the value of n1 is less than or equal to 1 then return the value 1 to the calling function. b) Otherwise call the recursive function using the statement. Return(n*fact fun(n1-1)) 3 Return the factorial value to the main @ scanned with OKEN Scanner \ Dr. M.G.R. _ EDUCATIONAL AND RESEARCH INSTITUTE (Deemed to be University) Maduravoyal, Chennai - 600 095. Tamilnadu. India. Am 150 9001: 2015 Certified Institution) PROGRAM :FACTORIAL USING RECURSION #include #include int factfun(int); void main() { int n, fact; clrscr(); printf("Enter the value for n "5 scanf("%d",&n); fact=factfun(n); printf("The Factorial Value = %d",fact); getch(); } int factfun(int n1) if(n4<=1) return(1); else return(n1*factfun(n1-1)); } @ scanned with OKEN Scanner ees ev Sea oeetcnoy Petes etme @ scanned with OKEN Scanner Dr. M.G.R. A 2 EDUCATIONAL AND RESEARCH INSTITUTE \ (Deemed to be Univer: sity) Maduravoyal, Chennai - 600 095. Tamilnadu. India. DATE: (An 150 9001 ; 2015 Certified Institution) Ex. No. :8 | STUDENT MARK SHEET PROCESSING USING STRUCTURE Aim: Algorithm : 1) Start 2) Define the structure student 5) Accept five subject marks 7) Stop. 3) Create variable for the structure 4) Accept values student name,roll no,branch. To write a C program for Student Mark Processing System using Structure. 6) Print student information scored more than 400 marks @ scanned with OKEN Scanner a Dr. M.G.R. we ey EDUCATIONAL AND RESEARCH INSTITUTE 7 (Deemed to be Universi Maduravoyal, Chenn {150900120 PROGRAM :STUDENT MARK PROCESSING USING STRUCTURE Hinclude #include | void main() { x S struct student { char name[80]; int rollno; char branch{20]; int submarks[5]; int totmarks; i struct student stud[10]; intij; clrser(); printf("THREE STUDENTS BIO DATA"); for(i=0;i<3;i++) printf("\nEnter the bio-data of %d students",i+1); printf("\nEnter the name,rolino,branch:"}; scanf("'%s%d%s",stud|i].name,&stud{i].rollno,stud{i].branch), printf("Enter the FIVE subject marks one by one:"); { scanf("g6d", &stud[i].submarks{j); stud{i].totmarks=stud[i].totmarks+stud{i] submarkstj]; yt printf("Bio-data of students whose marks are greater than 400 are:"); for(i=0;i<3;i++) { if(stud|i].totmarks>400) printf("\nName=%s,Rollno=%d,Totmarks=%d", stud }.totmarks); [i].name,stud{i].rolino,studfi } getch(); } a @ scanned with OKEN Scanner Vvueuvgwuvrv-~ Dr. M.G.R. @, EDUCATIONAL AND RESEARCH INSTITUTE \ : (Deemed to be University) Nts Maduravoyal, Chennai - 600 095, Tamilnadu. India. {a!80 9001; 2045 Cet astitotion) TOON Dio-data of 1 students TOA Scum eee) SOA es ats aR Es COE La Pete mre cy Seu oc use u mea the FIVE subject marks one by one:22 12 44 85 33 PCat Mme Cus pregereve spur Sus a. na eu dnc Sere en ey Cees Seren aper erat ea SSI Can ee eee eo oe ea & Scanned with OKEN Scanner Dr. M.G.R. , EDUCATIONAL AND RESEARCH INSTITUTE ( (Deemed to be Unive i tone? Maduravoyal, Chenna my, India. {(urts0 9001 ; 201 Ex. No. :9. ‘entified Institution . SWAPPING US| DATE: ING POINTERS Aim: To write a C Program to swap two values using Pointer. Algorithm : 1) Start 2) Assign value 1 to a and 2 to B 3) Print value Before swapping 4) Call the Swapping function : swap(&a,&b) 5) Print the swapped values 6) Stop. Function: swap(int *x,int 7 1) Start 2) Using temporary value't' the input values are swapped - 3) Print the values inside function. @ scanned with OKEN Scanner (Deemed to he Maduravoyal, Che PROGRAM : SWAPPING USING POINTERS #include #include int swap(int[],int{]); void main() clrser(); printf("value of a and b before swap() are %d and %d",a,b); swap(&a,&b); printf("\n Value of a and b after swap() are %d and %d",a,b); | getch(); } int swap(int *x,int *y) { int t; te*x; y=t; printf("\n Value of x and y in swap() are %d and %d",*x,*y); } Dr. M.G.R. Se EDUCATIONAL AND RESEARCH INSTITUTE 00 095. Ts University) inadu, India ied Institution @ scanned with OKEN Scanner EDUCAT L ANI | OUTPUT : SWAPPING USING POINTERS SS Oe Sten ear arene cranes PS eM Stes Oe ea & Scanned with OKEN Scanner Dr. M.G.R. en ey EDUCATIONAL AND RESEARCH INSTITUTE | we (Deemed to be Univers Maduravoyal, a {4180 9001; 2015 Crtied institution) Ht Ex. No. : 10 FILE OPERATIONS DATE: | Aim: To write a C Program to read and write data in file. Algorithm : 1) Start 2) Opena file input.dat with write mode | 3) Get a single character from the keyboard and write it in the file. 4) Close the file 5) Open the same file input.dat with read mode 6) Read single character at a time and print it on the monitor 7) Close the file 8) Stop @ scanned with OKEN Scanner * ay (Deemed to Maduraveyal, Chennai 600 eee a8 1001: 201 PROGRAM : FILE OPERATIONS #include #include void main() { FILE *fp1; char ¢; clrser(); printf("Enter data for input f file:\n"); fp1=fopen("input. dat","w" while((c=getchar())! putc(c,fp1); fclose(fp1); printf("Display data from file input.dat:"); fp1=fopen("input.« dat","r"); while((c=getc(fp1))!=EOF) printf("%c",c); fclose(fp1); getch(); Dr. M.G.R. EDUCATIONAL AND RESEARCH INSTITUTE sil 95, ja Unstitution) Sp aclu, Endliat, @ scanned with OKEN Scanner OUTPUT : FILE OPERATIONS Rear [Riazi Welcome to MGR University. Pisplay data from file input.dat:Hai Welcome to MGR University & Scanned with OKEN Scanner

You might also like