Precedence operators
Q1: Trace
a)
#include<iostream>
using namespace std;
int main()
{
int x=5;
cout<<x<<endl;
cout<<++x<<endl;
cout<<x++<<endl;
cout<<x<<endl;
return 0;
}
Solution
5
6
6
7
-------------------
b)
#include<iostream>
using namespace std;
int main()
{
int x=4, y=10, z=5;
y = x++ + y;
z = ++x + y++;
cout<<x<<y;
return 0;
}
Solution
6 15
If statement
Q2: Write a C++ program that prompts the user to enter a number. Then check
whether it is an even number and greater than 100 or not.
Answer:
#include <iostream>
using namespace std;
int main()
{
int num;
cout << "Enter number: ";
cin >> num;
if(num%2==0)
{cout<<"even number ";
if(num>100)
cout<<"and greater than 100";
else
cout<<"less than 100";
else
cout<<"odd number.";
return 0;
}
Switch statement
Q3: Write a C++ program that make a simple calculator. [use switch statement]
#include <iostream>
using namespace std;
int main()
{
char o;
int num1, num2;
cout << "Enter an operator (+, -, *, /): ";
cin >> o;
cout << "Enter two operands: ";
cin >> num1 >> num2;
switch (o){
case '+':
cout << num1 << " + " << num2 << " = " << num1+num2<<endl; break;
case '-':
cout << num1 << " - " << num2 << " = " << num1-num2<<endl; break;
case '*':
cout << num1 << " * " << num2 << " = " << num1*num2<<endl; break;
case '/':
switch(num2){
case 0:
cout<<"cannot divide by zero\n"; break;
default:
cout << num1 << " / " << num2 << " = " << num1/num2<<endl;
}
break;
default:
cout << "Error! operator is not correct";
}
return 0;
}
Loops
Q4: Write a C++ program that calculate the power.
#include<iostream>
using namespace std;
int main()
{
int num, power, result=1;
cout << " Input a number: ";
cin >> num;
cout << " Input a power: ";
cin >> power;
for (int i = 1; i <= power; i++) {
result*=num;
}
cout << num<< " to the power: " << power << " = "<< result<< endl;
return 0;
}
Q5: Write a C++ program that prompts the user to enter a number. Then check
whether is it prime number or not.
7--> 1, 7 2, 3, 4, 5, 6 {prime}
9--> (3) 2, 3, 4, 5, 6, 7, 8 {not prime}
#include <iostream>
using namespace std;
int main()
{
int num, isPrime = 1;
cout << " Input a number to check prime or not: ";
cin>> num;
int a = 2;
while( a <= num/2 ) {
if (num % a == 0) {
isPrime=0;
break;
}
a++;
}
if (isPrime == 1) {
cout << " The entered number is a prime number. \n";
}
else {
cout << " The number you entered is not a prime number. \n";
}
}
Nested Loops
Q6: Write a C++ program that display the following patterns:
a)
*
**
***
****
*****
#include <iostream>
using namespace std;
int main()
{
int i,star,rows;
cout << " Input number of rows: ";
cin >> rows;
for(i=1;i<=rows;i++) {
for(star=1;star<=i;star++)
cout<<"*";
cout<<endl;
}
}
b)
**
***
****
*****
#include <iostream>
using namespace std;
int main()
{
int i,star,rows,space;
cout << " Input number of rows: ";
cin >> rows;
for(i=1;i<=rows;i++) {
for(space=1;space<=rows-i;space++)
cout<<" ";
for(star=1;star<=i;star++)
cout<<"*"<<" ";
cout<<endl;
}
}
Functions
Q7: Write a C++ function that takes an input an integer number and return its factorial
Note: Factorial on n = 1*2*3*...*n
#include <iostream>
using namespace std;
int calFactorial (int num)
{
int fact=1;
for (int i = 1; i <= num; ++i)
fact *= i;
return fact;
}
int main()
{
int n;
cout << "Enter a positive integer: ";
cin >> n;
int factorial=calFactorial(n);
cout<< "Factorial of "<<n<<" = "<<factorial;
return 0;
}
Recursion Functions
Q8: Write a C++ recursive function that takes an input an integer number and return
its factorial
Note: Factorial on n = 1*2*3*...*n
5!=5*4*3*2*1
5!=5*4!
4!=4*3!
3!=3*2!
2!=2*1!
1!=1
General case: Factorial(n)=n*factorial(n-1)
Base case: factorial(1)=1
#include<iostream>
using namespace std;
int Factorial (int );
int main()
int n, result;
cout<<"enter number: ";
cin>>n;
result=Factorial(n);
cout<<"result is: "<< result;
cout<<"\nenter number: ";
cin>>n;
result=Factorial(n);
cout<<"result is: "<< result;
return 0;
int Factorial (int num){
if (num==1)
return 1;
return num*Factorial(num-1);
Call-by-reference functions
Q10: Write a C++ call by reference function calFactorial that takes an input an
integer number (as a call by value variable) and its factorial (as a call by reference
variable).
Note: Factorial on n = 1*2*3*...*n
#include <iostream>
using namespace std;
void calFactorial (int num, int &fact)
fact=1;
for (int i = 1; i <= num; ++i)
fact *= i;
int main()
{
int n,factorial;
cout << "Enter a positive integer: ";
cin >> n;
calFactorial(n, factorial);
cout<< "Factorial of "<<n<<" = "<<factorial;
return 0;
Static Variables
Q: Trace the following programs:
a)
void counter()
{ int count=0; cout << count++; }
int main()
{
for(int i=0;i<5;i++)
counter();
}
0000
b)
void counter()
{
static int count=0;
cout << count++;
}
int main()
{
for(int i=0;i<5;i++)
counter();
}
01234
Arrays
Q11: Write a C++ program with the following functions.
readArray: that reads an array of 10 numbers.
printArray: function that prints an array of 10 numbers.
searchElement: function that searches the existence of an input number in the
input array. It returns -1 if the number is not found in the array.
sortArray: function that sorts the input array.
arr
#include <iostream>
using namespace std;
void swap(int &, int&);
void sortArray(int[], int);
int elementExist(int [], int , int );
void readArray(int b[], int );
void printArray(const int a[], int size);
int main()
{
int arr[10], num;
cout<<"Enter 10 integer values\n";
for(int i=0;i<10;i++)
{ cout<<"value at index "<<i<<":"; cin>>arr[i]; }
cout<<"Enter 10 integer values for the second array:";
readArray(arr, 10);
cout<<"\n The array before sorting:\n";
printArray(arr, 10);
sortArray(arr, 10);
cout<<"\n The array after sorting: \n";
for(int i=0;i<5;i++)
cout<<arr[i]<<"\t";
cout<<"enter the number that you want to search: "; cin>>num;
int exist=elementExist(arr, 10, num);
if(exist)
cout<<"\n"<<num<<" exist";
else
cout<<"\n"<<num<<" not exist";
return 0;
int elementExist(int a[], int size, int elem)
for(int i=0; i<size; i++)
if(elem==a[i])
return 1;
return 0;
}
void printArray(const int a[], int size){
for(int i=0; i<size; i++ )
cout<<a[i]<<"\t";
void swap(int &a, int&b){
int temp=a; a=b; b=temp;
}
void sortArray(int a[], int size){
for(int i=0;i<size;i++)
for(int j=i+1;j<size;j++)
if(a[i]>a[j])
swap(a[i], a[j]);
}
void readArray(int a[], int size){
for(int l=0;l<size;l++)
{ cout<<"\n value "<<l<<":"; cin>>a[l]; }
}
2-D Arrays
Q11: Write a C++ program with the following functions.
readArray: that reads a 2-D array of numbers with size 2*2.
printArray: function that prints a 2-D array of numbers with size 2*2.
elementIndex: function that prints the index of the first occurrence for an input
number in an input 2-D array.
#include <iostream>
using namespace std;
void readArray(int [][2], int);
void printArray(const int [][2], int);
void elementIndex(const int[][2], int, int);
int main()
int arr[2][2];
cout<<"Enter array values\n";
for(int i=0;i<2;i++)
for(int j=0;j<2;j++)
{ cout<<"\nenter value at row: "<<i<<" and column : "<<j; cin>>arr[i][j];}
cout<<"\n Enter the array values :\n";
readArray(arr, 2);
cout<<"\n the array values are:\n";
printArray(arr, 2);
cout<<"\nenter the number you want to search: "; cin>>num;
elementIndex(arr, 2, num);
return 0;
}
void readArray(int a[][2], int row)
for(int i=0;i<row;i++)
for(int j=0;j<2;j++)
cin>>a[i][j];
void printArray(const int a[][2], int row)
for(int i=0;i<row;i++)
for(int j=0;j<2;j++)
cout<<a[i][j]<<"\t";
cout<<endl;
void elementIndex(const int x[][2], int row, int elem)
for(int i=0;i<row;i++)
{
for(int j=0;j<2;j++)
if(x[i][j]==elem)
{cout<<"\n"<<elem<<" exist at index: "<<i<<" and "<<j; return;}
cout<<"\n"<<elem<<" not exist.";
Pointers
Q13: Trace the following programs:
a)
#include <iostream>
using namespace std;
int main(){
int* ab;
int m;
m=29;
cout<<" Address of m : "<<&m;
cout<<"\n Value of m : "<<m;
ab=&m;
cout<<"\n Now ab is assigned with the address of m.\n";
cout<<" \n Value of pointer ab : "<<ab;
cout<<"\n Content that the pointer ab points to: "<<*ab;
m=34;
cout<<"\n The value of m assigned to 34 now.\n";
cout<<" \n Value of pointer ab : "<<ab;
cout<<" \n Content that the pointer ab points to: "<<*ab;
*ab=7;
cout<<"\n The pointer variable ab is assigned the value 7 now.\n";
cout<<" \n Address of m : "<<&m;
cout<<" \n Value of m : "<<m;
return 0;
Solution
Address of m : 0x7fff24a3f8bc
Value of m : 29
Now ab is assigned with the address of m.
Value of pointer ab : 0x7fff24a3f8bc
Content that the pointer ab points to: 29
The value of m assigned to 34 now.
Value of pointer ab : 0x7fff24a3f8bc
Content that the pointer ab points to: 34
The pointer variable ab is assigned the value 7 now.
Address of m : 0x7fff24a3f8bc
Value of m : 7
Dynamic variable
Trace
#include <iostream>
using namespace std;
int main(){
int *p;
p = new int;
*p = 1250;
cout<<" \n the content of the pointer p is: "<<*p;
Solution
The content of the pointer p is: 1250
Pointers & Arrays
Q14: Write a C++ program that displays all addresses of an array using both array and
pointers
arr arr+1 arr+4
Solution
#include <iostream>
using namespace std;
int main(){
float arr[5];
float *ptr;
cout << "Displaying address using arrays: " << endl;
for (int i = 0; i < 5; ++i) {
cout << "&arr[" << i << "] = " << &arr[i] << endl;
cout<<"Displaying address using pointers notation: "<< endl;
for (int i = 0; i < 5; ++i) {
cout << "arr+ " << i << " = "<<arr + i <<endl;
ptr = arr; // ptr = &arr[0]
cout<<"\n Displaying address using pointers: "<< endl;
for (int i = 0; i < 5; ++i) {
cout << "ptr + " << i << " = "<< ptr + i << endl;
return 0;
Q14: Write a C++ program that insert and display data entered by using pointer
notation.
arr
#include <iostream>
using namespace std;
int main() {
float arr[5];
float* ptr=arr;
// Inserting data using pointer notation
cout << "Enter 5 numbers: ";
for (int i = 0; i < 5; ++i) {
cin >> *(ptr + i) ; //cin>>arr[i];
// Displaying data using pointer notation
cout << "Displaying data: " << endl;
for (int i = 0; i < 5; ++i) {
cout << *(arr+ i) << endl ; //cout<<arr[i];
// Displaying data in reverse order
cout << "Displaying data in reverse order: " << endl;
for (int i = 4; i >=0; --i) {
cout << arr[i] << endl ; //cout<<(arr+i);
}
return 0;
Dynamic Arrays
Q16: Write a c++ program that create a dynamic array then use pointers to insert and
display its elements.
#include <iostream>
using namespace std;
int main(){
int *arr;
int arrSize;
cout << "Enter the array size: ";
cin >> arrSize;
arr = new int [arrSize];
// Inserting data using pointer notation
cout << "\n Enter "<<arrSize<<" numbers:\n ";
for (int i = 0; i < arrSize; ++i)
cin >> *(arr + i) ;
// Displaying data using pointer notation
cout << "\n\n Displaying data: " << endl;
for (int i = 0; i < arrSize; ++i)
cout << *(arr+ i) << endl ;
return 0;
Q17: Write a c++ program that create a dynamic array then define the following
functions:
readArray: to insert its elements.
printArray: to display its elements.
sortArray: to sort the dynamic array.
#include <iostream>
using namespace std;
void readArray(int *, int);
void printArray(int [], int);
void sortArray(int*, int);
void swap(int*, int*);
int main(){
int *a,n;
cout<<" Input the number of elements to store in the array : ";
cin>>n;
a=new int[n];
readArray(a, n);
sortArray(a, n);
cout<<"\n The elements in the array after sorting : \n";
printArray(a, n);
return 0;
void readArray(int *a, int n)
cout<<" \n Input "<< n<< "number of elements in the array : ";
for(int i=0;i<n;i++)
{
cout<<"\n element # "<<i+1<< ": " ;
cin>>*(a+i); // cin>>a[i]
void sortArray(int *a, int n){
for(int i=0;i<n;i++)
for(int j=i+1;j<n;j++)
if( *(a+i) > *(a+j))
swap(&(a+i), &(a+j))
void printArray(int a[], int n){
for(int i=0;i<n;i++)
cout<<" element # "<<i+1<< "has the value: "<<*(a+i);
}
void swap(int *a, int *b){
int tmp = *a;
*a = *b;
*b = tmp;
Pointer to Pointer
int *ptr; // pointer to an int, one asterisk
1
int **ptrptr; // pointer to a pointer to an int, two asterisks
2
Example:
int value = 5;
1
2
int *ptr = &value;
3
std::cout << *ptr; // dereference pointer to int to get int value
4
5
int **ptrptr = &ptr;
6
std::cout << **ptrptr; // first dereference to get pointer to int, second
7
dereference to get int value
ptrptr
ptr
value
105320
80024
5
259081
105320
80024
Output
Note that you can not set a pointer to a pointer directly to a value:
1 int value = 5;
2 int **ptrptr = &&value; // not valid
However, a pointer to a pointer can be set to null:
1 int **ptrptr = nullptr; // use 0 instead prior to C++11