The full C lab notebook in one place — every classic program (Part A, B and C) with code, how it works, and sample output, plus a quick reference for I/O and operators.
Every C lab program in one place. Each one follows the same rhythm — read the input, apply the
logic, print the result, show a sample run — so once you've got the pattern, they're all easy.
Use the On this page list to jump straight to any program.
Classic Turbo C style is used throughout (void main, clrscr, getch from <conio.h>).
Modern standard C uses int main(void) with return 0; and drops <conio.h> — everything
else is identical.
#include <stdio.h>#include <conio.h>void main(){ int a, b, temp; clrscr(); printf("Enter two values: "); scanf("%d %d", &a, &b); temp = a; a = b; b = temp; printf("\nAfter swapping: a = %d, b = %d", a, b); getch();}
How it works:temp holds a's value safely while b is copied into a, then the saved
value goes into b. (You can also swap with arithmetic: a=a+b; b=a-b; a=a-b;.)
#include <stdio.h>#include <conio.h>void main(){ int a, b, c; clrscr(); printf("Enter three numbers: "); scanf("%d %d %d", &a, &b, &c); if (a >= b && a >= c) printf("\n%d is the largest", a); else if (b >= c) printf("\n%d is the largest", b); else printf("\n%d is the largest", c); getch();}
How it works:a wins only if it beats both others; otherwise the larger of b and c.
#include <stdio.h>#include <conio.h>void main(){ float r, area; float pi = 3.14; clrscr(); printf("Enter the radius: "); scanf("%f", &r); area = pi * r * r; printf("\nArea of circle = %.2f", area); getch();}
#include <stdio.h>#include <conio.h>void main(){ int n, i; long fact = 1; clrscr(); printf("Enter a number: "); scanf("%d", &n); for (i = 1; i <= n; i++) fact = fact * i; printf("\nFactorial of %d = %ld", n, fact); getch();}
How it works: multiply fact by every number from 1 to n. long because factorials grow fast.
#include <stdio.h>#include <conio.h>void main(){ int n, i, sum = 0; clrscr(); printf("Enter N: "); scanf("%d", &n); for (i = 1; i <= n; i++) sum = sum + i; printf("\nSum of first %d natural numbers = %d", n, sum); getch();}
How it works: add 1, 2, 3 ... up to n. (Shortcut formula: n * (n + 1) / 2.)
#include <stdio.h>#include <conio.h>void main(){ int n, i, a = 0, b = 1, c; clrscr(); printf("How many terms? "); scanf("%d", &n); printf("\n%d %d", a, b); for (i = 3; i <= n; i++) { c = a + b; printf(" %d", c); a = b; b = c; } getch();}
How it works: each term is the sum of the previous two; slide the window forward each step.
#include <stdio.h>#include <conio.h>void main(){ int low, high, i, j, flag; clrscr(); printf("Enter the range (low high): "); scanf("%d %d", &low, &high); printf("\nPrimes: "); for (i = low; i <= high; i++) { if (i < 2) continue; flag = 1; for (j = 2; j <= i / 2; j++) { if (i % j == 0) { flag = 0; break; } } if (flag == 1) printf("%d ", i); } getch();}
How it works: for each number in the range, test if anything from 2 to i/2 divides it; if
nothing does, it's prime.
Enter the range (low high): 10 30Primes: 11 13 17 19 23 29
#include <stdio.h>#include <conio.h>int gcd(int a, int b){ if (b == 0) return a; return gcd(b, a % b); /* recursive call */}void main(){ int a, b; clrscr(); printf("Enter two numbers: "); scanf("%d %d", &a, &b); printf("\nGCD = %d", gcd(a, b)); getch();}
How it works: Euclid's rule — gcd(a, b) = gcd(b, a % b), stopping when b becomes 0. The
function calls itself with smaller values each time (recursion).
#include <stdio.h>#include <conio.h>void main(){ int a[50], n, i, key, found = 0; clrscr(); printf("How many numbers? "); scanf("%d", &n); printf("Enter %d numbers: ", n); for (i = 0; i < n; i++) scanf("%d", &a[i]); printf("Enter the number to search: "); scanf("%d", &key); for (i = 0; i < n; i++) { if (a[i] == key) { printf("\nFound at position %d", i + 1); found = 1; break; } } if (found == 0) printf("\nNot found"); getch();}
How it works: check each element one by one until the key is found.
How many numbers? 5Enter 5 numbers: 10 20 30 40 50Enter the number to search: 30Found at position 3
#include <stdio.h>#include <conio.h>void main(){ int a[10][10], b[10][10], c[10][10]; int r, col, i, j; clrscr(); printf("Enter rows and columns: "); scanf("%d %d", &r, &col); printf("Enter first matrix:\n"); for (i = 0; i < r; i++) for (j = 0; j < col; j++) scanf("%d", &a[i][j]); printf("Enter second matrix:\n"); for (i = 0; i < r; i++) for (j = 0; j < col; j++) scanf("%d", &b[i][j]); for (i = 0; i < r; i++) for (j = 0; j < col; j++) c[i][j] = a[i][j] + b[i][j]; printf("\nSum:\n"); for (i = 0; i < r; i++) { for (j = 0; j < col; j++) printf("%d ", c[i][j]); printf("\n"); } getch();}
How it works: add the two matrices cell by cell — c[i][j] = a[i][j] + b[i][j] — using
nested loops over rows and columns.
Enter rows and columns: 2 2First: 1 2 3 4 Second: 5 6 7 8Sum:6 810 12
#include <stdio.h>#include <conio.h>void swapByValue(int x, int y) /* gets copies */{ int t = x; x = y; y = t;}void swapByAddress(int *x, int *y) /* gets addresses */{ int t = *x; *x = *y; *y = t;}void main(){ int a = 10, b = 20; clrscr(); swapByValue(a, b); printf("After pass by value: a = %d, b = %d", a, b); swapByAddress(&a, &b); printf("\nAfter pass by address: a = %d, b = %d", a, b); getch();}
How it works:pass by value sends copies, so the originals don't change. Pass by address
sends pointers (&a), so the function edits the real variables through *x.
After pass by value: a = 10, b = 20After pass by address: a = 20, b = 10