Activity: C Programming II Lab

33 exercises across functions, recursion, pointers, structures, unions, and file handling

Grade XII • Computer Science Lab • Capstone ⏱️ ~45–50 min

Brief Intro — C Programming II Lab

33 exercises, seven walls of C skills — and at the end, you build your own program. This lab walks you through all 33 official C Programming II exercises: from basic functions to recursive algorithms, from pointer memory tricks to file storage. Each task builds on what you learned in the theory pair, and you'll predict outputs before checking your work.

The final Capstone Build asks you to design your own small C program — a menu-driven student-mark analyzer, a calculator with functions, or a person-record system with file save-load — combining several C-II concepts into something you can call your own.

Part 1 Pure Functions

Basic functions without decisions or loops.

Interactive Simulator: Output Predictor

Predict the output of C functions, then verify your answer against pre-computed results.

int add(int a, int b) { return a + b; } printf("%d", add(5, 3));

Task 1: Hello and Arithmetic

Write C functions for hello world and arithmetic operations.

Lab 1: Function that displays "Hello world" when invoked
Lab 2: Functions to perform all arithmetic operations (+, -, *, /, %) on two user-entered integers and display results
Trace: For inputs 5 and 3, expect: Sum=8, Diff=2, Product=15, Quotient=1, Remainder=2
Check Answer

Answer: void hello() { printf("Hello world"); } For arithmetic: return a+b for sum, a-b for difference, a*b for product, a/b for quotient, a%b for remainder.

Task 2: Interest, Areas, and Conversions

Write C functions for interest calculation, geometric formulas, and temperature conversion.

Lab 3: Calculate Interest I = PTR/100 from user input P, T, R
Lab 4: Calculate area of rectangle (length × width)
Lab 5: Calculate area of circle (π × r²)
Lab 6: Calculate volume of cube (side³)
Lab 7: Convert Fahrenheit to Celsius: C = (F - 32) × 5/9
Lab 8: Calculate square of a number (n²)
Check Answer

Answer: Interest: return (P*T*R)/100. Rectangle area: return length*width. Circle area: return 3.14159*r*r. Cube volume: return side*side*side. Fahrenheit to Celsius: return (F-32)*5/9. Square: return n*n.

Part 2 Functions with Decisions

Functions using if-else and switch for conditional logic.

Task 3: Comparisons with if-else

Write C functions for comparison and classification.

Lab 9: Find greater number between two user-entered numbers using if-else
Lab 10: Test whether an integer is negative or positive (if n < 0: negative, else: positive)
Lab 11: Find if an entered integer is odd or even (if n % 2 == 0: even, else: odd)
Lab 12: Find greatest number among three user-entered numbers using nested if-else
Check Answer

Answer: Greater: if (a > b) return a; else return b. Negative/positive: if (n < 0) return "negative"; else return "positive". Odd/even: if (n % 2 == 0) return "even"; else return "odd". Greatest of three: compare a with b, then compare result with c.

Task 4: Switch Case Statements

Write C functions using switch for multi-way branching.

Lab 13: Display days of week with user input (1-7) using switch: 1=Monday, 2=Tuesday, ..., 7=Sunday
Lab 14: Check whether alphabet is vowel or consonant using switch: case 'a','e','i','o','u' → vowel, default → consonant
Check Answer

Answer: Days: switch(day) { case 1: printf("Monday"); break; ... case 7: printf("Sunday"); }. Vowel: switch(ch) { case 'a': case 'e': case 'i': case 'o': case 'u': printf("vowel"); break; default: printf("consonant"); }

Part 3 Functions with Loops

Functions using for, while, and do-while for repetition.

Task 5: Counting Loops

Write C functions for counting and summing with loops.

Lab 15: Display "Hello world" N times where N is user-entered integer (for loop)
Lab 16: Print natural numbers up to 50 (for i=1 to 50)
Lab 17: Print and find sum of natural numbers between 10 and 20 (for i=10 to 20, sum += i)
Lab 18: Print and find sum of odd numbers between 50 and 100 (for i=50 to 100, if i%2!=0 then sum += i)
Check Answer

Answer: Hello N times: for(i=0; i

Task 6: Digit Work

Write C functions for reversing numbers and checking palindromes.

Lab 19: Find reverse of a number (extract digits using % 10, build reverse with rev = rev*10 + digit)
Lab 20: Check whether entered number is palindrome (if original == reverse: palindrome, else: not palindrome)
Check Answer

Answer: Reverse: while(n>0) { digit = n%10; rev = rev*10 + digit; n = n/10; }. Palindrome: store original, compute reverse, if(original == rev) then palindrome.

Task 7: Arrays

Write C programs for array operations.

Lab 21: Read marks of 5 students, count how many passed (>=40) and failed (<40)
Lab 22: Enter ten integers into array, sort and display in ascending order (bubble sort: compare adjacent, swap if out of order)
Check Answer

Answer: Pass/fail: for(i=0; i<5; i++) { scanf("%d", &marks[i]); if(marks[i]>=40) pass++; else fail++; }. Bubble sort: for(i=0; i<9; i++) for(j=0; j<9-i; j++) if(arr[j] > arr[j+1]) swap.

Part 4 Recursive Functions

Functions that call themselves for elegant problem-solving.

Task 8: Factorial and Fibonacci

Write recursive C functions for factorial and Fibonacci series.

Lab 23: Recursive factorial: if(n <= 1) return 1; else return n * factorial(n-1)
Lab 24: Recursive Fibonacci series of n terms: first two terms 0, 1; each subsequent term = sum of previous two

Check Answer

Answer: Factorial: int factorial(int n) { if(n <= 1) return 1; return n * factorial(n-1); }. Fibonacci: int fib(int n) { if(n <= 1) return n; return fib(n-1) + fib(n-2); }. Call fib(0), fib(1), ..., fib(n-1) for series.

Part 5 Pointers

Variables that hold memory addresses for direct memory manipulation.

Task 9: Pointer Basics

Write C programs for pointer declaration, assignment, and NULL.

Lab 25: Define pointer variable, assign address of variable to pointer, access value using pointer (int *ptr; ptr = &var; printf("%d", *ptr);)
Lab 26: Demonstrate NULL value assigned to pointer (int *ptr = NULL; printf("%p", ptr); prints 0 or NULL)
Check Answer

Answer: Pointer declare: int *ptr; Assign: ptr = &var; Access: printf("%d", *ptr); NULL: int *ptr = NULL; if(ptr == NULL) printf("Pointer is NULL");

Task 10: Swap by Value vs Address

Write C functions to swap integers using call by value and call by address.

Lab 27: Swap by value: function receives copies of values, caller's variables unchanged
Lab 28: Swap by address: function receives pointers (addresses), caller's variables actually swapped (void swap(int *a, int *b) { int temp = *a; *a = *b; *b = temp; })
Check Answer

Answer: Call by value: void swap(int a, int b) { int temp = a; a = b; b = temp; } — caller unchanged. Call by address: void swap(int *a, int *b) { int temp = *a; *a = *b; *b = temp; } — caller swapped via addresses.

Part 6 Structures, Unions & Files

User-defined data types and persistent data storage.

Task 11: Structures and Unions

Write C programs for struct Person, distance addition, and sizeof comparison.

Lab 29: Define struct Person with name, id, salary; store and display values (struct Person p; strcpy(p.name, "John"); p.id = 101; p.salary = 50000;)
Lab 30: Add two distances in feet+inches with carry (if inches >= 12, add 1 to feet, subtract 12 from inches)
Lab 31: Demonstrate struct vs union memory difference using sizeof (struct size = sum of all members; union size = size of largest member)
Check Answer

Answer: Struct Person: struct Person { char name[50]; int id; float salary; }; Access with p.name, p.id, p.salary. Distance carry: if(inches >= 12) { feet++; inches -= 12; }. Sizeof: printf("Struct: %d, Union: %d", sizeof(struct), sizeof(union));

Task 12: File Handling

Write C programs to write integer to file and read it back.

Lab 32: Write user-input integer to file (FILE *fp = fopen("data.txt", "w"); fprintf(fp, "%d", num); fclose(fp);)
Lab 33: Read integer from file and display (FILE *fp = fopen("data.txt", "r"); fscanf(fp, "%d", &num); printf("%d", num); fclose(fp);)
Check Answer

Answer: Write: FILE *fp = fopen("data.txt", "w"); if(fp != NULL) { fprintf(fp, "%d", num); fclose(fp); }. Read: FILE *fp = fopen("data.txt", "r"); if(fp != NULL) { fscanf(fp, "%d", &num); printf("%d", num); fclose(fp); }

Capstone Build — Your Own C Program

Design and implement your own small C program combining several C-II concepts. Choose one option:

Option 1: Menu-driven student-mark analyzer — enter marks, calculate average, determine grade, save/load from file
Option 2: Calculator with functions — menu-driven calculator with add, subtract, multiply, divide, square, cube using separate functions
Option 3: Person-record system with struct + file — define Person struct, add records, display all, save to file, load from file

Combine: functions, decisions, loops, pointers/structs, and file handling. Make it personalized — choose your own feature names and display style.

Ready to test your knowledge?

Take the Assessment →