C Programming Lab Report 1: 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:
Algorithm:
- Start
- Print the message “This is my first C program”.
- Stop
Flowchart:
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:
The sum of the given numbers is 35
Algorithm:
- Start
- Declare integer variables a, b, s.
- Read values for a and b.
- Calculate s = a + b.
- Display the value of s.
- Stop
Flowchart:
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 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:
- Start
- Declare integer variables a, b, s and a float variable p.
- Print the memory address of variable a.
- Print the memory address of variable b.
- Print the size in bytes of variable a.
- Print the size in bytes of variable p.
- Print the size in bytes of the literal value 1.5.
- Print the size in bytes of the float data type.
- Stop
Flowchart:
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:
The area of the circle is: 78.500000
The circumference of the circle is: 31.400000
Algorithm:
- Start
- Declare float variables r, a, c.
- Read the value of radius r.
- Calculate area a = 3.14 * r * r.
- Calculate circumference c = 2 * 3.14 * r.
- Display area a and circumference c.
- Stop
Flowchart:
Calculate c = 2*3.14*r
circumference (c)
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:
The volume of the sphere is 113.04
Algorithm:
- Start
- Declare float variables v, r.
- Read the value of r.
- Calculate v = (4.0/3.0) * 3.14 * r * r * r.
- Display the value of v.
- Stop
Flowchart:
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:
Simple interest = 100.00
Algorithm:
- Start
- Declare float variables p, t, r, s.
- Read values for p, r, and t.
- Calculate s = (p * t * r) / 100.
- Display s.
- Stop
Flowchart:
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:
The output is: 115.000000
Algorithm:
- Start
- Declare variables x, y, v.
- Read values for x and y.
- Calculate v = (x*x) + (y*y) – (100.0 / x).
- Display v.
- Stop
Flowchart:
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:
The mean of the given numbers is: 25.000000
Algorithm:
- Start
- Declare variables a, b, c, d, e.
- Read values for a, b, c, d.
- Calculate e = (a+b+c+d) / 4.0.
- Display e.
- Stop
Flowchart:
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:
The volume of the parallelepiped is 100
Algorithm:
- Start
- Declare variables l, b, h, v.
- Read values for l, b, h.
- Calculate v = l * b * h.
- Display v.
- Stop
Flowchart:
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:
The price of two pens & five copies after discount is 96.00
Algorithm:
- Start
- Declare float variables x, y, z, p.
- Read price of pen (x) and copy (y).
- Calculate total price z = 2*x + 5*y.
- Calculate discounted price p = z – (z * 20 / 100).
- Display p.
- Stop
Flowchart:
p = z – (z * 0.20)
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:
The total time in seconds is: 5415
Algorithm:
- Start
- Declare integer variables h, m, s, t.
- Read time as hours (h), minutes (m), and seconds (s).
- Calculate total seconds t = (h*3600) + (m*60) + s.
- Display t.
- Stop
Flowchart:
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:
Your age is 25
Your name is John
Algorithm:
- Start
- Declare an integer ‘a’ and a character array ‘name’.
- Read values for ‘a’ and ‘name’.
- Display the value of ‘a’.
- Display the value of ‘name’.
- Stop
Flowchart:
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:
You entered: G
Algorithm:
- Start
- Declare a char variable ch.
- Read a character into ch.
- Display the character ch.
- Stop
Flowchart:
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.