0% found this document useful (0 votes)
23 views

Lab 1

The document provides instructions for completing Lab 1 of the Computing Fundamentals course at the Pakistan Institute of Engineering and Applied Sciences. It instructs students to use Borland C to write and run a simple "Hello World" program. It then discusses variables in C, including declaring, initializing, and using variables. It provides examples of code and problems for students to solve. Finally, it covers special characters and format specifiers in C that are useful for formatting output.

Uploaded by

john kevin
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)
23 views

Lab 1

The document provides instructions for completing Lab 1 of the Computing Fundamentals course at the Pakistan Institute of Engineering and Applied Sciences. It instructs students to use Borland C to write and run a simple "Hello World" program. It then discusses variables in C, including declaring, initializing, and using variables. It provides examples of code and problems for students to solve. Finally, it covers special characters and format specifiers in C that are useful for formatting output.

Uploaded by

john kevin
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/ 9

Pakistan Institute of Engineering and

Applied Sciences

Computing Fundamentals
Laboratory Exercise-01

Date: Sep. 19, 2019

Computing Fundamentals Lab 1


Instructions
1. Use Borland C
2. Create a folder CF_Labs
3. Create new folder for each lab e.g. create folder “lab‐01” and save all the files in that folder for this lab
4. Create new file for each program
5. For any help contact lab staff or faculty member available in the lab

Programming Tool
For programming in C we need
1. An editor to create files to write code
2. A Compiler that will check our C code and will translate the code to machine code
3. A program that will indicate errors in our program
4. A program that will let us check the output of our program , in this lab we are using Borland C IDE, IDE stands
for Integrated Development Environment, it is a program that provides us all the tools necessary for
developing code

Can we use a different IDE?


There are many IDEs available for programming in C/C++, NetBeans, Eclipse, C Builder, MS Visual Studio, Turbo C
etc.

Borland C, Wright Your First Program

Steps to develop program in Borland C

1. Launch Borland C++ IDE


Go to C:\BC5\bin\ and double click bcw.exe, this will launch the program shown below, if you cannot find
the file ask any of the instructor available in the lab.

2. Create File
Next, click File menu, select New and select Text Edit , this will allow you to create a new text file, save
this file with c or cpp extension.

3. Write first program


Now you are ready to write your first program. Copy and paste the following code into the file, which
appears in the center of the screen.

#include<stdio.h>
#include<conio.h>
void main()
{
printf("hello winter");
getch();
}

Computing Fundamentals Lab 1


4. Compile and run the code
To compile the program press F9 or click Debug menu, select compile
To execute the program, click the Debug menu, select Run. Or press CRT+F9 key

Activity 1

Write a program to print your name.

Declaring, Initializing and Using Variables

The general format to declare any variable in c is as follows, however the value part is optional i.e. when declaring
variable it is not necessary to provide a value as well, and this can be done later in the program.

type identifier value ;

Example

Int x= 100 ;
float y= 2.5 ;

Example-1: Simple Declaration

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

void main(){

// variable declaration
int a, b;
int c;

// initialization
a = 10;
b = 20;
c = a + b;

printf("value of c : %d \n", c);


getch();
}

Example-2: Declaring and Initializing

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

void main(){
int a=10,b=20;
printf(“Starting Values of a and b are \n”);
printf(“a=%d,b=%d \n”,a,b);

//Re-Initializing
a = 30;
b = 40;
printf(“\nNew values of a and b are \n”);
printf(“a=%d,b=%d\n”,a,b);
getch();
}

Activity 2

Try to write the output of the following program, note down your answer, execute the program and compare
your answer with program output.

#include<stdio.h>
#include<conio.h>
void main ()

Computing Fundamentals Lab 1


{
int a=10,b=20,c=30;
printf(“Value of A = %d, value of B=%d”,a,b);
printf(“\nB+C=%d”,a+b);
a=b;
b=c;
c=a+b;
printf(“\nThe values of a,b and c are\n a= %d \nb=%d \nc=%d”,a,b,c);
getch();
}

Solving Problems

In the following section a number of example problems will be introduced along with their solution, read the
problems and try to understand the solution. If you can understand the concept try to solve the problems given in
this section.

Example-3: Sample Problem


Write a program to calculate the area of rectangle whose length and widths are 20 and 30 respectively.

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

void main(){ printf is an output function. It


int length,width,area; only displays data on the screen
length=20;
width=30;
area=length*width;
printf(“Area of rectangle is =%d”,area);
getch();
}

Data Input from keyboard:


To get input from the keyboard C provide a simple function called scanf, the function has two parts
1. Format string: Specifies which type of data to be input , uses %d, %c, %f, etc format specifiers
2. Address of variable/s to store value: One or more variable’s addresses to store the data entered from the
keyboard

Format string

scanf(“%d”,&a) ;

Variable address

Example-4: Scanf Function


Let’s rewrite our previous program to find the area of a rectangle, this time we will get the length and
width from the user using scanf
#include<stdio.h>
#include<conio.h>

void main(){
int length,width,area;
printf(“Enter Length = “);
scanf(“%d”,&length);
printf(“Enter Width = “);
scanf(“%d”,&width);
area=length*width;
printf(“Area of rectangle is =%d”,area);
getch();
}

Computing Fundamentals Lab 1


Activity 3

Write programs to solve the following problem.


1. What will be sale price of an item after 2 years if its value decreases 10% each year? Solve this for sale
price given by the user from the keyboard
2. Get three integers from the user (from keyboard) and display their sum and average
3. What is 10% of 20% of 45678

Using Special Characters

There are special characters in C that modifies the output of printf, some them are given below, each special
character starts with ‘\’

\n Print new line


\t Print a tab
\a Produces a beep (in old computers)
\\ Print backslash
\” Print double quote
\’ Print single quote

Understanding new line: \n


Write the following codes, execute it and compare the outputs.

Program-01:

#include <stdio.h>
#include <conio.h>
main(void)
{
printf(”This is a single sentence ”);
printf(“and will not be printed on two lines ”);
getch();
}
Learning: printf itself do not print a new line , programmer has to add suitable command for new lines

Program-01A

#include <stdio.h>
#include <conio.h>
void main(void)
{
printf(“This is not a \n single line “);
getch();
}

Understanding the tab: \t

#include <stdio.h>
#include <conio.h>
void main(void){
printf(“Characters in capitals will be printed far from each Other\n
A\t\t\t\tB\t\t\tC\t\t\tD”);
getch();
}

Activity 4

Use printf , new line character \n, tab character \t and * (stars, use shift+8 or num‐pad to type star character)
to Produce The Following Outputs
Output 1

Name : (Your Name)


Roll No : (Your Roll No)
Session : (Your Session)

Press any key to terminate

Computing Fundamentals Lab 1


Output 2

*******************
* *
* *
* *
* *
*******************

Format Specifiers

Format specifiers are used in printf function for printing numbers and characters, A format specifier acts like a place holder, it
reserves a place in a string for numbers and characters, for more understanding see the example below

Write the following example programs and see the outputs, by comparing the results of example understand the
use of format specifiers.

Program-01

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

void main(void){

printf(“There are %d number of days in a week“,7);

getch();

The %d is a format specifier or place holder for number 7, this is useful when we have to print the values stored in variables,
see the example below

Program-02

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

void main(void){

int num_days=365;

printf(“There are %d number of days in a year“, num_days);

getch();

Program-02B

#include <stdio.h>
#include <conio.h>
void main(void){
int num_days=365;
printf(“There are number of days in a year %d “, num_days);
getch();
}

Program-03

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

void main(void)

{
int random_num=10;
printf(“The value of random variable is = %d \n“ , random_num);

Computing Fundamentals Lab 1


random_num= 19;
printf(“The new value of random variable is = %d “ , random_num);
getch();
}

Followings are basic format specifiers in C (only %d is covered in this lab)


%d int (integers)
%c char (characters)
%f float (fractions)
%s string (sequence of characters)

Activity 5

Try to predict the output of the following programs and try to answer the question if any.

Program-01

#include <stdio.h>
#include <conio.h>
void main(void)
{
int a,b,c;
a=10;
b=40;
c=a+b;
printf(”The sum of a and b = %d“, c)
getch();
}

Program-02 (Printing Multiple Values)

#include <stdio.h>
#include <conio.h>
main(void)
{
int a,b,c;
a=10;
b=20;
c=30;
printf(” A = %d“,a);
printf(” B = %d“,b);
printf(” C = %d “,c);
getch();
}

Question: Why all values are printed on the same line?

Alternate way for program 02

#include <stdio.h>
#include <conio.h>
main(void)
{
int a,b,c;
a=10;
b=20;
c=30;
printf(” A = %d\n B = %d\n C = %d“,a,b,c);
getch();
}

Question: Why all values are printed on the different lines?

Computing Fundamentals Lab 1


Activity 6

Write code to develop the following programs.

Name:
Email:
City:

Education:

Degree/Certificate City / School / College / Univeristy


Matriculation ABC School etc
Intermediate ABC College etc
Graduation ABC University etc
Masters ABC University etc

Work Experience
(Few lines of your choice)

References
(Few lines of your choice)

Increment and Decrement Operators (Unary Operators)

There are two such operators, denoted as ++ and ‐‐ (double minus and plus sings), the operators can be used with
integer type data i.e. integer variables .The operators are not used independently, rather are appended before or
after an integer variable, such as ++a, or a++ similarly ‐‐a or a‐‐ where a is an integer variable. The operators adds
or subtracts 1 from the variable with which these are used.

Pre and Post Notation


If ++, or ‐‐ is appended before a variable name the operator are termed as pre‐increment or pre‐ decrement such
as ++a or ‐‐a otherwise the operators are referred as post increment or post decrement such as a++ or a—

Sample Program
The following program demonstrate use of increment and decrement operators
#include <conio.h>
#include <stdio.h>

int main(){
int a,b,c;
a=10;
b=20;
c=30;
printf(“Starting value of a,b,c are\n“);
printf(“a=%d,b=%d,c=%d\n“,a,b,c);
printf(“after a++,b++,c++ value are\n”);
a++;
b++;
c++;
printf(“a=%d, b=%d ,c=%d \n“,a,b,c);
getch();
return 0;

Computing Fundamentals Lab 1


Activity 7

1. Write program for the following expressions, where a,b,c are integer variables, evaluate the
expression and display the final result (final value of variable a)
Initial values of a,b,c are 10,20,30
a=b*c;
b*=b+c;
c=a++;
a=++b+c++;
2. Enhance the code and get the values of a,b and c from the user

Computing Fundamentals Lab 1

You might also like