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

Lab_2 Manual

The document is a lab manual for a programming fundamentals course, focusing on basic programming concepts in C++. It covers topics such as header files, namespaces, the main function, input/output operations, comments, and variable declaration and initialization. Additionally, it includes example programs and tasks for students to practice their programming skills.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

Lab_2 Manual

The document is a lab manual for a programming fundamentals course, focusing on basic programming concepts in C++. It covers topics such as header files, namespaces, the main function, input/output operations, comments, and variable declaration and initialization. Additionally, it includes example programs and tasks for students to practice their programming skills.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Lab Manual Programming Fundamental

Lab 02: Basic Programming, Output & Escape


Object Oriented Programming
Sequences
Objective(s):
Understanding of Basics of Programming, Output & Escape Sequences
CLOs: CL01, CLO4

1) Header Files

A header file is a file which contains C++ function declarations and macro definitions and to be shared between
several source files. You request the use of a header file in your program by including it, with the C++
preprocessing directive #include like you have seen inclusion of iostream header file, which comes along with
your compiler.

2) Using namespace std;


The namespace creates a declarative region in which various program elements are defined. The using Lab
Manual Programming Fundamental CS-161L statement informs the compiler that you want to use the std
namespace. If the following line is not included the cout would not have been executed, as cout is included in the
namespace std.

3) Main Function
void main() is the first statement executed whenever the C++ program is run. It is the starting point of the
program. If main function is not included in the program, the compiler will show an error. Void is a data type of
function main, it shows the program is not returning any value

4) cout<<”Hello World”;
This line is a statement. Statements in C++ always end with semi-colon. Statements are always executed in the
order they appear. The cout corresponds to a standard output stream. It is used to display the output on the screen.
The symbol “<<” refers to an insertion operator. Its function is to direct the string constants to cout which display
it onto screen.
5) cin>>

cin corresponds to a standard input stream and is used to take input from input stream.

6) Comment

It makes a program more readable and gives an overall description of the code.
Comments are helpful in skipping the execution of some parts of the code.
Every time a program or code is reused after long periods of time, the comment recaps all the information of the
code quickly.
Types of comments:
1
Lab Manual Programming Fundamental
// Single Line Comment
/*Multi Line Comment*/

Variable:
Variables and Literals Variables represent storage locations in the computer’s memory. Literals are constant values
that are assigned to variables. Variables allow you to store and work with data in the computer’s memory. They
provide an “interface” to RAM. Part of the job of programming is to determine how many variables a program will
need and what types of information they will hold.
Declaration of Variables:
Declaration of variables means that compiler assigns a memory location for the variable. Declaration of a variable
consists of data type and the variable name.
Data_type var_name;
e.g. int number;
This is called a variable definition. It tells the compiler the variable’s name and the type of data it will hold. This line
indicates the variable’s name is number.
int var, number;
float var, num, sum;
char ch;
Initialization of variable:
Initialization of variables assigns an initial value to the variable declared. Variable value is initialized by
giving variable name a number consisting of equal sign followed by constant value.
Data type var_name=value;
e.g: int num=6;
float num=5.4,sum=0;
char ch=’A’;

Examples 01:

This program has a variable

#include<iostream>
Using namespace std;
int main()
{
int number; //initialization of variable
number=6;
cout<<”The value in Number is:”<<number<<endl; //ptint variable

return 0;
}

Output:

The value in Number is:6

2
Lab Manual Programming Fundamental

Examples 02:
This program has a variable

#include<iostream>
using namespace std;
int main()
{
int number;
number=6;
cout<<”The value in Number is:”;
cin>>number;
return 0;
}

Output:
The value in Number is:6

Example 03:

This program illustrate the sum of two numbers

#include<iostream>
using namespace std;
int main()
{
Int num1,num2,sum;
num1=2;
num2=3;
sum=num1+num2;
cout<<”Sum of two numbers are:”<<sum<<endl;
return 0;
}
Output:
Sum of two number is:5

3
Lab Manual Programming Fundamental
Example 04:
This program illustrate the sum of two numbers

#include<iostream>
using namespace std;
int main()
{
Int num1,num2,sum;

cout<<”Enter first number:”;


cin>>num1;
cout<<”Enter second number:”;
sum=num1+num2;
cout<<”Sum of two number is:<<sum;
return 0;
}
Output:
Enter first number:2
Enter second number:4
Sum of two numbers is:6

Task
1) Write a program to display

*
* *
* *
* *
*
1) Write a program to display
******
*
******
*
******

2) Write a program to show the following output

1
12
123
1234
12345
3) Write a program that get two integer numbers from user and store them in variables. and preformed the basic Arithmetic
Operations (+, - ,* , /, %). Print the results of operations as bellow

4
Lab Manual Programming Fundamental

4) Write a program in C++ to compute the total and average of five numbers.
5) Write a program that prompt user to input student name, address, id and cgpa.
6) Write a program to calculate the area of triangle.
7) The distance between two objects (in km.) is input through the keyboard. Write a program to convert and print this distance
in meters, feet, inches and centimeters.
Hint: Numbers are taken from user

You might also like