C Programming Lab
Unit 1 — Problem Analysis Charts, Flowcharts &
Pseudocode
Program 1: Sum of Digits of an Integer
Program 2: Largest of Three Numbers
Program 3: Factorial (n!)
Unit 2 — Conditional Logic
Program 1: Grade Calculator (if-else ladder)
Program 2: Leap Year Test
Program 3: Menu-driven Calculator (switch)
Unit 3 — Functions
Program 1: Check Prime
Program 2: Recursive Factorial
Program 3: GCD using Euclid
Unit 4 — Pointers, Dynamic Memory, String & Array Operations
Program 1: Dynamic Array Average
Program 2: String Reverse & Vowel Count
Program 3: Bubble Sort
Unit 5 — Structures and Unions
Program: Employee with Union for Pay Type
Unit 6 — Files (Text & Binary)
Program 1: Write & Read Text File
Program 2: Binary Copy
Program 3: Store & Load Records
Unit 7 — Libraries
Program 1: Quadratic Roots using <math.h>
Program 2: Character Classification using <ctype.h>
Program 3: User-defined Library (header + source)
Unit 8 — Mini Projects
Project 1: Student Management System
Project 2: Simple Inventory Management
EX NO: 1 - SUM OF DIGITS
Algorithm / Pseudocode
step 1: start the program
step 2:read n
step 3:sum = 0
step 4:while n > 0 do
step 5: sum = sum + (n % 10)
step 6:n = n / 10
step 7:end while
step 8:print sum
step9 : stop the program
Flowchart
C Program
#include <stdio.h>
int main(void){
long n; int sum = 0;
printf("Enter an integer: ");
if (scanf("%ld", &n)!=1) return 0;
if (n<0) n = -n;
while(n>0){ sum += n%10; n/=10; }
printf("Sum of digits = %d\n", sum);
return 0;
}
Output:
Input:
Enter an integer: -4523
Output:
Sum of digits = 14
Result :
Thus the SUM OF DIGITS program was successfully executed.
EX NO:2 - Largest of Three Numbers
Algorithm
Step 1: Start
Step 2: Read three numbers a, b, c
Step 3: Set max = a
Step 4: If b > max, then set max = b
Step 5: If c > max, then set max = c
Step 6: Print max as the largest number
Step 7: Stop
Flowchart :
C Program
#include <stdio.h>
int main(void){
double a,b,c, max;
printf("Enter three numbers: ");
if (scanf("%lf%lf%lf", &a,&b,&c)!=3) return 0;
max = a; if(b>max) max=b; if(c>max) max=c;
printf("Largest = %.2f\n", max);
return 0;
}
Output:
Input:
Enter three numbers: 12.5 7.3 9.8
Output:
Largest = 12.50
Result :
Thus the Largest of Three Numbers program was successfully executed.
EX NO:3 - Factorial (Iterative)
Algorithm:
1. Start
2. Declare an integer variable n and an unsigned long variable fact
3. Prompt the user to enter a number n (expected range: 0 to 12)
4. Read the input value of n
5. Check if the input is invalid:
6. If scanf fails or n < 0, then:
a. Print "Invalid"
b. Stop
7. Initialize fact = 1
8. Repeat for i from 1 to n:
9. Multiply fact by i and update fact
10. Print the result: n! = fact
FLOWCHART:
PROGRAM
/* Factorial using iterative method
Pseudocode: fact=1; for i=1..n fact*=i; print fact
*/
#include <stdio.h>
int main(void){
int n;
printf("Enter n (0-12): ");
if (scanf("%d", &n)!=1 || n<0){ puts("Invalid"); return 0; }
unsigned long fact=1; for(int i=1;i<=n;i++) fact*=i;
printf("%d! = %lu\n", n, fact);
return 0;
}
Output:
Input:
Enter n (0-12): 5
Output:
5! = 120
Result :
Thus the Factorial program was successfully executed.
EX NO:4 - Grade Calculator (if-else)
🧠 Algorithm: Grade Calculator
1. Start
2. Declare an integer variable m
3. Prompt the user to enter marks (0–100)
4. Read the input value of m
5. Check if input is invalid:
o If scanf fails or m < 0 or m > 100, then:
Print "Invalid"
Stop
6. If m >= 90, then print "Grade: O"
7. Else if m >= 80, then print "Grade: A+"
8. Else if m >= 70, then print "Grade: A"
9. Else if m >= 60, then print "Grade: B"
10. Else if m >= 50, then print "Grade: C"
11. Else print "Grade: F"
12. Stop
FLOWCHART :
/* Grade Calculator - if-else ladder */
#include <stdio.h>
int main(void){
int m; printf("Enter marks (0-100): ");
if (scanf("%d", &m)!=1) return 0;
if(m<0||m>100) puts("Invalid");
else if(m>=90) puts("Grade: O");
else if(m>=80) puts("Grade: A+");
else if(m>=70) puts("Grade: A");
else if(m>=60) puts("Grade: B");
else if(m>=50) puts("Grade: C");
else puts("Grade: F");
return 0;
}
Output:
Input:
Enter marks (0-100): 87
Output:
Grade: A+
Result :
Thus the Grade Calculator (if-else) program was successfully
executed.
EX NO:5- Leap Year Test
/* Leap year test */
#include <stdio.h>
int main(void){
int y; printf("Enter year: ");
if (scanf("%d", &y)!=1) return 0;
if ((y%400==0) || (y%4==0 && y%100!=0))
printf("%d is leap year\n", y);
else printf("%d is not leap year\n", y);
return 0;
}
Output:
Input:
Enter year: 2000
Output:
2000 is leap year
EX NO:6- Menu-driven Calculator (switch)
/* Menu-driven calculator using switch */
#include <stdio.h>
int main(void){
double a,b; char op;
printf("Enter a op b (e.g., 3 + 4): ");
if (scanf("%lf %c %lf", &a, &op, &b)!=3) return 0;
switch(op){
case '+': printf("%.2f\n", a+b); break;
case '-': printf("%.2f\n", a-b); break;
case '*': printf("%.2f\n", a*b); break;
case '/': if(b!=0) printf("%.2f\n", a/b); else puts("Divide by zero!");
break;
default: puts("Unknown operator");
}
return 0;
}
Output:
Input:
Enter a op b (e.g., 3 + 4): 10 / 4
Output:
2.50
EX NO:7- Prime Check (function)
/* Check prime using helper function */
#include <stdio.h>
int is_prime(int n){
if(n<2) return 0; if(n%2==0) return n==2;
for(int i=3;i*i<=n;i+=2) if(n%i==0) return 0;
return 1;
}
int main(void){
int n; printf("Enter n: ");
if (scanf("%d", &n)!=1) return 0;
printf(is_prime(n)?"Prime\n":"Not prime\n");
return 0;
}
Output:
Input:
Enter n: 29
Output:
Prime
EX NO:6 RECURSIVE FACTORIAL (FUNCTION)
/* Recursive factorial */
#include <stdio.h>
unsigned long fact(unsigned int n){ return n<2?1UL:n*fact(n-1); }
int main(void){
unsigned int n; printf("Enter n (0-12): ");
if (scanf("%u", &n)!=1 || n>12) return 0;
printf("%u! = %lu\n", n, fact(n));
return 0;
}
Output:
Input:
Enter n (0-12): 7
Output:
7! = 5040
Unit 3.3 - GCD (function)
/* GCD using Euclid algorithm */
#include <stdio.h>
int gcd(int a,int b){ while(b){ int t=a%b; a=b; b=t;} return a<0?-a:a; }
int main(void){
int a,b; printf("Enter two integers: ");
if (scanf("%d%d", &a,&b)!=2) return 0;
printf("GCD = %d\n", gcd(a,b));
return 0;
}
Output:
Input:
Enter two integers: 72 30
Output:
GCD = 6
Unit 4.1 - Dynamic Array Average (malloc)
/* Dynamic array average (malloc + pointer arithmetic) */
#include <stdio.h>
#include <stdlib.h>
int main(void){
size_t n; printf("Enter count: ");
if (scanf("%zu", &n)!=1 || n==0) return 0;
double *p = (double*)malloc(n*sizeof(double));
if(!p){ puts("Memory error"); return 0; }
for(size_t i=0;i<n;i++){ printf("a[%zu] = ", i); scanf("%lf", p+i); }
double sum=0; for(double *q=p; q<p+n; ++q) sum += *q;
printf("Average = %.3f\n", sum/n);
free(p); return 0;
}
Output:
Input:
Enter count: 3
a[0] = 10
a[1] = 20
a[2] = 30
Output:
Average = 20.000
Unit 4.2 - String Reverse & Vowel Count
/* String reverse and vowel count (pointer walk) */
#include <stdio.h>
#include <string.h>
#include <ctype.h>
int main(void){
char s[200]; printf("Enter a string: ");
// read leftover newline from previous input if any
if(!fgets(s,sizeof s, stdin)) return 0;
if(s[0]=='\n') if(!fgets(s,sizeof s, stdin)) return 0;
size_t len=strcspn(s,"\n"); s[len]='\0';
int vowels=0; for(char *p=s; *p; ++p){
char c=tolower((unsigned char)*p);
if(c=='a'||c=='e'||c=='i'||c=='o'||c=='u') vowels++;
}
// reverse in-place
for(size_t i=0,j=len?len-1:0; i<j; ++i, --j){ char t=s[i]; s[i]=s[j];
s[j]=t; }
printf("Reversed: %s\nVowels: %d\n", s, vowels);
return 0;
}
Output:
Input:
Enter a string: Programming
Output:
Reversed: gnimmargorP
Vowels: 3
Unit 4.3 - Bubble Sort (pointer arithmetic)
/* Bubble sort using pointer arithmetic */
#include <stdio.h>
void bubble(int *a, int n){
for(int i=0;i<n-1;i++){
int swapped=0;
for(int *p=a; p<a+(n-1-i); ++p){
if(*p>*(p+1)){ int t=*p; *p=*(p+1); *(p+1)=t; swapped=1; }
}
if(!swapped) break;
}
}
int main(void){
int n; printf("Enter n: "); if(scanf("%d", &n)!=1||n<=0) return 0;
int a[1000]; if(n>1000) n=1000; for(int i=0;i<n;i++){ printf("a[%d] = ", i);
scanf("%d", &a[i]); }
bubble(a,n); printf("Sorted: ");
for(int i=0;i<n;i++) printf("%d ", a[i]); puts("");
return 0;
}
Output:
Input:
Enter n: 5
a[0] = 64
a[1] = 25
a[2] = 12
a[3] = 22
a[4] = 11
Output:
Sorted: 11 12 22 25 64
Unit 5 - Structures & Unions (Employee example)
/* Employee example using union for pay type */
#include <stdio.h>
#include <string.h>
typedef union { double monthly_salary; struct { double hourly_rate; int hours; }
contract; } Pay;
typedef struct { int id; char name[40]; char type; Pay pay; } Employee;
double compute_pay(const Employee *e){
if(e->type=='S') return e->pay.monthly_salary;
if(e->type=='C') return e->pay.contract.hourly_rate * e->pay.contract.hours;
return 0.0;
}
int main(void){
Employee e={0}; e.id=101; strcpy(e.name,"Arun");
e.type='C'; e.pay.contract.hourly_rate=500.0; e.pay.contract.hours=160;
printf("%s earns %.2f this month\n", e.name, compute_pay(&e));
e.type='S'; e.pay.monthly_salary=75000.0;
printf("If salaried: %.2f\n", compute_pay(&e));
return 0;
}
Output:
Output:
Arun earns 80000.00 this month
If salaried: 75000.00
Unit 6.1 - Write & Read Text File
/* Write and read text file */
#include <stdio.h>
int main(void){
FILE *fp=fopen("notes.txt","w"); if(!fp){perror("open"); return 1;}
fputs("Hello file!\nC file I/O demo.\n", fp); fclose(fp);
fp=fopen("notes.txt","r"); if(!fp){perror("open"); return 1;}
char line[128]; while(fgets(line,sizeof line, fp)) printf("%s", line);
fclose(fp);
return 0;
}
Output:
Output (writes notes.txt and then prints):
Hello file!
C file I/O demo.
Unit 6.2 - Binary Copy (file copy)
/* Binary copy: usage: ./copy src dst */
#include <stdio.h>
int main(int argc, char **argv){
if(argc<3){ puts("Usage: copy src dst"); return 0; }
FILE *in=fopen(argv[1],"rb"), *out=fopen(argv[2],"wb");
if(!in||!out){ perror("open"); return 1; }
unsigned char buf[4096]; size_t n;
while((n=fread(buf,1,sizeof buf,in))>0) fwrite(buf,1,n,out);
fclose(in); fclose(out); puts("Copied.");
return 0;
}
Output:
Sample run:
$ ./copy image.jpg image_copy.jpg
Copied.
Unit 6.3 - Store & Load Records (binary struct I/O)
/* Store & load records (binary struct I/O) */
#include <stdio.h>
#include <string.h>
typedef struct { int roll; char name[32]; float cgpa; } Student;
int main(void){
Student s; FILE *fp=fopen("students.dat","ab+"); if(!fp){perror("open");
return 1;}
// append one record
printf("Enter roll name cgpa: ");
if(scanf("%d %31s %f", &s.roll, s.name, &s.cgpa)==3){ fwrite(&s,sizeof
s,1,fp); }
rewind(fp);
// list all records
printf("\n-- All Records --\n");
while(fread(&s,sizeof s,1,fp)==1){ printf("%d\t%-10s\t%.2f\n", s.roll,
s.name, s.cgpa); }
fclose(fp); return 0;
}
Output:
Sample interaction:
Enter roll name cgpa: 1 Alice 8.5
-- All Records --
1 Alice 8.50
Unit 7.1 - Quadratic Roots (math.h)
/* Quadratic roots using math.h */
#include <stdio.h>
#include <math.h>
int main(void){
double a,b,c; printf("Enter a b c: ");
if(scanf("%lf%lf%lf", &a,&b,&c)!=3 || a==0){ puts("Invalid"); return 0; }
double d=b*b-4*a*c;
if(d>0){ double r1=(-b+sqrt(d))/(2*a), r2=(-b-sqrt(d))/(2*a); printf("Real &
distinct: %.4f %.4f\n", r1,r2);}
else if(d==0){ double r=-b/(2*a); printf("Real & equal: %.4f\n", r);}
else { double real=-b/(2*a), imag=sqrt(-d)/(2*a); printf("Complex: %.4f ±
%.4fi\n", real, imag);}
return 0;
}
Output:
Input:
Enter a b c: 1 -3 2
Output:
Real & distinct: 2.0000 1.0000
Unit 7.2 - Character Classification (ctype.h)
/* Character classification using ctype.h */
#include <stdio.h>
#include <ctype.h>
#include <string.h>
int main(void){
char s[200]; printf("Enter text: ");
if(!fgets(s,sizeof s, stdin)) return 0;
if(s[0]=='\n') if(!fgets(s,sizeof s, stdin)) return 0;
int letters=0, digits=0, spaces=0, others=0;
for(size_t i=0;i<strlen(s);++i){ unsigned char c=s[i];
if(isalpha(c)) letters++; else if(isdigit(c)) digits++;
else if(isspace(c)) spaces++; else others++;
}
printf("letters=%d digits=%d spaces=%d others=%d\n",
letters,digits,spaces,others);
return 0;
}
Output:
Input:
Enter text: Hello 123!
Output:
letters=5 digits=3 spaces=1 others=1
Unit 7.3 - User-defined Library (mylib.h, mylib.c, main.c)
/* mylib.h */
#ifndef MYLIB_H
#define MYLIB_H
int sum_array(const int *a, int n);
#endif
/* mylib.c */
int sum_array(const int *a, int n){ int s=0; for(int i=0;i<n;i++) s+=a[i];
return s; }
/* main.c - using mylib */
#include <stdio.h>
#include "mylib.h"
int main(void){ int a[]={1,2,3,4,5};
printf("Sum = %d\n", sum_array(a,5));
return 0; }
Sample Input/Output & Compile instructions:
Compile:
cc -c mylib.c && cc main.c mylib.o -o app
Run:
Sum = 15
Unit 8.1 - Project: Student Management System
/* Project 1: Student Management (add/list/search) - students.dat (binary) */
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
typedef struct { int roll; char name[32]; float cgpa; } Student;
void add_record(void){
FILE *fp=fopen("students.dat","ab"); if(!fp){perror("open"); return;}
Student s; printf("Enter roll name cgpa: ");
if(scanf("%d %31s %f", &s.roll, s.name, &s.cgpa)==3) fwrite(&s,sizeof
s,1,fp);
fclose(fp);
}
void list_records(void){
FILE *fp=fopen("students.dat","rb"); if(!fp){puts("No data"); return;}
Student s; printf("Roll\tName\t\tCGPA\n");
while(fread(&s,sizeof s,1,fp)==1) printf("%d\t%-10s\t%.2f\n", s.roll,
s.name, s.cgpa);
fclose(fp);
}
void search_roll(void){
int key; printf("Enter roll to search: "); scanf("%d", &key);
FILE *fp=fopen("students.dat","rb"); if(!fp){puts("No data"); return;}
Student s; int found=0; while(fread(&s,sizeof s,1,fp)==1){ if(s.roll==key){
printf("Found: %d %s %.2f\n", s.roll, s.name, s.cgpa); found=1;
break; }}
if(!found) puts("Not found"); fclose(fp);
}
int main(void){
for(;;){
int ch; puts("\n1.Add 2.List 3.Search 0.Exit");
if(scanf("%d", &ch)!=1) break;
switch(ch){ case 1:add_record();break; case 2:list_records();break; case
3:search_roll();break; case 0:return 0; default: puts("Invalid"); }
}
return 0;
}
Output:
Sample run (user input shown):
1
Enter roll name cgpa: 1 Alice 9.0
1
Enter roll name cgpa: 2 Bob 8.2
2
Output:
Roll Name CGPA
1 Alice 9.00
2 Bob 8.20
Unit 8.2 - Project: Simple Inventory Management
/* Project 2: Simple Inventory (add/list/update qty) - inventory.bin */
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
typedef struct { int id; char name[40]; int qty; double price; } Item;
void add_item(void){
FILE *fp=fopen("inventory.bin","ab"); if(!fp){perror("open"); return;}
Item it; printf("Enter id name qty price: ");
if(scanf("%d %39s %d %lf", &it.id, it.name, &it.qty, &it.price)==4)
fwrite(&it,sizeof it,1,fp);
fclose(fp);
}
void list_items(void){
FILE *fp=fopen("inventory.bin","rb"); if(!fp){puts("No data"); return;}
Item it; printf("ID\tName\tQty\tPrice\n");
while(fread(&it,sizeof it,1,fp)==1) printf("%d\t%-10s\t%d\t%.2f\n",
it.id,it.name,it.qty,it.price);
fclose(fp);
}
void update_qty(void){
int key, delta; printf("Enter id and delta (+/-): "); scanf("%d %d", &key,
&delta);
FILE *fp=fopen("inventory.bin","rb+"); if(!fp){puts("No data"); return;}
Item it; while(fread(&it,sizeof it,1,fp)==1){ if(it.id==key){ it.qty +=
delta; if(it.qty<0) it.qty=0; fseek(fp, -(long)sizeof it, SEEK_CUR);
fwrite(&it,sizeof it,1,fp); printf("Updated.\n"); fclose(fp); return; } }
puts("Not found"); fclose(fp);
}
int main(void){
for(;;){
int ch; puts("\n1.Add 2.List 3.UpdateQty 0.Exit");
if(scanf("%d", &ch)!=1) break;
switch(ch){ case 1:add_item();break; case 2:list_items();break; case
3:update_qty();break; case 0:return 0; default: puts("Invalid"); }
}
return 0;
}
Output:
Sample run (user input shown):
1
Enter id name qty price: 101 Pen 50 1.20
1
Enter id name qty price: 102 Pencil 100 0.50
2
Output:
ID Name Qty Price
101 Pen 50 1.20
102 Pencil 100 0.50
3
Enter id and delta (+/-): 101 -10
Updated.