KMK 1093 Basic Programming
LAB 02
C++ Programming
What you will learn today:
1.How to declare a variable
2.Identifiers and keywords
3.Using cin to get user input
4.Using assignment and arithmetic operators
5.Using different data types for variable
KMK 1093 Basic Programming
Lab2-1: Declaring and Initializing variable
1: Type the program below and save in a file name myage.cpp
2. Compile and run program myage in file myage.cpp
3. Let’s examine the following C++ program. We have numbered the lines so that we can discuss them.
myage.cpp
1. #include <iostream>
2. using namespace std;
3. int main()
4. {
5. int myage; //declare a variable “myage” of type int
6. myage=21; //initialize myage
7. cout << "I am " <<myage<<” years old”<< endl;
8. return 0;
9. }
Line 1 begin with include directives to the C++ preprocessor. The preprocessor scans the
programs for lines beginning with a hash mark (#). The words “include <iostream>” tell the preprocessor
to insert the contents of the file iostream in place of the directive. The angle brackets around the file
name indicate that the file is in the standard include directory.
Line 2 is a using directive. This directive tells the compiler that all the identifiers in the std
namespace are accessible to the program.
Line 3 contains the words that begin with the executable part of the program-that is,the part
that contains the statements that describe what you want to have done. A functions performs an action
and returns a result. The data type identifier int says that the value returned by function main is an
integer. The compiler knows that the main is a function because it is followed by a pair of parentheses.
KMK 1093 Basic Programming
Line 4 contains only one character: the left brace ({) . This character begins a block that is the
body of the function. The right brace(}) on line 9 is the closing brace of the block;it ends the body of the
function. The body of the functions contain the statements that are translated by the compiler and
executed when the program is run.
Line 5 contains the declarations of int variables. In line 5, myAge is declared to be an integer
variable.The compiler assigns a memory location to myAge.
Line 6 are assignment statements. In line 6 myAge is given 21 as a value.
Line 7 cause the information to be written on the screen. cout,which is define in <iostream>, is a
a predefine variable that denotes an output stream. An output stream is just what it sound like;a stream
of character sent to some output device. The operator << (double less –than signs)is called the insertion
operator. The stream of characters described on the right of the insertion operator is sent to the output
stream named on the left of the first insertion operator.
Line 8 says to return the values zero. By convention ,main returns zero when the program
executes with no errors. Line 9 ends function main and thus the program. The output from this program
is :
Sample Run:
I am 21 years old
KMK 1093 Basic Programming
Lab2-2: Different data types and using cin to get user
input
1: Type the program below and save in a file name myage2.cpp
2. Compile and run program myage2 in file myage2.cpp
myage2.cpp
#include <iostream>
using namespace std;
int main()
{
int myAge; //declare a variable “myage” of type int
float myHeight; //store user’s height
char firstInitial; // first Initial value
cout<<”Input your first initial “;
cin>>firstInitial;
cout<<”How old are you”;
cin>>myAge;
cout<<”What is your height in centimetre “;
cin>>myHeight;
cout<<endl;
cout << "Oh, your first initial is " <<firstInitial << endl;
cout<<”Your age is “ <<myAge<<” and you are “ <<myHeight <<”cm tall . “;
Sample Run:
KMK 1093 Basic Programming
Lab2-3: Arithmetic operators
1: Type the program below and save in a file name calculate.cpp
2. Compile and run program calculate in file calculate.cpp
#include <iostream>
using namespace std;
int main()
{
int first,second,sum,diff;
cout<< “Input integer 1: “;
cin>>first;
cout<<”Inputer integer 2: “;
cin>>second;
cout<<endl;
sum=first+second; //assigning the value of first +second to sum
//using the “+” arithmetic operators to get the sum of first and second.
cout<<”first + second = “<<sum <<endl;
diff=first-second;
cout<<” first – second ”<<diff<<endl;
}
Sample Run:
KMK 1093 Basic Programming
Lab2-Exercise 1
1. Write a program that will prompt the user to input 2 numbers. (The user can enter float
number). Find the sum and difference of the two numbers.
Sample Run:
Enter Number 1 : 4.2
Enter Number 2: 2.2
The sum: 4.2 + 2.2 = 6.4
The difference : 5.2 – 2.2 = 3.0
Lab2-Exercise 2
2. Write a program that will prompt the user to input his/her age. Find what year he/she
was born using the input year as references.
Sample Run:
Please enter your current age: 21
Please enter current year : 2011
You are born in year : 1990