EXERCISES
Dr. Md Rakib Hassan
Associate Professor,
Dept. of Computer Science & Mathematics.
Exercises
2
Describe the two ways to include comments in
a C++ program.
What is wrong with this program?
#include <iostream>
int main()
{ // prints "Hello,World!":
cout << "Hello,World!\n"
}
Dr. Md. Rakib Hassan
Exercises (Cont.)
3
What is wrong with the following C-style
comment?
cout << "Hello,/* change? */ World.\n";
What’s wrong with this program:
#include <iostream>;
int main
{
// prints "n = 22":
n = 22;
cout << "n = << n << endl;
}
Dr. Md. Rakib Hassan
Exercises (Cont.)
4
What does a declaration do?
What is the purpose of the preprocessing directive:
#include <iostream>
What is the shortest possible C++ program?
Where does the name “C++” come from?
What’s wrong with these declarations:
int first = 22,last = 99,new = 44,old = 66;
Dr. Md. Rakib Hassan
Exercise (Cont.)
5
If m=5 and n=2, what the values of m and n
will be after each of the following statements
executes:
m *= n++;
m += --n;
Dr. Md. Rakib Hassan
Programming Exercise
6
Program 1:
Write a program that prints the following:
*
* *
* * *
* * * *
Dr. Md. Rakib Hassan
Programming Exercise (Cont.)
7
Program 2:
Write and run a program that prints the sum,
difference, product, quotient, and remainder of
two integers. Initialize the integers with the values
60 and 7.
Program 3:
Write and run a program that prints the sum,
difference, product, quotient, and remainder of
two integers that are input interactively.
Dr. Md. Rakib Hassan
Programming Exercise (Cont.)
8
Program 4:
Writea program that will take temperature as
Celsius and convert it into Fahrenheit.
Program 5:
Write
a program that will take temperature as
Fahrenheit and convert it into Celsius.
Dr. Md. Rakib Hassan
Programming Exercise (Cont.)
9
Program 6:
Writea program that will take a number as input
using cin>> and test whether the number is even
or odd.
Program 7:
Find
the area of a circle for a given radius. The
output should be like the following:
Enter
the radius of a circle: _
The area of the circle is: _
Dr. Md. Rakib Hassan
Programming Exercise (Cont.)
10
Program 8:
Write a program that will check a number whether it is
positive or negative.
Program 9:
Write a program to read two numbers X and Y and
swap their values.
Program 10:
Write a program to read 3 number and find the
smallest and largest number.
Dr. Md. Rakib Hassan
Questions???
11 Dr. Md. Rakib Hassan
Course Lectures
12
www.mrhacademy.com/cpp
Dr. Md. Rakib Hassan