C++ Programming
C++ Programming
Table of Contents
Introduction
2. To find the Roots of a Quadratic Equation: Real and Distinct, Repeated and Imaginary
5. To find the sum and average of a list of numbers with and without arrays
9. Manipulation of Matrices
(i) Add and subtract two Matrices.
(ii) Product of two Matrices
Review Exercises
Programming Exercises
Bibliography
C++ Programming
Learning Objectives:
C++ has become the most preferred and widely used programming language today as it
offers a powerful way of solving complex problems. Students need ample practice to master
C++ programming as is the case for any other programming languages. This chapter provides
numerous complete programs written in a simple format to improve clarity and facilitate
better understanding. These C++ programs try to provide answers to problems in a simple
and easy way. Wherever necessary, pictorial descriptions of programs (flowcharts) and
comments in programs have been included. Language tips and other special considerations
are highlighted as notes wherever essential.
The compiler used for the programs is Turbo C++ for Windows 4.5. Any compiler (Visual
C++, gcc or g++ in windows or Linux platforms) can be used with slight modifications in the
programs. The C++ examples listed are designed to give ample practice to the fresh
students. All the topic will concentrate on the fundamentals but exposing the C++‟s behavior,
feature, in-depth and in details. It is combination of the notes, examples, questions and
quizzes. The program topics listed below have been arranged in the learning sequence order
with the aim that students will be able to write programs in C++.
1. To evaluate a polynomial:
(i) Area of Circle
(ii) Volume of Sphere
(iii) Conversion of temperature from Fahrenheit to Celsius
case 3:
{
cout “ Enter the value of radius r \n”;
cin r; //accepting the input
V = 3.1415 * r * r * r; //calculating volume
cout<<”Volume of sphere is :” << V;
break;
}
default:
cout<<” Wrong choice entered! \n”
} // end of switch
} while (ch <=3); // end of do while
getch()
} //END OF PROGRAM
2. To find the Roots of a Quadratic Equation: Real and Distinct, Repeated and
Imaginary
}
getch()
} //END OF PROGRAM
3. To locate a Number in a Given List
(i) Linear search Program
Program 1:
// Locate a number in a list using Binary search
# includeiostream.h //for header files
# includeconio.h //for clrscr()
int search (int[], int, int);
int main ( )
{
int a [100], n, i, f, num, res; // declaring variables
clrscr( );
cout “ To locate a number in a given list ( Binary Search)\n”;
cout “ enter the list size\n”;
cin>> n; //total number in the list
cout “ Enter elements of the list in ascending order” << “\n”;
for (i=0; i<n; ++i )
{
cin>> a[i]; // entering numbers in an array using for loop
}
cout “ Enter the number to be searched:\n”;
cin>>num; //accepting number to be searched
res= search(a, n, num) // calling function search with actual parameters
if (res!=0)
cout << “ \n Number is in list at “ << res+1 << “position”;
else
cout << “ \n Number is not in the list “ ;
getch();
} // End of Program
hi = mid-1;} // else the process is repeated starting from left of mid position/index
} // end of function
}
getch( );
} // End of Program
} // End of Program
5. To find the sum and average of a Given List of Numbers with and without array
// Sum and Average of a list of n numbers using while loop without array
# includeiostream.h //for header files
# includeconio.h //for clrscr()
int main ( )
{
int i = 0, n; //declaring integer type variable
double sum = 0, avg; // declaring sum and average as double
cout “ To find the sum and average of 10 numbers\n”;
while (i < 10 )
{
cout “ Enter a number n \n”;
cin>>n; // entering numbers using while loop
sum = sum + n; // calculating sum
i++ // updating count variable
}
avg = sum/10; // calculating average
cout<< “ Sum is =” << sum << “\n and average is =” << avg;
getch( );
} //End of Program
// Factorial of a number
# includeiostream.h //for header files
# includeconio.h //for clrscr()
void main ()
{
int i,n; // Declaring integer type variable
int f =1, //initializing f to be 1
clrscr ( );
cout “ To Calculate Factorial of a Number\n”;
cout<< “Enter Number = “;
cin>> n;
for(i=1; i<=n; i++) // Loop to calculate factorial of a number
f = f*i; // evaluating factorial
cout “ Factorial of the number” << n << “ is ” <<f;
getch( );
} //End of Program
getch( );
} //End of Program
7. (i) To check whether a Given Number is a Prime Number
cin>> n;
for ( i = 2; i < n; i ++ )//loop to check the divisibility of the number by 2 to n
{
if ( n % i ) ==0) //checking whether the no is divisible by i
{
c++;//flag whose value is incremented whenever the no is divisible by i
}
}
if (c = = 0) // when flag is zero the number is prime
cout n << “ is a prime number\n”;
else
cout n << “ is not a prime number \n”;
}
getch( );
} //End of program
a[j] = a[k];
a[k] = temp;
}
}
}
cout<< “ The numbers arranged in descending order are \n”;
for ( i = 0; i < n; i ++)
{
cout<< a[i] << “\t”;
}
break;
}
cout<< “ \n Do you want to start the program again?:1 = yes; 0 = no \n”
cin>> x;
}
while(x = = 1); // checking do-while loop condition
getch( );
} //End of Program
9 Manipulation of Matrices:
}
cout<< “\n”;
}
do
{
cout<< “ Enter choice 1: Addition 2: Subtraction \n”;
cin>> ch; //accepting choice
switch(ch)
{
case1: // The sum of the two matrices A and B
for (i=0; i<r1; i ++)
{
for (j=0; j< c1; j++)
{
c [i] [j] = a [i] [j] + b[i] [j];
}
cout “ The Sum of the two matrices A and B is :\n”;
for (i = 0; i < r1; i++)
{
for (j = 0; j < c1; j++)
{
cout<< c [i] [j] << “\t“ ; // displaying the sum Matrix
}
cout<< “\n”;
}
break;
}
Case2: // The difference of the two matrices A and B
for (i = 0; i < r1; i ++)
{
for ( j = 0; j < c1; j++)
{
c [i] [j] = a [i] [j] - b[i] [j];
}
cout “ The Difference of the two matrices A and B is :\n”;
for (i = 0; i < r1; i++)
{
for (j = 0; j < c1; j++)
{
cout<< c [i] [j] << “\t“ ; // displaying the difference Matrix
}
cout<< “\n”;
}
break;
}
cout “ \n Do you want to continue? Press 1 for Yes and 0 for \n”;
cin>> x;
}
while ( x = = 1); // checking do-while loop condition
}
else
{
cout<< “ Matrix cannot be added/subtracted\n”; //if-else condition
}
getch ( );
} // End of Program
# includeiostream.h
# includeconio.h
int main ( )
{
int r1, c1, r2, c2,i ,j, k ; // declaring variables
int a[10][10], b[10][10], c[10][10]; //initialization of size of 2 dimendional array for
matrix
clrscr ( );
cout<< “Multiplication of two matrices \n”;
cout<< “ Enter the number of rows and columns of Matrix A\n”;
cin>> r1 >> c1; // size of matrix A
cout<< “ Enter the number of rows and columns of Matrix B\n”;
cin>> r2>> c2; // size of matrix B
if(c1 = = r2) //condition for multiplication
{
cout “ Matrix A and B can be multiplied\n”;
cout<< “\n”;
}
}
else
{
cout<< “ Matrix cannot be multiplied\n”; //if-else condition
}
getch ( );
} // End of Program
10. Determination of
(i) Summing a series /4 = 1 - 1/3 + 1/5 - 1/7 + 1/9....
x
1 x
#include<iostream.h>
#include<conio.h>
#include<math.h>
void main()
{
clrscr();
float y,x1,xn,fx,dfx;
int i=1;
cout<< "Program to calculate the root of equation x*x-12=0"<<endl;
cout<< "Enter the guess value for the root of equation x*x-12=0"<<endl;
cin>>x1;
while(fabs(xn-y)>0.00001)
{
fx=x1*x1-12;
dfx=2*x1;
xn=x1-(fx/dfx);
y=x1;
x1=xn;
cout<<"Iteration "<<i<<". "<<x1<<endl;
i++;
}
Review Exercises
a. x=10,y=10
b. X=10,y=11
c. X=11,y=10
d. X=11,y=11
d. None of these
16. What is the only function all C++ programs must contain?
A. start()
B. system()
C. main()
D. program()
17. Which of the following is a correct comment?
A. */ Comments */
B. ** Comment **
C. /* Comment */
D. { Comment }
Programming Exercises
8. Write a C++ program to calculate the sum of a squares of odd numbers between
112 and 489
9. Write a C ++Program to test the increment and decrement operators with while
loop
10. Write a C++ program to calculate the integer square root of a number
11. Write a Boolean valued function with a single integer parameter It will return True
if the integer is a prime number between 1 and 1000 (note: 1is not usually counted
as a prime number). Hints:
(i) if a number is not prime, it has at least one prime factor less than or equal to its
square root, and (ii) (32 x 32) = 1024 and 1024 > 1000.
12. Write a C++ program to find factorial of integers using recursive function
13. Write a C++ program to find the power of a number using recursive function
14. Write a C++ program through which an unspecified number of elements can be
entered. Find sum and average of these numbers entered and also the deviation
from its mean of the numbers
15. Writ a C++ program to create a Pascal triangle in a square matrix
16. Write C++ program s to determine the root of the following equation to three
significant digits 𝑓 𝑥 = 𝑥 𝑠𝑖𝑛𝑥 + 𝑥 cos 𝑥 = 0 using (i) Bisection method, (ii) Newton
Raphson method and (iii) secant method. Compare the three methods in terms of
order of convergence
17. For the polynomial equation 𝑥 4 − 8 𝑥 3 + 14𝑥 2 9𝑥 − 25 = 0. Does a root exist ? if so,
write a C++program to find one root using Newton Raphson method
18. Write a C++program to find the value of sinx by summing the following series with
an absolute error less than .005 for x = .25
𝑥3 𝑥5
𝑠𝑖𝑛𝑥 = 𝑥 − + … ..
3! 5!
Bibliography