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

Programming Fundamentals Lab 02 (Writing and Compilling 1st Prog)

This document is a lab manual for a programming fundamentals course covering C++. It includes topics on writing a basic C++ program, understanding each line of a sample "Hello World" program, C++ data types, escape sequences, and 4 programming tasks involving basic math operations and geometry calculations using data types like int and float. The tasks are designed to help students learn how to write, compile, and understand simple C++ programs.

Uploaded by

Ahmad Abduhu
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
46 views

Programming Fundamentals Lab 02 (Writing and Compilling 1st Prog)

This document is a lab manual for a programming fundamentals course covering C++. It includes topics on writing a basic C++ program, understanding each line of a sample "Hello World" program, C++ data types, escape sequences, and 4 programming tasks involving basic math operations and geometry calculations using data types like int and float. The tasks are designed to help students learn how to write, compile, and understand simple C++ programs.

Uploaded by

Ahmad Abduhu
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

Programming Fundamentals

Lab Manual 2

Topics:
Understanding how to Write and Compile a
C++ Program.
Understanding each line of first C++ program.
Understanding C++ Data types.

Lab Instructor: Ahmad Abduhu

Session: Spring 2019

School of Systems and Technology


UMT Lahore, Pakistan

1
Contents
Writing first C++ program :.............................................................................................................3
Hello World example.......................................................................................................................3
Sample Task.................................................................................................................................3
Understanding every line of the Sample program:..........................................................................4
C++ Data Types:.............................................................................................................................4
Escape Sequences:...........................................................................................................................5
Lab Tasks.........................................................................................................................................6
Task 1:.........................................................................................................................................6
Task 2:.........................................................................................................................................6
Task 3:.........................................................................................................................................6
Task 4:.........................................................................................................................................7

2
Writing first C++ program :
Hello World example
Learning  C++ programming can be simplified into:
 Writing your program in a text-editor and saving it with correct extension(.CPP, .C, .CP)
 Compiling your program using a compiler or online IDE

The “Hello World” program is the first step towards learning any programming language and
also one of the simplest program you will learn. All you have to do is display the message “Hello
World” on the screen. Let us now look at the program:

Sample Task
// Simple C++ program to display "Hello World"
  
// Header file for input output functions

#include<iostream> 
  
using namespace std;
  
// main function -

// where the execution of program begins

int main()

    // prints hello world

    cout<<"Hello World";
      
    return 0;
}

3
Understanding every line of the Sample program:
1. // Simple C++ program to display “Hello World”  : This line is a comment line. A
comment is used to display additional information about the program. Comment does not
contain any programming logic. When a comment is encountered by a compiler, the
compiler simply skips that line of code. Any line beginning with ‘//’ without quotes OR in
between /*…*/ in C++ is  comment.
2. #include: In C++,  all lines that start with pound (#) sign are called directives and are
processed by preprocessor which is a program invoked by the compiler.
The #include directive tells the compiler to include a file and #include<iostream> . It
tells the compiler to include the standard iostream file which contains declarations of all the
standard input/output library functions.
3. int main(): This line is used to declare a function named “main” which returns data of
integer type. A function is a group of statements that are designed to perform a specific
task. Execution of every C++ program begins with the main() function, no matter where the
function is located in the program. So, every C++ program must have a main() function.
4. { and }: The opening braces ‘{‘ indicates the beginning of the main function and the
closing braces ‘}’ indicates the ending of the main function. Every thing between these two
comprises the body of the main function.
5. std::cout<<“Hello World”;:  This line tells the compiler to display the message “Hello
Worlld” on the screen. This line is called a statement in C++. Every statement is meant to
perform some task. A semi-colon ‘;’ is used to end a statement. Semi-colon character at the
end of statement is used to indicate that the statement is ending there.
The std::cout is used to identify the standard character output device which is usually the
desktop screen. Every thing followed by the character “<<” is displayed to the output
device.
6. return 0; : This is also a statement. This statement is used to return a value from a
function and indicates the finishing of a function. This statement is basically used in
functions to return the results of the operations performed by a function.
7. Indentation: As you can see the cout and the return statement have been indented or
moved to the right side. This is done to make the code more readable. In a program as
Hello World, it does not hold much relevance seems but as the programs become more
complex, it makes the code more readable, less error prone. Therefore, you must always
use indentations and comments to make the code more readable.

C++ Data Types:


There are only a few basic data types in C Like:

 char
 int
 float
 double

4
Escape Sequences:
Escape sequences are typically used to specify actions such as carriage returns and tab
movements on terminals and printers. They are also used to provide literal representations of
nonprinting characters and characters that usually have special meanings, such as the double
quotation mark (").
Character combinations consisting of a backslash (\) followed by a letter or by a combination
of digits are called "escape sequences." Escape sequence can be used to represent a newline
character, a horizontal tab or any other special character. The following table lists some of the
ANSI escape sequences and what they represent.
Escape Sequence Represents
\b Backspace

\n New line

\r Carriage return

\t Horizontal tab

\v Vertical tab

\' Single quotation mark

5
Lab Tasks
Task 1:

Write a c program to print her/his name, id, address, phone number to display the text on the
screen like that?
Sample Output:

Ali
15003065299
Jail Road, Lahore

Task 2:

Write five C++ statements to print the asterisk pattern as shown below using printf?

Sample Output:

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

Task 3:

Write a program which takes 2 integers as input from user and prints their sum, product,
difference, product, division and remainder.
Sample Output
Enter first integer 100
Enter second integer 72
Sum is 172
Difference is 28
Product is 7200
Quotient is 1
Remainder is 28

Now write the above program using two float variables, Look what happened.

6
Task 4:

1. Write appropriate C++ statements to match the description in each of the following comments.

1. declare an integer variable for radius


2. declare an integer variable for diameter
3. declare an integer variable for circumference
4. declare an integer variable for area
5. declare a constant integer variable for value of pie
6. prompt user to enter value of radius
7. store that value in radius
8. calculate diameter of circle and store in appropriate variable
9. calculate circumference of circle and store in appropriate variable
10. calculate area of circle and store in appropriate variable

Sample Output
Enter the radius 25
Diameter = 50
Circumference = 157
Area = 1963

Now write the above program again but use float type for radius, diameter, circumference and
area. Use the value 3.14159 for π. What is the new output?

You might also like