COMSATS Institute of Information Technology Lahore
DEPARTMENT OF COMPUTER SCIENCE
Programming Fundamentals
LAB TASK (CSC103)
PREPARED BY
Abdul Karim Shahid,
Assistant Professor
Create a DOC file to upload your work on turnitin
Use following information to join class at trunitin
(https://www.turnitin.com/newuser_join.asp?svr=21&session-
id=234ac93dddbbe74a75fdc77179266dbe&lang=en_us&r=82.1809675577146 ):
Class name: FA19-BCS-B
Class ID: 22196054
Enrollment Key: 12345678
Class name: FA19-BCS-C
Class ID: 22196068
Enrollment Key: 12345678
CSC103 Programming Fundamentals-Lab Page 2 of 8
Doc File Format for labwork submission
Title Page:
Lab Work XX
CSC103-Programming Fundamentals
Submitted by:
NAME
FA19-BCS-XXX
B/C
Submitted to:
Mr. Abdul Karim Shahid
Submitted on: September 3, 2019
Department of Computer Science
COMSATS University Islamabad
Lahore Campus
CSC103 Programming Fundamentals-Lab Page 3 of 8
Lab task Solution:
Problem statement
Flowchart or Pseudo code (if asked)
Solution (C code must contain Your name and registration number)
Screen shot of sample output with white background
CSC103 Programming Fundamentals-Lab Page 4 of 8
CSC103 Programming Fundamentals Lab
Week1&2: The Programming Environment
Learning Objectives:
The objectives of this first experiment are:
1. To make you familiar with the Windows programming environment and the "C"
compiler. For this purpose you will use DevCPP/CodeBlocks/Turbo C (TC). You will
write, compile and run a (it may be your first) programme in "C"
Your lab report is expected to contain the following for each exercise:
C Source Code with proper comments.
At the top of your C source code also write the following comments
o /* This programme is prepared by XXXXXXXXXXX Reg no.: FA19-BCS-
XXX Section: B/C on dd/mm/yyyy. This programme ……….(a bit of
explanation what your programme does.*/
Formatted Output and the printf function
One of the common task in every program is the printing of output. We use the output to request input
from a user and later display the status/result, computations etc. In C programming there are several
functions for printing formatted output. Here we discuss the printf() function, which writes output to
the computer monitor. To use the printf() function we must include the stdio library in the source
code. To do this just place the following code at the beginning of your program.
To print a simple message in computer screen you might call printf() function as follows:
#include <stdio.h>
int main()
{
printf ("You are learning printf() function");
return 0;
}
CSC103 Programming Fundamentals-Lab Page 5 of 8
Output:
You are learning printf() function
In the above examples, the cursor will remain at the end of the printed output. If you repeat the above
code in the following way the second message would appear immediately after the first one.
#include <stdio.h>
int main()
{
printf("You are learning printf() function");
printf("You are learning printf() function");
return 0;
}
Output:
You are learning printf() function You are learning printf() function
If we want to print the second output in a new line we must need a different way, which has discussed
in the following section.
Escape sequences in C
An escape sequence is a series of characters that represents a special character. It begins with a
backslash character (\), which indicates that the character(s) that follow the backslash character
should be treated in a special way. C uses escape sequences within a format string to print certain
special characters. For example \n moves the output position to the beginning of the next line. The
following is a list of escape sequences.
Escape sequence Action
\n prints a new line
\b backs up one character
CSC103 Programming Fundamentals-Lab Page 6 of 8
\t moves the output position to the next tab stop
\\ prints a backslash
\" prints a double quote
\' prints a single quote
You will get an idea of using the above escape sequences from the following example.
#include<stdio.h>
int main()
{
printf("Create a new line\n");
printf("Print a double quotes (\") within a string\n");
printf("Print a single quotes (\') within a string\n");
printf("Print a Backslash\\ within a string\n");
printf("Using Backspace\b within a string\n");
printf("Using\tTab within a string\n");
return 0;
}
Output:
Create a new line
Print a double quotes (") within a string
Print a single quotes (') within a string
Print a Backslash\ within a string
Using Backspace within a string
Using Tab within a string
Exercise 1: Print following shape using simple printf statements (You may print these shapes
vertically in one program)
(1) (2) (3) (4) (5) (6)
* ********** * * ***** *****
*** * * ** ** **** ****
***** * * *** *** *** ***
*** * * **** **** ** **
* ********** ***** ***** * *
CSC103 Programming Fundamentals-Lab Page 7 of 8
Exercise 2: Write a program that prints the numbers 1 to 4 on the same line. Write the program using the
following methods.
a) Using one printf statement with no conversion specifiers.
b) Using one printf statement with four conversion specifiers.
c) Using four printf statements
Exercise 3: Write a C-Program to perform the simple arithmetic operations (addition,
subtraction, multiplication, division, remainder).
Exercise 4: Write a C-Program to swap two integer numbers without and with using third
variable.
Exercise 5: Write a C-Program to calculate area and Perimeter of the triangle.
[Area of triangle= ½ x base x vertical height]
[Perimeter of triangle = a + b + c]
Start
Declare variable base, vh
and tarea
Calculate area of triangle
tarea = 0.5 * base * vh
Print area of triangle
triangle
End
CSC103 Programming Fundamentals-Lab Page 8 of 8