0% found this document useful (0 votes)
3 views67 pages

Chp2_C++ Programming Basics

This document covers the basics of C++ programming, including escape sequences, variables, data types, and input/output operations. It explains the use of manipulators like setw, the declaration and assignment of variables, and provides examples of integer and floating-point data types. Additionally, it discusses the importance of keywords, variable naming rules, and type conversion.

Uploaded by

thaungthaung0944
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)
3 views67 pages

Chp2_C++ Programming Basics

This document covers the basics of C++ programming, including escape sequences, variables, data types, and input/output operations. It explains the use of manipulators like setw, the declaration and assignment of variables, and provides examples of integer and floating-point data types. Additionally, it discusses the importance of keywords, variable naming rules, and type conversion.

Uploaded by

thaungthaung0944
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/ 67

C++ Programming Basics

Chapter 2
Programming in C++

Lecture By: Dr. Hsu Mon Kyi


Faculty of Computing (PU-Maubin) 1
Outlines
❖ Escape Sequences
❖ Variables
❖ The setw Manipulator
❖ Arithmetic Operators
❖ Type Conversion
❖ Library Functions

Faculty of Computing (PU-Maubin) 2


Escape Sequence Characters
Escape Sequence Character

\a Alert (i.e., bell)

\b Backspace

\f Form feed

\n New line

\r Carriage return

\t Horizontal tab

\\ Backslash

\’ Single quote

\” Double quote

Faculty of Computing (PU-Maubin) 3


Some Escape sequence Instructions & Their Outputs

Example Instructions Outputs

Computer
▪ cout<<“Computer\nScience”; →
Science

▪ cout<<“Computer\tScience”; → Computer Science

▪ cout<<“\’Computer Science\’”; → ‘Computer Science’

▪ cout<<“\”Computer Science\””; → “Computer Science”

▪ cout<<“\\Computer Science\\”; → \Computer Science\

Faculty of Computing (PU-Maubin) 4


The endl Manipulator
• This causes a linefeed to be inserted into the stream, so that subsequent text
is displayed on the next line.
• It has the same effect as sending the ‘\n’ character.
Example
cout<<“Hello, Good Morning!”<<endl;
cout<<“Have a nice day.”;
(or)
cout<<“Hello, Good Morning\n”;
cout<<“Have a nice day.”;
Output
Hello, Good Morning!
Have a nice day. 5
Faculty of Computing (PU-Maubin)
Variable

• is a symbolic name
• can be given a variety of values
• is a memory location whose contents can be changed.
• is used to stores some data in a C++ program

Faculty of Computing (PU-Maubin) 6


Data Types
• Generally, two broad categories of Data

Data

Numeric Data Non Numeric


Data

double,
int float long bool char string
etc…

Faculty of Computing (PU-Maubin) 7


Identifier Names (Variable Names)

• Variable, Functions, Structures, Classes and etc.


• Rules
• May be any length
• Are case sensitive
• Must begin with a letter, which includes an underscore(_)
• Subsequent characters may be letters, digits and underscores
• Cannot be a keyword
• May only be defined once in a scope
• Should avoid library names
• Must be declared before use
• Must be defined exactly once
Faculty of Computing (PU-Maubin) 8
Reserved Words or Keyword

• A keyword is a predefined word with a special meaning.

• Cannot be used these words as a variable name.

• Each keyword has its specific purpose.

• For example, include, main, cout etc.

Faculty of Computing (PU-Maubin) 9


Variable Names

Samples of acceptable variable Samples of unacceptable variable


names: names:

▪ Grade ▪ My Name

▪ myName ▪ 3rd_Number

▪ movie_name ▪ first-name

▪ a_123 ▪ Grade(Test)

▪ abc ▪ short

▪ _temp ▪ 123

Faculty of Computing (PU-Maubin) 10


Basic C++ Data Types

Faculty of Computing (PU-Maubin) 11


Integer Data Type
• A data type which contains only whole numbers. Whole numbers means,
numbers having no decimal point. For example 2, 70, 360 , 2500, -25 etc
• When we declare an integer variable, compiler reserves four bytes in
memory (System Dependent)
• In MS-DOS integer occupies 2bytes,on 32 bit System integer occupies 4
bytes memory.
• int variablename;
• for example int a;

Faculty of Computing (PU-Maubin) 12


Variable of type int in memory

Faculty of Computing (PU-Maubin) 13


Declaration / Assignment Statement
• A declaration introduces a variable’s name (var1) into a program and specifies its type
( such as int, float, char).
e.g. Declaration
• int var1;
• int var1, var2;
• Assignment statement assigns values to variable.
• int var1;
• var1=10;
• Equal Sign ‘=‘ assigns the value on the right hand to the variable on the left hand side.
• Initialization statement ( Declaration +assignment )
• int var1=10;

Faculty of Computing (PU-Maubin) 14


Example Program
//intvars.cpp
//demonstrates int variables
#include<iostream>
using namespace std;
int main()
{
int var1, var2; //define var 1; define var2; also call identifers;
var1=20; // assing 20 to var1;
var2= var1 + 10; // assingn value to var2;
cout << “var1 + 10 is ” ; // output text;
cout<< var2<<endl; // output value;
return 0;

} Faculty of Computing (PU-Maubin) 15


Other Integer Types

• Long - 4 bytes (long int = long)


- especially used for system 64 or MS-DOS

• Short - 2 bytes
- Both have fixed sizes no matter what system is used.

Faculty of Computing (PU-Maubin) 16


Char Data Type
• Char data type used to store, input and output a single character in our program.
• Type char stores integers that range in value from -128 to 127.
• To store ASCII character (eg ‘a’, ‘b’,’c’,’3’,’$’)
• Character data type uses single quotation marks around a character. It differs
from string constants, which uses double quotation marks.
• It occupies only one byte (= 8 bits) in memory.
• char variablename;
• e.g.,
• char c;
• char ch = ‘a’;
Faculty of Computing (PU-Maubin) 17
Character and String constant

• Character constant => ‘a’


• String constant => “Hello”
• When compiler encounters such a character constant ( ‘a ’), translate
into corresponding ASCII code (‘a’ => 97)

Faculty of Computing (PU-Maubin) 18


Variable of type char in memory

Faculty of Computing (PU-Maubin) 19


Example Program
//charvars.cpp
// demonstrates character variables
#include <iostream> //for cout, etc.
using namespace std;
int main()
{
char charvar1 = ‘A’; //define char variable as character
char charvar2 = ‘\t’; //define char variable as tab
cout << charvar1; //display character
cout << charvar2; //display character
charvar1 = ‘B’; //set char variable to char constant
cout << charvar1; //display character
cout << ‘\n’; //display newline character
return 0;
}

Faculty of Computing (PU-Maubin) 20


Input Statement in C++(cin)

• int var1=10; in this example, the value of variable is assigned within the
program which is making the program to restrict to this value only.
• We have a function called “cin” function which provides opportunity to
make more generalize program.
• It provides an option to the users to enter the values at run time from
the keyboard.
• cin>>variablename;
• For example , cin>>var1;
Faculty of Computing (PU-Maubin) 21
Input with cin

Faculty of Computing (PU-Maubin) 22


Input with cin (Example)
varAssign.cpp varInput.cpp
//display the multiplication of the two //display the multiplication of the two numbers using
numbers without using cin cin
#include <iostream> #include <iostream>
using namespace std; using namespace std;
int main() int main()
{ {
int x = 2, y = 3; int x, y;
int result; int result;
result = x * y; cin >> x;
cout << “Result = " << result; cin >> y; Output
return 0; Output result = x * y; 12
} cout << “Result = " << result; 3
Result = 6
return 0; Result = 36
}
Faculty of Computing (PU-Maubin) 23
Cascading of Input/Output Operator

Cascading of the input operator

▪ refers to the consecutive occurrence of input operators in a single statement.

Example:

cin >> a >> b >> c >> d ;

Cascading of the output operator

▪ refers to the consecutive occurrence of output operators in a single statement.

Example:

cout << a << b << c << d ;

Faculty of Computing (PU-Maubin) 24


Cascading of Input/Output Operator(Example)
cascadInput.cpp
/*C++ program to accept three numbers from user and display them using cascading cout & cin .*/
#include <iostream>
using namespace std; a b c
int main()
12 23 55
{
//accept three numbers from user
int a, b, c;
cout << "Enter the values of three numbers: “<< endl;
Enter the values of three numbers:
//cascading cin User
12 23 55
input
cin >>a >>b >>c; The value of first number : 12
//cascading cout The value of second number: 23
The value of third number: 55
cout << "The value of first number : " << a
<< "\nThe value of second number : " << b
<< "\nThe value of third number : "<< c << endl;
}
Faculty of Computing (PU-Maubin) 25
Program for adding two numbers

addNumber.cpp
#include<iostream.h>
using namespace std;
int main()
{
int a,b,c;
cout<<“Enter Value for a ”;
cin>>a;
cout<<“Enter Value for b ”;
cin>>b;
c = a + b;
cout << “ Sum = ” << c;
return 0;
}

Faculty of Computing (PU-Maubin) 26


Program for converting Fahrenheit to Celsius
//fahren.cpp
//demonstrates cin, newline
#include<iostream>
using namespace std;
int main()
{
int ftemp; //for temperature in fahrenheit
cout<<"Enter temperature in faherenheit: ";
cin>>ftemp;
int ctemp=(ftemp-32) * 5/9;
cout<<"Equivalent in Celsius is: "<<ctemp<<'\n’;
return 0;
}

Faculty of Computing (PU-Maubin) 27


Expression and Statements
Expressions produce a value
▪ Constant
▪ Variable
▪ Function call (returning a value – not void)
▪ Grouping of the above combined with operators
For example: 10, var1, var1+10, sqrt(var1), var1>10

Statements form a complete unit of work (called instructions)


▪ Are terminated with a semicolon (;)
▪ May span lines or may be more than one on a line
▪ Simple statements. For example : sum = var1 + var2;
▪ Compound (or block) statements are formed of simple statements between { and }. Block is
not terminated with a semicolon. For example:
{
var1 = 10, var2 = var1 * 2;
sum = var1 + var2;
cout<<“Sum = “<<sum;
}
Faculty of Computing (PU-Maubin) 28
Float Data Type

• Float are the numbers having decimal point in it.


• A floating point has two parts. An integer portion and fractional portion also called
mantissa.
• For example 24.60, 29.55 etc.
• When we declare a float variable, compiler reserves four consecutive bytes in
memory.
• occupies 4 bytes (32 bits) in memory
• float variablename;
• For example , float b;

Faculty of Computing (PU-Maubin) 29


Variable of type float in memory

Faculty of Computing (PU-Maubin) 30


Calculate and display circle’s area
// circarea.cpp
// demonstrates floating point variables
#include <iostream> //for cout, etc.
using namespace std;
int main()
{float rad; //variable of type float
const float PI = 3.14159F; //type const float
cout << “Enter radius of circle: “; //prompt
cin >> rad; //get radius
float area = PI * rad * rad; //find area
cout << “Area is “ << area << endl; //display answer
return 0;
} 31
Faculty of Computing (PU-Maubin)
Double and Long Double
• Larger floating point type
• Double => Require 8 bytes
• Long Double(= Double) =>compiler dependent

Faculty of Computing (PU-Maubin) 32


Floating Point Constant
• const float PI= 3.14159F

• 3.14159 => floating point constant

• F => float type

• The value of PI will not change throughout the program

• Any attempt to alter , error message from compiler

• floating-point constants using exponential notation


• 1,000,000,000 => 1.0E9
• 1234.56 =>1.23456E3
• 0.0000635239=>6.35239E–5
Faculty of Computing (PU-Maubin) 33
# define Directive

• Constant can also be specified using the preprocessor directive #


define.

• # define PI 3.14159

• Appear at the beginning of program

Faculty of Computing (PU-Maubin) 34


const and #define
const Qualifier
▪ Keyword const (for constant) precedes the data type of a variable
▪ The value of a variable will not change throughout the program
For example:
const float PI=3.14159F;

#define Directive
▪ Constants can also be specified using the preprocessor directive
#define
▪ Can’t specify the data type of the constant using #define
For example:
#define PI 3.14159

Faculty of Computing (PU-Maubin) 35


Type bool

• int => billions of possible value

• char => 256

• bool => only two possible values : true and false

• Requires only one bit of storage.

• In practice , compilers store them as bytes because quickly


accessed.

Faculty of Computing (PU-Maubin) 36


The setw Manipulator
▪ The manipulators are operators used with the insertion operator (<<) to modify the way how
the data is displayed.

▪ Manipulators are defined in <iomanip>.

setw manipulator

▪ changes the field width of output.

▪ The default field →just wide enough to hold the value.

▪ int 567 →a field 3 characters wide

▪ string “university” →a field 10 characters wide.

Faculty of Computing (PU-Maubin) 37


Example (without setw Manipulator)
//withoutSetw.cpp
//demonstrates need for setw manipulator
#include<iostream>
using namespace std;
int main()
{
long pop1=2425785, pop2=47, pop3=9761;
cout<<"LOCATION "<<"POP."<<endl
<<"Portcity "<<pop1<<endl
<<"Hightown "<<pop2<<endl
<<"Lowville "<<pop3<<endl;
return 0;
}
Faculty of Computing (PU-Maubin) 38
Example (with setw Manipulator)
//withSetw.cpp
//demonstrates setw manipulator
#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
long pop1=2425785, pop2=47, pop3=9761;
cout<<setw(8)<<"LOCATION "<<setw(12)<<"POP."<<endl
<<setw(8)<<"Portcity "<<setw(12)<<pop1<<endl
<<setw(8)<<"Hightown "<<setw(12)<<pop2<<endl
<<setw(8)<<"Lowville "<<setw(12)<<pop3<<endl;
return 0;
}
Faculty of Computing (PU-Maubin) 39
Field width and setw

Faculty of Computing (PU-Maubin) 40


The setw Manipulator (Example)

#include<iostream>
#include<iomanip> //used for setw function
using namespace std;
int main()
{
cout <<setw(10)<< "City" << setw(20)<<"Nation"<< endl;
cout<<setw(10)<<"Yangon"<<setw(20)<<"Myanmar"<<endl;
Explanation
cout<<setw(10)<<"Seoul"<<setw(20)<<"Korea"<<endl;
●●●●●●City●●●●●●●●●●●●●●Nation
return 0;
●●●●Yangon●●●●●●●●●●●●Myanmar
} ●●●●●Seoul●●●●●●●●●●●●●●●Korea

Faculty of Computing (PU-Maubin) 41


Arithmetic Operators

▪ are used to perform mathematical operations on variables and values.

Table1. Basic arithmetic operators


Operator Meaning Example

+ addition ans = 7 + 3

- subtraction ans = 7 - 3

* multiplication ans = 7 * 3

/ division ans = 7 / 3

% modulus ans = 7 % 3

Faculty of Computing (PU-Maubin) 42


Arithmetic Operators (Example 1)
remainderEg.cpp
//example of division and modulus operator
#include<iostream>
using namespace std;
int main()
{
cout << 19 / 10 << endl; //1
cout << 19 % 10 << endl; //9
cout << 19.0 / 10 << endl; //1.9
cout << 19 / 10.0 << endl; //1.9
cout << 19.0 / 10.0 << endl; //1.9
cout << 2.5 % 2 << endl; //Error
cout << 5 % 2.5 << endl; //Error

return 0;
}

Faculty of Computing (PU-Maubin) 43


Arithmetic Operators (Example 2)
arithmeticEg.cpp
//display the use of arithmetic operators (addition and multiplication)
#include<iostream>
using namespace std;
Enter two numbers to be
int main() operated
{ with arithmetic operators:

//calculate addition and multiplication of two numbers


int num1, num2;
cout << "Enter two numbers to be operated with arithmetic operators: ";
cin >> num1 >> num2; Output
cout << "Num1 + Num2 = " << num1 + num2 << endl; 12 9
cout << "Num1 * Num2 = " << num1 * num2 << endl;
Num1 + Num2 = 21
return 0;
} Num1 * Num2 = 108

Faculty of Computing (PU-Maubin) 44


Arithmetic Assignment Operators

▪ are used to assign values to variables.

Table2. A list of all assignment operators


Operator Example Same as

= x=5 x=5

+= x += 3 x=x+3

-= x -= 3 x=x-3

*= x *= 3 x=x*3

/= x /= 3 x=x/3

%= x %= 3 x=x%3

Faculty of Computing (PU-Maubin) 45


Arithmetic Assignment Operators (Example 1)
arithmeticAssign.cpp
//assign a single value to three variables and display the sum
#include<iostream>
using namespace std;
int main()
{
int num1, num2, num3;
int result;
num1 = num2 = num3 = 5; //assign the value 5 to variables num1,
num2 and num3
result = num1 + num2 + num3; //calculate sum
cout << "The result is " << result <<endl;
return 0;
Output
} The result is 15

Faculty of Computing (PU-Maubin) 46


Arithmetic Assignment Operators (Example 2)
arithmeticAssign2.cpp
//demonstrates arithmetic assignment operators
#include<iostream>
using namespace std;
int main()
{ int ans=27;
ans+=10; //same as: ans = ans + 10;
cout<<ans<<", ";
ans-=7; //same as: ans = ans - 7;
cout<<ans<<", ";
ans*=2; //same as: ans = ans * 2;
cout<<ans<<", ";
ans/=3; //same as: ans = ans / 3;
Output
cout<<ans<<", "; The result is
ans%=3; //same as: ans = ans % 3; 37,30,60,20,2

cout<<ans<<endl;
47
return 0; Faculty of Computing (PU-Maubin)
Increment Operators

Increment operators

▪ are used to increase the value of the variable by one.

++ //increment operator

Decrement operators

▪ are used to decrease the value of the variable by one.

-- //decrement operator

▪ Both increment and decrement operators are used on single operand or variable, so it is
also called as unary operator.

Example: x++, y--

Faculty of Computing (PU-Maubin) 48


Prefix / Postfix

▪ The increment/decrement operator can be used in two ways:

1. a prefix meaning that the operator precedes the variable.

▪ Prefix notation → ++var;

▪ The value of var is incremented by 1 then, it returns the value.

2. a postfix meaning that the operator follows the variable.

▪ Postfix notation → var++;

▪ The original value of var is returned first then, var is incremented by 1.

▪ The decrement (--)operator works like the ++operator except it decreases the value by 1.

Faculty of Computing (PU-Maubin) 49


Prefix / Postfix (Example)

#include <iostream>
using namespace std;
int main() var1 var2 Output
{ 5 5 5
int var1 = 5, var2 = 5; 6 6 7
cout << var1++ << endl; 7 6
5
cout << ++var1 << endl; 6
4
cout << ++var2 << endl; 5
5
cout << var2 <<endl;
cout<< - -var2 <<endl;
cout<< var2-- <<endl;
return 0;
}

Faculty of Computing (PU-Maubin) 50


Expression

▪ is a combination of variables, constants and operators that represents a computation.

▪ consists of one or more operands, zero or more operators to compute a value and
produces some value.

▪ Example of C++ expression:

▪ (x/y) – z

▪ alpha +12

▪ (alpha – 37) * beta / 2

Faculty of Computing (PU-Maubin) 51


Precedence
▪ Operator precedence specifies the order of operations in expressions that contain more
than one operator.
Table 3: Arithmetic operator precedence

Precedence Operator Description Example Associativity


1 () Grouping (a + b) / 4 Left to right
operator
2 ++ Increment ++a Right to left
-- Decrement - -a
3 * Multiplication int i = 2 * 4
/ Division float f = 10 / 3 Left to right
% Modulus int rem = 4 % 3
4 + Addition int i = 2 + 3 Left to right
- Subtraction int i = 5 - 1
5 = assignment int i = 5 Right to left

▪ In this expression (a – 32) * 5 / 9 , which is calculated first?


Faculty of Computing (PU-Maubin) 52
Expression (Example 1)
▪ Expression = (a – 32) * 5 / 9 ▪ Expression = a – 32 * 5 / 9
▪ Given a = 40 ▪ Given a = 40
(a – 32) * 5 / 9 a – 32 * 5 / 9
= (40 – 32) * 5 / 9 = 40 – 32 * 5 / 9
=8*5/9 = 40 – 160 / 9
= 40 / 9 = 40 – 17
=4 = 23

Faculty of Computing (PU-Maubin) 53


Expression (Example 2)
▪ Expression = 40 + 32 * 10 / 3 + 7 – 5 % 3

= 40 + 320 / 3 + 7 – 5 % 3

= 40 + 106 + 7 – 5 % 3

= 40 + 106 + 7 – 2

= 146 + 7 – 2

= 153 – 2

= 151

Faculty of Computing (PU-Maubin) 54


Type Conversion

▪ A type cast is basically a conversion from one type to another.

▪ Implicit Type Conversion or Automatic Type Conversion

▪ Explicit Type Conversion

▪ Implicit conversion is used when an expression contains variables of more than one
type.

▪ All the data types present in the expression are converted to data type of the
variable with the largest data type.

▪ Explicit conversion also called type casting is user defined and the user can type
cast the result to make it of a particular data type.

Syntax: (type) expression

Faculty of Computing (PU-Maubin) 55


Automatic Type Conversion (Example)
implicitConvert.cpp
//Example of implicit conversion
#include <iostream>
using namespace std;
int main()
{ int x = 10; // integer x
char y = 'a'; // character c
Output
x = x + y; // y implicitly converted to int. ASCII, the value of 'a' is 97 x = 107
float z = x + 1.0; // x is implicitly converted to float y=a
cout << "x = " << x << endl; z = 108
cout << "y = " << y << endl;
cout << "z = " << z << endl;
return 0;
Faculty of Computing (PU-Maubin) 56
}
Automatic Type Conversion (cont’d)

Table 1: Order of Data Types


Data Type Order

long double Highest


double
float
long
int
short
char Lowest

Faculty of Computing (PU-Maubin) 57


Example(Data range)
// signtest.cpp
// tests signed and unsigned integers
#include <iostream>
using namespace std;
int main()
{
int signedVar = 1500000000; //signed
unsigned int unsignVar = 1500000000; //unsigned
signedVar = (signedVar * 2) / 3; //calculation exceeds range
unsignVar = (unsignVar * 2) / 3; //calculation within range
cout << “signedVar = “ << signedVar << endl; //wrong
cout << “unsignVar = “ << unsignVar << endl; //OK
return 0;
Faculty of Computing (PU-Maubin) 58
}
Conversion using Cast Operator

▪ A cast operator is an unary operator which forces one data type to be


converted into another data type.

▪ C++ supports four types of casting:

▪ Static Cast

▪ Dynamic Cast

▪ Const Cast

▪ Reinterpret Cast

Faculty of Computing (PU-Maubin) 59


Conversion using Static Cast Operator

//explicit conversion from float to integer


#include <iostream>
using namespace std;
int main()
{ Output

float f = 3.5; b=3

int b;
// using cast operator
b = static_cast<int>(f);
cout << "b = "<< b;
}

Faculty of Computing (PU-Maubin) 60


Conversion using Static Cast Operator
// cast.cpp
// tests signed and unsigned integers
#include <iostream>
using namespace std;
int main()
{
Output
int intVar = 1500000000; //1,500,000,000 intVar = 211509811
intVar = 1500000000
intVar = (intVar * 10) / 10; //result too large
cout << “intVar = “ << intVar << endl; //wrong answer
intVar = 1500000000; //cast to double
intVar = (static_cast<double>(intVar) * 10) / 10;
cout << “intVar = “ << intVar << endl; //right answer
return 0;
61
} Faculty of Computing (PU-Maubin)
Library Functions Functions

▪ Many functions and symbols needed to run a C++ program are provided as
collection of libraries.
▪ Every library has a name and is referred to by a header file.
▪ Each header file contains file access, mathematical computations, and data
conversion, among other its information for a particular group of library
functions.
▪ iostream header file →I/O functions and objects, including cout and cin.
▪ iomanip header file → changes the field width of output.
▪ math header file → information for mathematics functions such as sqrt().
▪ string header file →string functions such as strcpy()and so on.

Faculty of Computing (PU-Maubin) 62


Library Functions Functions
// sqrt.cpp
// demonstrates sqrt() library function
#include <iostream> //for cout, etc.
#include <cmath> //for sqrt()
using namespace std;
int main()
{
double number, answer; //sqrt() requires type double
cout << “Enter a number: “;
cin >> number; //get the number
answer = sqrt(number); //find square root
cout << “Square root is “
<< answer << endl; //display it
return 0; }
Faculty of Computing (PU-Maubin) 63
Let Do Exercises

Faculty of Computing (PU-Maubin) 64


Exercises (Escaping sequence)
1. Write a program that displays your favorite poem. Use an
appropriate escape sequence for the line breaks.
Candy is dandy,
But liquor is quicker.
2. Write a program that displays below sentences. Use an appropriate escape
sequence for your output statement.
“Run, Spot, run,” she said.
3. Write a program that displays the following output.
Electronic Clinic
4. Write a program that displays the following output.
‘C++ ‘ is an Object-Oriented Programming
5. Write a program that displays the following output.
‘U’ ‘C’ ‘S’ ‘Mub’
Faculty of Computing (PU-Maubin) 65
Exercises( variables)
1. Write a program that displays the following output using integer,
floating point and character variable.
Integer number is= 97
Floating point number is= 105.75
Character is= a

Faculty of Computing (PU-Maubin) 66


References
• “Object-Oriented Programming in C++” by Robert Lafore, 4th Edition
ISBN: 0-672-32308-7
• C++ L 2-2 MIIT V3.ppt from Faculty of Computer Science, University of
Computer Studies(BOS)
• C++ L 2-3 MIIT V3.ppt from Faculty of Computer Science, University of
Computer Studies(BOS)
• C++ L 2-4 MIIT V3.ppt from Faculty of Computer Science, University of
Computer Studies(BOS)
• C++ L 2-5 MIIT V3.ppt from Faculty of Computer Science, University of
Computer Studies(BOS)
• C++ L 2-6 MIIT V3.ppt from Faculty of Computer Science, University of
Computer Studies(BOS)

Faculty of Computing (PU-Maubin) 67

You might also like