Unit 1 of 1 · BCA Sem 1

Unit 1: C programming fundamentals to file handling

Problem Solving Techniques Laboratory notes · PTU syllabus (UGSEC2502)

3 min read7 topics11 exam questions
On this page
  1. Unit summary
  2. How to approach every lab program
  3. Basic I/O, ASCII values and data type sizes
  4. Conditional programs
  5. Loop and switch programs
  6. Function programs
  7. Arrays and strings
  8. Pointers, structures, unions and files
  9. Key terms
  10. Quick revision
  11. Important questions

Unit summary

This lab turns the theory of Problem Solving Techniques into working C programs. You progress from simple input/output and arithmetic to decisions, loops, functions, arrays, strings, pointers, structures and finally files.

The notes below give the logic and a model program for each family of lab exercises, plus the viva questions examiners ask most often.

After this unit you can

  • Write, compile and run C programs and fix common errors
  • Use conditions, loops and switch to solve classic lab problems
  • Use functions, arrays, strings, pointers and structures in programs
  • Read and write data files

PTU syllabus topics

  • Basic I/O and arithmetic programs
  • ASCII value and data type size programs
  • conditional programs (largest/greater of numbers, even/odd, prime check)
  • loop-based programs (multiplication tables, simple calculator using switch-case)
  • function-based programs (largest of three, factorial, first 20 squares)
  • matrix addition
  • string functions (length, copy, compare, reverse)
  • pointer-based programs
  • structure/union memory demonstration
  • student I-card using structures
  • file read/write programs (employee details)
ProcessHow to write every lab program
  1. 1Read the aim

    Note inputs and expected output

  2. 2Write the logic

    Flowchart or pseudocode first

  3. 3Code and compile

    Fix syntax errors

  4. 4Test

    Normal, boundary and invalid inputs

  5. 5Record

    Aim, code, output and viva notes

1

Topic 1

How to approach every lab program

ProcessLab workflow
  1. 1

    Understand the problem

    Inputs and expected output

  2. 2

    Write the algorithm or flowchart

  3. 3

    Type the program

    Indent and comment it

  4. 4

    Compile

    gcc prog.c -o prog

  5. 5

    Test

    Normal, boundary and invalid inputs

  6. 6

    Record in the practical file

    Aim, code, output

Exam tip

In the practical exam you are marked on the program, the output and the viva. Keep a neat practical file with the aim, program and output for every experiment.

2

Topic 2

Basic I/O, ASCII values and data type sizes

c#include <stdio.h>
int main() {
    char ch;
    printf("Enter a character: ");
    scanf("%c", &ch);
    printf("ASCII value of %c = %d\n", ch, ch);
    printf("int: %zu, float: %zu, double: %zu, char: %zu bytes\n",
           sizeof(int), sizeof(float), sizeof(double), sizeof(char));
    return 0;
}

Printing a char with %d shows its ASCII code. sizeof gives the size of a type in bytes.

3

Topic 3

Conditional programs

  • Even or odd: if (n % 2 == 0) → even, else odd.
  • Largest of three: compare with nested if or max = (a > b) ? (a > c ? a : c) : (b > c ? b : c);
  • Prime check: a number n > 1 is prime if no number from 2 to √n divides it.
cint n, i, isPrime = 1;
scanf("%d", &n);
if (n < 2) isPrime = 0;
for (i = 2; i * i <= n; i++)
    if (n % i == 0) { isPrime = 0; break; }
printf(isPrime ? "Prime" : "Not prime");
4

Topic 4

Loop and switch programs

  • Multiplication table: for (i = 1; i <= 10; i++) printf("%d x %d = %d\n", n, i, n * i);
  • Simple calculator with switch:
cchar op; float a, b;
scanf("%f %c %f", &a, &op, &b);
switch (op) {
    case '+': printf("%.2f", a + b); break;
    case '-': printf("%.2f", a - b); break;
    case '*': printf("%.2f", a * b); break;
    case '/': if (b != 0) printf("%.2f", a / b); else printf("Divide by zero"); break;
    default: printf("Invalid operator");
}
5

Topic 5

Function programs

  • Factorial using a function (iterative or recursive), largest of three using a function, and first 20 squares using a function that returns n × n.
clong factorial(int n) {
    long f = 1;
    for (int i = 2; i <= n; i++) f *= i;
    return f;
}
6

Topic 6

Arrays and strings

  • Matrix addition: read two m × n matrices and add them with nested loops: c[i][j] = a[i][j] + b[i][j];
  • String functions: use strlen, strcpy, strcmp and a reverse (with strrev or a manual swap loop).
c/* Reverse a string without strrev */
int len = strlen(s);
for (int i = 0; i < len / 2; i++) {
    char t = s[i]; s[i] = s[len - 1 - i]; s[len - 1 - i] = t;
}
7

Topic 7

Pointers, structures, unions and files

  • Pointer program: print a variable's value and address using & and *; swap with pointers.
  • Structure vs union memory: print sizeof of a struct and a union with the same members to show the difference.
  • Student I-card: a structure with name, roll number, course and address, printed in an ID-card layout.
  • Employee file: write employee records with fprintf to a file and read them back with fscanf.
cFILE *fp = fopen("emp.txt", "w");
fprintf(fp, "%d %s %.2f\n", id, name, salary);
fclose(fp);

Key terms

Compiler
Software that translates C source code into machine code
Syntax error
A mistake in the rules of the language, found at compile time
Logical error
A mistake in the logic, giving wrong output without an error message
sizeof
An operator that returns the size of a type or variable in bytes
Practical file
The record of aim, program and output for each experiment

Quick revision

  • %d on a char prints its ASCII value.
  • Test primes only up to √n.
  • Always add break in each switch case and check divide by zero.
  • Reverse a string by swapping from both ends.
  • sizeof(struct) ≥ sum of members; sizeof(union) = largest member.

Important exam questions

Practice questions written to the PTU exam pattern for this unit's syllabus: short answers (Section A style) and long answers (Sections B and C style).

Short-answer questions

  1. Q1.What is the difference between a syntax error and a logical error?
  2. Q2.How do you print the ASCII value of a character?
  3. Q3.Why do we check divisors only up to √n in a prime program?
  4. Q4.What is the output of sizeof(char)?
  5. Q5.Why is fclose() needed?
  6. Q6.How is a structure different from a union in memory?

Long-answer questions

  1. Q1.Write a program to check whether a number is prime and explain its logic.
  2. Q2.Write a menu-driven calculator using switch-case.
  3. Q3.Write a program to add two matrices and display the result.
  4. Q4.Write a program to create a student I-card using a structure.
  5. Q5.Write a program to write and read employee details using a file.

Stuck on this unit?

Message SBS on WhatsApp for help with Problem Solving Techniques Laboratory, or to ask about studying BCA at Synetic.

WhatsApp us