Basic Programming Lab 2
Basic Programming Lab 2
LAB 02
C++ Programming
3. Let’s examine the following C++ program. We have numbered the lines so that we can discuss them.
myage.cpp
1. #include <iostream>
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 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
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
Sample Run:
KMK 1093 Basic Programming
#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;
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:
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: