Review Exercise Chapter 2 and 3

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

Review Exercise: Chapter 2 and 3

1. Algorithm Design: Flowchart and Pseudo-code


a. Design an algorithm and the corresponding flowchart for finding the sum of the
numbers 2, 4, 6, 8, …, n
b. Using flowcharts, write an algorithm to read 100 numbers and then display the
sum.
c. Write an algorithm to read two numbers then display the largest.
d. Write an algorithm to read 100 numbers then display the largest.
e. Write an algorithm to read a number and count the number of digits of the value
f. Write an algorithm to read an integer n and prints the factorial of n, assume that
n = 10.
g. Write an algorithm to read an integer n and check whether the number is prime
or not?
h. Write an algorithm to read a number and find the sum of digits of the given
number.
2. C++ Fundamental
a. Identifier | Keywords | CONSTANT | Variables | Data type
i. Identify valid identifiers from the following list
1. Hello
2. _TEST
3. For
4. break
5. _1
6. 2013_my_year
ii. Write the two ways of declaring a CONSTANT variable in C++?
iii. Give the declaration for two variables called count and distance. count is
of type int and is initialized to zero. distance is of type double and is
initialized to 1.5.
iv. Give the declaration for three variables called firtsName, lastName and
age with appropriate data type.
v. True/False
1. Identifiers are the names defined by the programmer to the basic
elements of a program.
2. It can consist of letters, digits, and underscore.
3. The starting letter of identifiers can be lowercase, uppercase or
underscore.
4. Keywords are the reserved words whose meaning is known by
the compiler.
5. Keywords can be started only with the lowercase letter.

1|Page
b. Operators
i. Determine the value, true or false, of each of the following Boolean
expressions, assuming that the value of the variable count is 0, x is 5, y
is 8 and limit is 10. Give your answer as one of the values true or false.
1. (count == 0) && (limit < 20)
2. count == 0 && limit < 20
3. (limit > 20) || (count < 5)
4. !(count == 12)
5. (count == 1) && (x < y)
6. (count < 10) || (x < y)
7. !( ((count < 10) || (x < y)) && (count >= 0) )
8. ((limit/count) > 7) || (limit < 20)
9. (limit < 20) || ((limit/count) > 7)
10. ((limit/count) > 7) && (limit < 0)
11. (limit < 0) && ((limit/count) > 7)
12. (5 && 7) + (!6)

3. Writing program, displaying output and identifying errors


a. Writing program
i. Write a program in C++ to calculate the area of rectangle.
Sample Output:
Calculate the volume of a cube : side3
---------------------------------------
Input the side of a cube : 5
The volume of a cube is : 125

ii. Write a program in C++ to find the third angle of a triangle.


Sample Output:
Find the third angle of a triangle :
-----------------------------------------
Input the 1st angle of the triangle : 30
Input the 2nd angle of the triangle : 60
The 3rd of the triangle is : 90

iii. Write a C++ program to solve the second degree equation


aX2 + bX + c= 0 for any real a,b and c
Sample Output
Enter the values a,b and c with space: 1 -2 1
The value of x is: 1
iv. Write a complete C++ program that asks the user for a number of
gallons and then outputs the equivalent number of liters. There are
3.78533 liters in a gallon. Use a declared constant.

2|Page
b. Displaying output
i. What is the output of the following program lines when embedded in a
correct program that declares number to be of type int?
number = (1/3) * 3;
cout << "(1/3) * 3 is equal to " << number;
ii. What output would be produced by the following two lines (when
embedded in a complete and correct program)?
//cout << "Hello from";
cout << "Self-Test Exercise";
iii. Write the output of the following C++ program
int m = 10, n=5;
n = ++m;
cout << "m = " << m << ", n = " << n << endl;
n = m++;
cout << "m = " << m << ", n = " << n << endl;
cout << "m = " << m++ << endl;
cout << "m = " << m << endl;
cout << "m = " << ++m << endl;
c. Identifying error
i. Basic Syntax
#include<iostream>
using namespace std;
void main(){ /* This is the main function
cout<<'C++ Programming questions and answers';
}
------------
#include<iostream>
using namespace std;
void main(){ // This is the main function
integer a;
float b;
}
-------------
#include<iostream>
using namespace std;
int main(){ // This is another C++ program with some errors
a,b,s int;
d float;
cout<<"The end of the program";
return 0;
}

3|Page
ii. Accept two number and calculate the five arithmetic operation
int sum,sub,mult,div,module;
int a,b;
cout<<"Enter value of a ="
cin<<a;
cout<<"Enter value of b =;
cin<<b;
sum=a+b;
sub=a-bmult=a*b;
div=a/'b';
module=a%b;

4|Page

You might also like