COSC 206 Lab1
COSC 206 Lab1
Lab Manual
Installation C Compiler:
The most popular C compiler for Windows is GCC (GNU Compiler Collection). However, for
simplicity, you might want to use a compiler like MinGW (Minimalist GNU for Windows), which is
a Windows distribution of GCC.
MinGW Installation:
1. Download MinGW:
2. Install MinGW:
1
Click on installation then apply changes
2
2. Add MinGW to PATH:
After installing MinGW you need to add the compiler's bin directory to your system's PATH
environment variable.
3. Verify Installation:
• Open a command prompt and type the following commands to verify that the compiler is
installed:
If you see version information for both commands, the installation was successful.
3
Lab 1:
Objective:
1. Introduce the basic structure of a C program and the printf function
#include <stdio.h>
int main() {
printf("Hello, World!\n");
return 0;
}
4
Lab 2: Variables and Data Types
Objective:
1. Introduce the concept of variables and basic data types.
2. To be familiar with syntax and structure of C-programming.
3. To learn problem solving techniques using C
Activity 1
Write a Program to calculate and display the volume of a CUBE having its height (h=10cm), width
(w=12cm) and depth (8cm).
Problem Analysis:
The problem is to calculate the volume of a CUBE having its inputs parameters identified as:Height
(integer type), width (integer type) and depth (integer type). The output of the program is to display
the volume; hence the output parameter is identified as vol (integer type). During the processing or
calculation phase, we don’t need any extra parameters (variables) for this problem.
The volume of the cube is the multiplication of its height, width and depth, hence the mathematical
formula to calculate volume is:
Algorithm:
1. Start
2. Define variables: h(int), w(int), d(int), vol(int)
3. Assign value to variables: h = 10, w=12, d=8
4. Calculate the volume as: vol = h*w*d
5. Display the volume (vol)
6. Stop
Code:
//Following code is written and compiled in VS Code IDE
5
#include<stdio.h>
//start the program
int main(void) {
//Variable declaration
int h,w,d,vol;
//assign value to
variables
h=10;w=12;d=8;
}
Output (Compilation, Debugging & Testing) The Volume of the cube is: 960
Lab 3
Objective(s):
To be familiar with different data types, Operators and Expressions in C.
Activity:
Write a program to take input of name, roll number and marks obtained by a student in 5
subjects each have its 100 full marks and display the name, roll number with percentage
score secured.
Algorithm:
1. Start
2. Define variables: name, rollno, msub1, msub2, msub3, msub4, msub5, msum, score
3. Take input from keyboard for all the input variables
4. Calculate the sum of marks of 5 subjects and also calculate the percentage score as: Msum =
msub1 + msub2 + msub3 + msub4 + msub5; Score = msum 500 × 100
5. Display the name, roll number and percentage score.
6. Stop
6
#include<stdio.h>
#include<conio.h> int
main(void)
{
char name[20];
int rollno;
scanf("%[^\n]", name);
scanf("%d", &rollno);
msum=msub1+msub2+msub3+msub4+msub5;
score = msum/500*100;
return 0;
}
Lab 4:
Objective(s):
To understand the programming knowledge using Decision Statements (if, if-else, ifelse if,
switch)
7
Activity
Write a program to input marks of 5 subjects (Physics, Chemistry, Math, English & Biology) for a
student. Display the rank of each subjects and also the result of total marks and percentage obtained
with his/her rank in the class. The rank is categorized as fail (marks < 40%), pass & third division
(marks between 40 to 55%), second (marks between 55 to 65%), first (marks between 65 to 80%),
Distinction (marks between 80 to 95%), extra ordinary (marks above 95 to 100%).
#include <stdio.h>
int main() {
// Variables to store marks in five subjects
float physics, chemistry, math, english, biology;
8
} else if (percentage >= 40 && percentage < 55) {
printf("Pass & Third Division\n"); }
else if (percentage >= 55 && percentage < 65) {
printf("Second Division\n");
}
else if (percentage >= 65 && percentage < 80) {
printf("First Division\n");
} else if (percentage >= 80 && percentage < 95) {
printf("Distinction\n");
} else {
printf("Extraordinary\n");
}
return 0;
}
1. Write a program that asks a number and test the number whether it is multiple of 5 or not,
divisible by 7 but not by eleven.
Lab 5:
Objective(s):
To understand the programming using Loop Statements (for, while, do-while)
Activity
Write a program to input two integer numbers and display the sum of even numbers between these
two input numbers.
#include <stdio.h>
int main() {
// Variables to store two integer numbers int
num1, num2;
9
// Calculate and display the sum of even numbers between num1 and num2
int sum = 0;
printf("Even numbers between %d and %d: ", num1, num2);
for (int i = num1; i <= num2; i++) {
if (i % 2 == 0) {
printf("%d ", i);
sum += i;
}
}
return 0;
}
10