[go: up one dir, main page]

0% found this document useful (0 votes)
13 views10 pages

COSC 206 Lab1

The document is a lab manual for COSC 206, providing instructions for installing the C compiler MinGW, verifying installation, and using text editors or IDEs like Visual Studio Code. It includes detailed lab exercises focusing on basic C programming concepts such as variables, data types, user input, decision statements, and loop statements. Each lab has specific objectives and tasks, guiding students through writing and executing C programs.

Uploaded by

dlovefky
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views10 pages

COSC 206 Lab1

The document is a lab manual for COSC 206, providing instructions for installing the C compiler MinGW, verifying installation, and using text editors or IDEs like Visual Studio Code. It includes detailed lab exercises focusing on basic C programming concepts such as variables, data types, user input, decision statements, and loop statements. Each lab has specific objectives and tasks, guiding students through writing and executing C programs.

Uploaded by

dlovefky
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

COSC 206: Introduction to C programming

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:

 Visit the MinGW website: https://sourceforge.net/projects/mingw/


 Download the installer (e.g., mingw-get-setup.exe).

2. Install MinGW:

 Run the installer and follow the setup wizard.

 In the MinGW Installation Manager, select:

"mingw32-base" and "mingw32-gcc-g++" packages.

 Click "Apply Changes" to download and install the selected tools.

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.

• Locate the folder where MinGW is installed (usually C:\MinGW\bin).


• Add this path to the system PATH variable:
 Go to Control Panel → System and Security → System → Advanced System
Settings → Environment Variables.
 In the "System variables" section, find and select the "Path" variable, then click
"Edit."
 Add the path to the MinGW bin directory at the end (e.g., C:\MinGW\bin).
 Clik on ok

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.

4. Choose aText Editor or IDE:


You can use any text editor to write C code, but using an integrated development environment (IDE)
can enhance your experience.
Some popular options include:
• Visual Studio Code (https://code.visualstudio.com/)
• To use Visual studio code for writing your c program:
▪ Open the visual studio code or install it from the above link
▪ Go to the Extensions view by clicking the Extensions icon in the sidebar or
pressing Ctrl+Shift+X (Windows/Linux)
▪ Search for "Code Runner" and install it. This extension allows you to
quickly run C programs without setting up tasks and launch configurations.

3
Lab 1:

Objective:
1. Introduce the basic structure of a C program and the printf function

• Task: Write a Simple Program


• Instruct students to write a simple "Hello, World!" program
• Emphasize the use of #include <stdio.h> and the “printf”
function.

• Task: Compile and Run
• Demonstrate how to compile the program using the
compiler.
• Execute the program and explain the output.

#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

Task: Declare and Use Variables


Teach students how to declare variables
Create a program that uses different data
types (int, float, char).

• Task: Input from User


Introduce “scanf” for user input.
Write a program that takes user input and displays it.

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:

vol = height* width* depth. (vol = h*w*d)

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;

//calculation using mathematical formula


vol=h*w*d;
//display the volume
printf("The Volume of the cube is: %d",vol);
//end the main program
return 0;

}
Output (Compilation, Debugging & Testing) The Volume of the cube is: 960

Exercises (please code yourself and show the output to instructor):


1. Write a program to display “hello world” in C.
2. Write a program to add two numbers (5&7) and display its sum.
3. Write a program to multiply two numbers (10&8) and display its product.
4. Write a program to calculate area of a circle having its radius (r=5).
5. Write a program to calculate area of an ellipse having its axes (minor=4cm, major=6cm).
6. Write a program to calculate simple interest for a given P=4000, T=2, R=5.5. (I =
P*T*R/100)

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;

float msub1, msub2, msub3, msub4, msub5, msum, score;

printf("Enter Name of Student: ");

scanf("%[^\n]", name);

printf ("\nRoll Number: ");

scanf("%d", &rollno);

printf ("\nEnter Marks in 5 Subjects:\n");

scanf("%f%f%f%f%f", &msub1, &msub2, &msub3, &msub4, &msub5);

msum=msub1+msub2+msub3+msub4+msub5;

score = msum/500*100;

printf("\nName of Student: %s", name);

printf("\nRoll Number: %d", rollno);

printf ("\nPercentage Score Secured: %2.2f%c", score,'%');

return 0;
}

Exercises (please code yourself and show the output to instructor):


1. Write a program to declare two integer and one float variables then initialize them to 10, 15,
and 12.6. Also print the variable values in the screen.
2. Write a C program to prompt the user to input 3 integer values and print these values in
forward and reversed order.
3. Write a program to calculate simple and compound interest.

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;

// Get input for marks in each subject


printf("Enter marks in Physics: ");
scanf("%f", &physics);

printf("Enter marks in Chemistry: ");


scanf("%f", &chemistry);

printf("Enter marks in Math: ");


scanf("%f", &math);

printf("Enter marks in English: ");


scanf("%f", &english);

printf("Enter marks in Biology: ");


scanf("%f", &biology);

// Calculate total marks and percentage


float totalMarks = physics + chemistry + math + english + biology;
float percentage = (totalMarks / 500) * 100;

// Display marks in each subject


printf("\nMarks in each subject:\n");
printf("Physics: %.2f\n", physics);
printf("Chemistry: %.2f\n", chemistry);
printf("Math: %.2f\n", math);
printf("English: %.2f\n", english);
printf("Biology: %.2f\n", biology);

// Display total marks and percentage


printf("\nTotal Marks: %.2f\n", totalMarks);
printf("Percentage: %.2f%%\n", percentage);

// Determine and display rank


printf("\nRank: ");
if (percentage < 40) {
printf("Fail\n");

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;
}

Exercises (Please Code yourself and show the output to instructor):

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;

// Get input for the two numbers


printf("Enter the first integer: ");
scanf("%d", &num1);

printf("Enter the second integer: ");


scanf("%d", &num2);

// Ensure num1 is smaller than num2


if (num1 > num2) {
// Swap values if num1 is greater than num2
int temp = num1;
num1 = num2;
num2 = temp;
}

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;
}
}

printf("\nSum of even numbers: %d\n", sum);

return 0;
}

10

You might also like