C Programming Lab Report 1: Introduction to C & Programs

C Programming Lab Report 1: Introduction to C

Introduction to C

Lab Report Information

Lab No.: 1

Title: Programming & Introduction to C

Course: Computer Programming (CT 101), I Year I Part

Credit: Important Notes

C Programming Lab Report: Theory

This section of the C Programming Lab Report covers the fundamental concepts, important keywords, and terms used in C programming.

Algorithm & Flowchart

An algorithm is a finite set of ordered steps to solve a problem. A flowchart is a graphical representation of an algorithm, using symbols like ovals (start/stop), parallelograms (input/output), and rectangles (processing).

Source Code, Object Code, and Execution

Source code is a program written in a high-level language (e.g., a `.c` file). A compiler translates this into machine-readable object code. A linker then combines object code with necessary libraries to create a single executable file (`.exe`).

Testing & Debugging

This is the process of finding and removing errors (bugs) from a program. Common errors include syntax errors (language rule violations), run-time errors (occur during execution), and logical errors (flaws in the program’s logic).

Core C Concepts

  • Keywords: Reserved words with predefined meanings (e.g., `int`, `float`, `if`, `while`).
  • Data Types: Define the type of data a variable can hold (e.g., `int`, `char`, `float`).
  • Variables: Named memory locations to store data that can change.
  • Constants: Fixed values that do not change during program execution.
  • Operators: Symbols that perform operations (e.g., `+`, `-`, `*`, `/`, `==`, `&&`).
  • Header Files: Files with a `.h` extension (e.g., `stdio.h`) that contain declarations for standard library functions. For more details, see this external resource on header files.

C Programming Lab Report: Programs

1. Program to Display a Message

Question:

Type the following program to see the output.

Program:

#include <stdio.h>
#include <conio.h>

void main() {
    clrscr(); // Clears the screen
    printf("This is my first C program");
    getch(); // Holds the output screen
}

Output:

This is my first C program

Algorithm:

  1. Start
  2. Print the message “This is my first C program”.
  3. Stop

Flowchart:

Start
Display “This is my first C program”
Stop

2. Program to Add Two Integers

Question:

Write a program to add two integer numbers.

Program:

#include <stdio.h>
#include <conio.h>

void main() {
    int a, b, s;
    printf("Enter two numbers: ");
    scanf("%d %d", &a, &b);
    s = a + b;
    printf("\nThe sum of the given numbers is %d", s);
    getch();
}

Output:

Enter two numbers: 10 25
The sum of the given numbers is 35

Algorithm:

  1. Start
  2. Declare integer variables a, b, s.
  3. Read values for a and b.
  4. Calculate s = a + b.
  5. Display the value of s.
  6. Stop

Flowchart:

Start
Input a, b
s = a + b
Display s
Stop

3. Program to Show Address and Size of Variables

Question:

Type the following program and see the output.

Program:

#include <stdio.h>
#include <conio.h>

void main(void) {
    clrscr();
    int a, s, b;
    float p;

    printf("Address of a is %x\n", &a);
    printf("Address of b is %x\n", &b);
    printf("Memory space of a is %d bytes\n", sizeof(a));
    printf("Size of p is %d bytes\n", sizeof(p));
    printf("Size of 1.5 is %d bytes\n", sizeof(1.5));
    printf("Size of float data type is %d bytes", sizeof(float));

    getch();
}

Output:

Address of a is ffde
Address of b is ffdc
Memory space of a is 2 bytes
Size of p is 4 bytes
Size of 1.5 is 8 bytes
Size of float data type is 4 bytes

(Note: Addresses and sizes may vary based on the compiler and system architecture.)

Algorithm:

  1. Start
  2. Declare integer variables a, b, s and a float variable p.
  3. Print the memory address of variable a.
  4. Print the memory address of variable b.
  5. Print the size in bytes of variable a.
  6. Print the size in bytes of variable p.
  7. Print the size in bytes of the literal value 1.5.
  8. Print the size in bytes of the float data type.
  9. Stop

Flowchart:

Start
Declare int a, s, b; float p;
Display Address of a, b
Display Size of a, p, 1.5, float
Stop

4. Program to Calculate Area and Circumference of a Circle

Question:

Write a program to calculate the area and circumference of a circle of radius r.

Program:

#include <stdio.h>
#include <conio.h>

void main() {
    clrscr();
    float a, r, c;
    printf("\nEnter radius: ");
    scanf("%f", &r);
    a = 3.14 * r * r;
    c = 2 * 3.14 * r;
    printf("\nThe area of the circle is: %f", a);
    printf("\nThe circumference of the circle is: %f", c);
    getch();
}

Output:

Enter radius: 5
The area of the circle is: 78.500000
The circumference of the circle is: 31.400000

Algorithm:

  1. Start
  2. Declare float variables r, a, c.
  3. Read the value of radius r.
  4. Calculate area a = 3.14 * r * r.
  5. Calculate circumference c = 2 * 3.14 * r.
  6. Display area a and circumference c.
  7. Stop

Flowchart:

Start
Input radius (r)
Calculate a = 3.14*r*r
Calculate c = 2*3.14*r
Display area (a),
circumference (c)
Stop

5. Program to Calculate Volume of a Sphere

Question:

Write a program to calculate the volume of a sphere of radius r.

Program:

#include <stdio.h>
#include <conio.h>
void main() {
    clrscr();
    float v, r;
    printf("\nEnter radius of sphere: ");
    scanf("%f", &r);
    v = (4.0 / 3.0) * 3.14 * r * r * r;
    printf("\nThe volume of the sphere is %.2f", v);
    getch();
}

Output:

Enter radius of sphere: 3
The volume of the sphere is 113.04

Algorithm:

  1. Start
  2. Declare float variables v, r.
  3. Read the value of r.
  4. Calculate v = (4.0/3.0) * 3.14 * r * r * r.
  5. Display the value of v.
  6. Stop

Flowchart:

Start
Input radius (r)
v = (4.0/3.0) * 3.14 * r * r * r
Display volume (v)
Stop

6. Program to Calculate Simple Interest

Question:

Write a program to calculate simple interest. Read values of P, T, R from the user.

Program:

#include <stdio.h>
#include <conio.h>
void main() {
    float p, t, r, s;
    printf("\nEnter principal, rate, time: ");
    scanf("%f %f %f", &p, &r, &t);
    s = (p * t * r) / 100;
    printf("\nSimple interest = %.2f", s);
    getch();
}

Output:

Enter principal, rate, time: 1000 5 2
Simple interest = 100.00

Algorithm:

  1. Start
  2. Declare float variables p, t, r, s.
  3. Read values for p, r, and t.
  4. Calculate s = (p * t * r) / 100.
  5. Display s.
  6. Stop

Flowchart:

Start
Input p, r, t
s = (p * t * r) / 100
Display s
Stop

7. Program to Evaluate an Expression

Question:

Read values of x and y from the user and evaluate the expression v = x² + y² – 100/x.

Program:

#include <stdio.h>
#include <conio.h>
void main() {
    clrscr();
    int x, y;
    double v;
    printf("\nEnter the value of x and y: ");
    scanf("%d %d", &x, &y);
    v = (x * x) + (y * y) - (100.0 / x);
    printf("\nThe output is: %lf", v);
    getch();
}

Output:

Enter the value of x and y: 10 5
The output is: 115.000000

Algorithm:

  1. Start
  2. Declare variables x, y, v.
  3. Read values for x and y.
  4. Calculate v = (x*x) + (y*y) – (100.0 / x).
  5. Display v.
  6. Stop

Flowchart:

Start
Input x, y
v = (x*x) + (y*y) – (100.0 / x)
Display v
Stop

8. Program to Find the Mean of Four Numbers

Question:

Write a program to read 4 integers from the user and display the mean of the numbers.

Program:

#include <stdio.h>
#include <conio.h>
void main() {
    clrscr();
    int a, b, c, d;
    float e;
    printf("Enter 4 different numbers: ");
    scanf("%d %d %d %d", &a, &b, &c, &d);
    e = (a + b + c + d) / 4.0;
    printf("The mean of the given numbers is: %f", e);
    getch();
}

Output:

Enter 4 different numbers: 10 20 30 40
The mean of the given numbers is: 25.000000

Algorithm:

  1. Start
  2. Declare variables a, b, c, d, e.
  3. Read values for a, b, c, d.
  4. Calculate e = (a+b+c+d) / 4.0.
  5. Display e.
  6. Stop

Flowchart:

Start
Input a, b, c, d
e = (a+b+c+d) / 4.0
Display e
Stop

9. Program to Find the Volume of a Parallelepiped

Question:

Write a program to read the length, breadth, and height of a parallelepiped and display the volume.

Program:

#include <stdio.h>
#include <conio.h>
void main() {
    clrscr();
    int l, b, h, v;
    printf("\nEnter length, width, height of parallelepiped: ");
    scanf("%d %d %d", &l, &b, &h);
    v = l * b * h;
    printf("\nThe volume of the parallelepiped is %d", v);
    getch();
}

Output:

Enter length, width, height of parallelepiped: 10 5 2
The volume of the parallelepiped is 100

Algorithm:

  1. Start
  2. Declare variables l, b, h, v.
  3. Read values for l, b, h.
  4. Calculate v = l * b * h.
  5. Display v.
  6. Stop

Flowchart:

Start
Input l, b, h
v = l * b * h
Display v
Stop

10. Program to Calculate Price After Discount

Question:

Write a program to read the price of two pens and five copies and calculate the price after a 20% discount.

Program:

#include <stdio.h>
#include <conio.h>
void main() {
    clrscr();
    float x, y, z, p;
    printf("Enter price of a pen & a copy: ");
    scanf("%f %f", &x, &y);
    z = 2 * x + 5 * y;
    p = z - (z * 20 / 100);
    printf("\nThe price of two pens & five copies after discount is %.2f", p);
    getch();
}

Output:

Enter price of a pen & a copy: 10 20
The price of two pens & five copies after discount is 96.00

Algorithm:

  1. Start
  2. Declare float variables x, y, z, p.
  3. Read price of pen (x) and copy (y).
  4. Calculate total price z = 2*x + 5*y.
  5. Calculate discounted price p = z – (z * 20 / 100).
  6. Display p.
  7. Stop

Flowchart:

Start
Input x, y
z = 2*x + 5*y
p = z – (z * 0.20)
Display p
Stop

11. Program to Convert Time to Seconds

Question:

Write a program to read the time value (hh:mm:ss) and convert it to total seconds and display it.

Program:

#include <stdio.h>
#include <conio.h>
void main() {
    clrscr();
    int h, m, s, t;
    printf("Enter time value (hh mm ss): ");
    scanf("%d %d %d", &h, &m, &s);
    t = (h * 3600) + (m * 60) + s;
    printf("\nThe total time in seconds is: %d", t);
    getch();
}

Output:

Enter time value (hh mm ss): 1 30 15
The total time in seconds is: 5415

Algorithm:

  1. Start
  2. Declare integer variables h, m, s, t.
  3. Read time as hours (h), minutes (m), and seconds (s).
  4. Calculate total seconds t = (h*3600) + (m*60) + s.
  5. Display t.
  6. Stop

Flowchart:

Start
Input h, m, s
t = (h * 3600) + (m * 60) + s
Display t
Stop

12. Program to Read and Display Name and Age

Question:

Write a program to read the name and age of a person and display them.

Program:

#include <stdio.h>
#include <conio.h>
void main() {
    int a;
    char name[20];
    printf("Enter age and name: ");
    scanf("%d %s", &a, name);
    printf("\nYour age is %d", a);
    printf("\nYour name is %s", name);
    getch();
}

Output:

Enter age and name: 25 John
Your age is 25
Your name is John

Algorithm:

  1. Start
  2. Declare an integer ‘a’ and a character array ‘name’.
  3. Read values for ‘a’ and ‘name’.
  4. Display the value of ‘a’.
  5. Display the value of ‘name’.
  6. Stop

Flowchart:

Start
Input age (a), name
Display a, name
Stop

13. Program to Read and Display a Character

Question:

Write a program to read a character and display it.

Program:

#include <stdio.h>
#include <conio.h>
void main() {
    clrscr();
    char ch;
    printf("\nEnter a character: ");
    scanf("%c", &ch);
    printf("\nYou entered: %c", ch);
    getch();
}

Output:

Enter a character: G
You entered: G

Algorithm:

  1. Start
  2. Declare a char variable ch.
  3. Read a character into ch.
  4. Display the character ch.
  5. Stop

Flowchart:

Start
Input character (ch)
Display ch
Stop

Discussion and Conclusion

This C Programming Lab Report provides a comprehensive introduction to the fundamentals of the C programming language. The theory section effectively covers essential building blocks, from the conceptual stages of algorithm and flowchart design to the practical aspects of writing, compiling, and executing code. Key concepts such as data types, variables, operators, and I/O functions were defined and explained.

The practical programming exercises demonstrate the application of these theoretical concepts. The programs range from basic input/output operations to simple mathematical calculations, providing a solid foundation for beginners. Each program in this C Programming Lab Report is accompanied by its corresponding algorithm and flowchart where applicable, reinforcing the importance of structured problem-solving before coding. Through these exercises, the process of translating a logical solution into functional C code is clearly illustrated. In conclusion, this lab successfully covers the initial steps required to understand and write simple programs in C.

Scroll to Top