C++ Solved Problems
C++ Solved Problems
Problems
By Tedros Salih
8/17/2017
1
Warning and Disclaimer
This book is prepared as introductory part for showing how a given
problem can be solved using a C++ programing language. Every effort
has been made to make this book as complete and as accurate as
possible, but no warranty or appropriateness is implied. The author
and the publisher shall have neither responsibility to any person or
entity with respect to any loss or damages arising from the
information contained in this book.
E-mail: [email protected]
2
Contents
Frequently asked C++ programs ..................................................................................................... 7
C++ program to check perfect number ....................................................................................... 7
C++ program to check the given number is Armstrong number or not .................................... 10
C++ program to check a given number is prime number or not ............................................... 13
C++ program to check a given number is strong number or not .............................................. 18
C++ program for odd or even number ...................................................................................... 20
C++ program for palindrome numbers .................................................................................... 23
C++ program for solving quadratic equation ............................................................................ 26
C++ program to find Fibonacci series ...................................................................................... 27
C++ program to find factorial of a number ............................................................................... 30
C++ program for Floyd’s triangle ............................................................................................. 33
C++ program to print Pascal triangle. ....................................................................................... 34
C++ program to print multiplication table ................................................................................ 35
C++ program to print hello world without using semicolon ........................................................ 36
C++ program to reverse any number ........................................................................................ 37
Using while loop: .................................................................................................................. 37
Using for loop: ...................................................................................................................... 38
using recursion: ..................................................................................................................... 39
C++ program to sum digits of given number ............................................................................ 39
Using while loop: .................................................................................................................. 39
using for loop: ....................................................................................................................... 40
using recursion: ..................................................................................................................... 40
C++ Program to find power of a number .................................................................................. 41
C++ program to add and subtract two numbers ........................................................................ 42
To add two numbers without using addition operator: ......................................................... 42
To subtract two numbers without using subtraction operator:.............................................. 43
C++ program to compare numbers ........................................................................................... 43
To find largest among three numbers using binary minus operator: .................................... 43
To find largest among three numbers using conditional operator: ....................................... 44
To find largest of ‘N’ numbers : .......................................................................................... 44
C++ Program to find out generic root of a number ................................................................. 45
C++ Program to find prime factors of a number ..................................................................... 46
Program in C++ to print 1 to 100 without using loop ............................................................... 46
3
C++ program with digits ........................................................................................................... 47
Extract digits from integer in C++ language: ....................................................................... 47
To count number of digits in a number:................................................................................ 48
C++ program for L.C.M and H.C.F. ......................................................................................... 49
LCM program in C++ with two numbers : ........................................................................... 50
LCM program in C++ with multiple numbers : .................................................................... 51
HFC program in C++ with two numbers : ........................................................................... 51
HCF program with multiple numbers in c++........................................................................ 53
Swapping....................................................................................................................................... 54
C++ program to swap two numbers .......................................................................................... 54
using Temp............................................................................................................................ 54
Swapping with out Temp ...................................................................................................... 54
Swapping in C++ without temporary variable .............................................. 54
C++ program for swapping of two arrays ................................................................................. 55
C++ program to swap between two array of string ................................................................. 56
Conversion ( Number System )..................................................................................................... 57
C++ Program to convert decimal to binary .............................................................................. 57
C++ program to convert decimal to octal ................................................................................. 58
C++ program to convert Decimal to hexadecimal ................................................................... 59
C++ program to convert Octal to binary ................................................................................... 61
C++ program to convert Octal to decimal conversion .............................................................. 62
C++ program to convert Hexadecimal to binary conversion .................................................... 63
C++ program to convert Binary to octal ................................................................................... 65
C++ program to convert binary number to decimal number .................................................... 67
C++ program Binary to hexadecimal conversion ..................................................................... 68
String ............................................................................................................................................. 69
C++ program to convert uppercase to lowercase ...................................................................... 69
C++ program to convert lowercase in to upper case ................................................................ 69
C++ program for sorting of strings ........................................................................................... 70
C++ program for concatenation of two strings ......................................................................... 70
C++ program to reverse a string ............................................................................................... 72
Using strrev: .......................................................................................................................... 72
without using strrev: ............................................................................................................. 72
using recursion : ................................................................................................................. 73
4
C++ program to compare two strings ....................................................................................... 73
C++ program to copy String ..................................................................................................... 74
Matrix ............................................................................................................................................ 76
C++ program to add two matrices ............................................................................................ 76
C++ program to subtract two matrices ..................................................................................... 77
C++ program to multiply two matrices .................................................................................... 79
C++ program to find the sum of the diagonal matrix ............................................................... 82
C++ program to find out transport of a matrix ......................................................................... 83
C++ program to find inverse of a matrix .................................................................................. 84
C++ program to display lower triangular matrix ...................................................................... 86
C++ program to display upper triangular matrix ...................................................................... 87
C++ program to multiply matrix using Strassen ....................................................................... 88
C++ program to find determinant of a matrix ........................................................................... 90
C++ program to find out the sum of series 1 + 2 + …. + n ...................................................... 92
C++ program to find out the sum of series 1^2 + 2^2 + …. + n^2 ........................................... 93
C++ program to find out the sum of series 1^3 + 2^3 + …. + n^3 ........................................... 94
C++ program to find out the sum of in A.P. series ................................................................... 94
C++ program to find out the sum of G.P series ........................................................................ 96
C++ program to find out the sum of infinite G.P. series: geometric progression ..................... 97
Array ............................................................................................................................................. 98
C++ program to find the largest element in an array ................................................................ 98
C++ program to find the second largest element in an array .................................................... 99
C++ program to remove duplicated elements in an array ......................................................... 99
Sorting ......................................................................................................................................... 101
Bubble sort using C++ program............................................................................................. 101
Insertion sort using C++ program ........................................................................................... 102
Selection sort using C++ program .......................................................................................... 103
Quick sort using C++ program ........................................................................................... 103
Merge sort program using C++ program ................................................................................ 104
Recursion .................................................................................................................................... 107
Factorial program by recursion in C++................................................................................... 107
GCD program by recursion in C++ ........................................................................................ 107
Sum of digits of a number using recursion in C++ ................................................................. 108
Power of a number using recursion in C++ ............................................................................ 108
5
Area and volume ......................................................................................................................... 109
C++ program to calculate the area of circle ............................................................................ 109
C++ program to find the area of a triangle ............................................................................. 110
C++ program to find the area of an equilateral triangle.......................................................... 111
C++ program to find the area of a right angled triangle ......................................................... 112
C++ program to find the area of a rectangle ........................................................................... 113
C++ program to find the area of a trapezium .......................................................................... 113
C++ program for area of a cube .............................................................................................. 114
C++ program to find the volume and surface area of cuboids ................................................ 115
C++ program for area of a cylinder ........................................................................................ 117
C++ program to find the volume and surface area of cone .................................................... 118
6
Frequently asked C++ programs
C++ program to check perfect number
while(i<n){
if(n%i==0)
sum=sum+i;
i++;
}
if(sum==n)
cout<<i<<" is a perfect number";
else
cout<<i<<" is not a perfect number";
return 0;
}
Sample output:
Enter a number: 6
6 is a perfect number
7
Code 2:
To check numbers between a given range
#include<iostream>
using namespace std;
int main(){
int n,i,sum;
int min,max;
while(i<n){
if(n%i==0)
sum=sum+i;
i++;
}
if(sum==n)
cout<<" "<<n;
}
return 0;
}
Sample output:
Enter the minimum range: 1
Enter the maximum range: 20
Perfect numbers in given range is: 6
8
Code 3:
To display a perfect numbers with the range of 1 to 100
#include<iostream>
using namespace std;
int main(){
int n,i,sum;
cout<<"Perfect numbers are: ";
for(n=1;n<=100;n++){
i=1;
sum = 0;
while(i<n){
if(n%i==0)
sum=sum+i;
i++;
}
if(sum==n)
cout<<" "<<n;
}
return 0;
}
Sample output:
Enter a number: 6
6 is a perfect number
9
C++ program to check the given number is Armstrong number or not
Definition of Armstrong number or what is an Armstrong
number:
Definition according to C++ programming point of view:
Those numbers which sum of the cube of its digits is
equal to that number are known as Armstrong numbers.
For example 153 since 1^3 + 5^3 + 3^3 = 1+ 125 + 9 =153
Other Armstrong numbers: 370,371,407 etc.
In general definition:
Those numbers which sum of its digits to power of
number of its digits is equal to that number are known
as Armstrong numbers.
Example 1: 153
Total digits in 153 is 3
And 1^3 + 5^3 + 3^3 = 1 + 125 + 27 = 153
Example 2: 1634
Total digits in 1634 is 4
And 1^4 + 6^4 + 3^4 +4^4 = 1 + 1296 + 81 + 64 =1634
Examples of Armstrong numbers: 1, 2, 3, 4, 5, 6, 7, 8, 9,
153, 370, 371, 407, 1634, 8208, 9474, 54748, 92727, 93084,
548834, 1741725
Code 1:
To check a number is Armstrong
#include<iostream>
using namespace std;
int main(){
int num,r,sum=0,temp;
temp=num;
while(num!=0){
r=num%10;
num=num/10;
sum=sum+(r*r*r);
}
if(sum==temp)
cout<<temp<<" is an Armstrong number";
else
cout<<temp<<"is not an Armstrong number";
return 0;
}
10
Sample output:
Enter a number: 153
153 is an Armstrong number
Code 2:
To find Armstrong number between the ranges
#include<iostream>
using namespace std;
int main(){
int num,r,sum,temp;
int min,max;
while(temp!=0){
r=temp%10;
temp=temp/10;
sum=sum+(r*r*r);
}
if(sum==num)
cout<<" "<<num;
}
return 0;
}
Sample output:
Enter the minimum range: 1
Enter the maximum range: 200
Armstrong numbers in given range are: 1 153
11
Code 3:
To check Armstrong number using for loop
#include<iostream>
using namespace std;
int main(){
int num,r,sum=0,temp;
for(temp=num;num!=0;num=num/10){
r=num%10;
sum=sum+(r*r*r);
}
if(sum==temp)
cout<<temp<<" is an Armstrong number";
else
cout<<temp<<"is not an Armstrong number";
return 0;
}
Sample output:
Enter a number: 370
370 is an Armstrong number
Logic of Armstrong number in c
12
C++ program to check a given number is prime number or not
Code 1:
To check prime number
#include<iostream>
using namespace std;
int main(){
int num,i,count=0;
cout<<"Enter a number: ";
cin>>num;
for(i=2;i<=num/2;i++){
if(num%i==0){
count++;
break;
}
}
if(count==0 && num!= 1)
cout<<num<<" is a prime number";
else
cout<<num<<" is not a prime number";
return 0;
}
13
Sample output:
Enter a number: 5
5 is a prime number
Code 2:
To check a prime number between the range
#include<iostream>
using namespace std;
int main(){
int num,i,count=0;
for(num = 1;num<=100;num++){
count = 0;
for(i=2;i<=num/2;i++){
if(num%i==0){
count++;
break;
}
}
if(count==0 && num!= 1)
cout<<" "<<num;
}
return 0;
}
Output:
2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71
73 79 83 89 97
14
Code 3:
#include<iostream>
using namespace std;
int main(){
int num,i,count,n;
cout<<"Enter max range: ";
cin>>n;
for(num = 1;num<=n;num++){
count = 0;
for(i=2;i<=num/2;i++){
if(num%i==0){
count++;
break;
}
}
return 0;
}
Sample output:
Enter max range: 50
2 3 5 7 11 13 17 19 23 29 31 37 41 43 47
15
Code 4:
C++ code to display prime numbers within a range
#include<iostream>
using namespace std;
int main(){
int num,i,count,min,max;
for(num = min;num<=max;num++){
count = 0;
for(i=2;i<=num/2;i++){
if(num%i==0){
count++;
break;
}
}
return 0;
}
Sample output:
Enter min range: 10
Enter max range: 50
11 13 17 19 23 29 31 37 41 43 47
16
Code 5:
C++ program to find sum of prime numbers
#include<iostream>
using namespace std;
int main(){
int num,i,count,min,max,sum=0;
for(num = min;num<=max;num++){
count = 0;
for(i=2;i<=num/2;i++){
if(num%i==0){
count++;
break;
}
}
Sample output:
Enter min range: 50
Enter max range: 100
17
C++ program to check a given number is strong number or not
Code 1:
C++ program to check whether a number is strong or not
#include<iostream>
using namespace std;
int main(){
int num,i,f,r,sum=0,temp;
temp=num;
while(num){
i=1,f=1;
r=num%10;
while(i<=r){
f=f*i;
i++;
}
sum=sum+f;
num=num/10;
}
if(sum==temp)
cout<<temp<<" is a strong number";
else
cout<<temp<<" is not a strong number";
return 0;
}
Sample output:
Enter a number: 145
145 is a strong number
18
Code 2:
C++ program to check strong number with in range
#include<iostream>
using namespace std;
int main(){
int num,i,f,r,sum,temp;
int min,max;
while(temp){
i=1;
f=1;
r=temp%10;
while(i<=r){
f=f*i;
i++;
}
sum=sum+f;
temp=temp/10;
}
if(sum==num)
cout<<" "<<num;
}
return 0;
}
Sample output:
Enter minimum range: 100
Enter maximum range: 100000
Strong numbers in given range are: 145 40585
19
C++ program for odd or even number
Algorithm:
}
Sample output:
Enter any integer: 5
5 is odd number.
Code 2:
C++ program to display odd numbers with in the range
#include<iostream>
using namespace std;
int main(){
int number;
int min,max;
cout<<"Enter the minimum range: ";
cin>>min;
cout<<"Enter the maximum range: ";
cin>>max;
cout<<"Odd numbers in given range are: ";
for(number = min;number <= max; number++)
if(number % 2 !=0)
cout<<" "<<number;
return 0;
}
Sample output:
Enter the minimum range: 1
Enter the maximum range: 20
20
Odd numbers in given ranges are: 1 3 5 7 9 11 13 15 17
19
Code 3:
C++ program to display Even numbers with in the range
#include<iostream>
using namespace std;
int main(){
int number;
int min,max;
if(number % 2 ==0)
cout<<" "<<number;
return 0;
}
Sample output:
Enter the minimum range: 1
Enter the maximum range: 20
Even numbers in given ranges are: 2 4 6 8 10 12 14 16
18 20
Code 4:
Sum of Even numbers in C++
#include<iostream>
using namespace std;
int main(){
int number;
int min,max,sum=0;
21
for(number = min;number <= max; number++)
if(number % 2 ==0)
sum = sum + number;
cout<<"Sum of Even numbers in given range is: "<<sum;
return 0;
Sample output:
Enter the minimum range: 1
Enter the maximum range: 100
Sum of Even numbers in given range is: 2550
Code 5:
Sum of odd and even numbers c++ program
#include<iostream>
using namespace std;
int main(){
int number;
int min,max;
long odd_sum =0,even_sum = 0;
return 0;
Sample output:
Enter the minimum range: 1
Enter the maximum range: 10
Sum of even numbers in given range is: 30
Sum of odd numbers in given range is: 25
22
C++ program for palindrome numbers
Code 1:
#include<iostream>
using namespace std;
int main(){
int num,r,sum=0,temp;
temp=num;
while(num){
r=num%10;
num=num/10;
sum=sum*10+r;
}
if(temp==sum)
cout<<temp<<" is a palindrome";
else
cout<<temp<<" is not a palindrome";
return 0;
}
Sample output:
Enter a number: 131
131 is a palindrome
23
int min,max;
while(temp){
r=temp%10;
temp=temp/10;
sum=sum*10+r;
}
if(num==sum)
cout<<" "<<num;
}
return 0;
}
Sample output:
Enter the minimum range: 1
Enter the maximum range: 50
Palindrome numbers in given range are: 1 2 3 4 5 6 7 8
9 11 22 33 44
Code 3:
Check if a number is a palindrome using for loop
#include<iostream>
using namespace std;
int main(){
int num,r,sum=0,temp;
for(temp=num;num!=0;num=num/10){
r=num%10;
sum=sum*10+r;
}
if(temp==sum)
cout<<temp<<" is a palindrome";
else
cout<<temp<<" is not a palindrome";
24
return 0;
}
Sample output:
Enter a number: 1221
1221 is a palindrome
Code 4:
check if a number is palindrome using recursion
#include<iostream>
using namespace std;
int checkPalindrome(int);
int main(){
int num,sum;
sum = checkPalindrome(num);
if(num==sum)
cout<<num<<" is a palindrome";
else
cout<<num<<" is not a palindrome";
return 0;
}
if(num!=0){
r=num%10;
sum=sum*10+r;
checkPalindrome(num/10);
}
return sum;
}
Sample output:
Enter a number: 25
25 is not a palindrome
25
Definition of Palindrome string:
#include<iostream>
#include<cstring>
using namespace std;
int main(){
char str[50],rev[50];
int i=0,j,count=0;
cout<<"\nEnter a string:";
cin>>str;
while(str[i] !='\0')
{
count++;
i++;
}
for(i=count,j=0;i>=0;i--,j++)
rev[j]=str[i];
rev[j]='\0';
if(strcmp(rev,str))
cout<<"\nThe string is a palindrome";
else
cout<<"\nThe string is not a palindrome";
return 0;
}
#include<iostream>
#include<cmath>
using namespace std;
int main(){
float a,b,c;
float d,root1,root2;
d = b * b - 4 * a * c;
26
cout<<"Roots are complex number.\n";
return 0;
}
return 0;
}
Sample output:
Enter the coefficient of the quadratic equation: 1 2 1
Roots of quadratic equation are: 1 , -1
Algorithm:
What is Fibonacci series?
Fn = Fn-2 + Fn-1
0 , 1 ,1 , 2 , 3 , 5 , 8 , 13 , 21 , 34 , 55 ...
#include<iostream>
using namespace std;
int main(){
int k,r;
long int i=0l,j=1,f;
for(k=2;k<r;k++){
f=i+j;
i=j;
j=f;
cout<<" "<<j;
}
return 0;
}
Sample output:
Enter the number range: 15
FIBONACCI SERIES: 0 1 1 2 3 5 8 13 21 34 55 89 144 233
377
Code 2:
Fibonacci series using array in C++
#include<iostream>
using namespace std;
int main(){
int i,range;
long int arr[40];
arr[0]=0;
arr[1]=1;
for(i=2;i<range;i++){
arr[i] = arr[i-1] + arr[i-2];
}
28
cout<<"Fibonacci series is: ";
for(i=0;i<range;i++)
cout<<" "<<arr[i];
return 0;
}
Sample output:
Enter the number range: 20
Fibonacci series is: 0 1 1 2 3 5 8 13 21 34 55 89 144
233 377 610 987 1597 2584 4181
Code 3:
Fibonacci series in C++ using while loop
#include<iostream>
using namespace std;
int main(){
int k=2,r;
long int i=0l,j=1,f;
while(k<r){
f=i+j;
i=j;
j=f;
cout<<" "<<j;
k++;
}
return 0;
}
Sample output:
Enter the number range: 10
Fibonacci series is: 0 1 1 2 3 5 8 13 21 34
29
Code 4:
Sum of Fibonacci series in C++
#include<iostream>
using namespace std;
int main(){
int k,r;
long int i=0,j=1,f;
long int sum = 1;
for(k=2;k<r;k++){
f=i+j;
i=j;
j=f;
sum = sum + j;
}
return 0;
}
Sample output:
Enter the number range: 4
Sum of Fibonacci series is: 4
Algorithm:
Factorial value
Code 1:
factorial of a number
#include<iostream>
using namespace std;
int main(){
int i=1,f=1,num;
30
cin>>num;
while(i<=num){
f=f*i;
i++;
}
Sample output:
Enter a number: 5
Factorial of 5 is: 120
Code 2:
using for loop
#include<iostream>
using namespace std;
int main(){
int i,f=1,num;
for(i=1;i<=num;i++)
f=f*i;
Code 3:
using function
#include<iostream>
using namespace std;
int findFactorial(int);
int main(){
int i,factorial,num;
factorial = findFactorial(num);
cout<<"Factorial of "<<num<<" is: "<<factorial;
return 0;
31
}
for(i=1;i<=num;i++)
f=f*i;
return f;
}
Sample output:
Enter a number: 8
Factorial of 8 is: 40320
Code 5:
Factorial series in c++
#include<iostream>
using namespace std;
int main(){
unsigned long int f=1;
int i,num,min,max;
for(i=1;i<=num;i++)
f=f*i;
cout<<" "<<f;
}
return 0;
}
Sample output:
Enter the minimum range: 1
Enter the maximum range: 10
Factorial series in given range: 1 2 6 24 120 720 5040
40320 362880 3628800
32
C++ program for Floyd’s triangle
What is Floyd’s triangle?
Example 1:
1
2 3
4 5 6
7 8 9 10
Example 2:
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
16 17 18 19 20 21
Code:
#include<iostream>
using namespace std;
int main(){
int i,j,r,k=1;
cout<<"FLOYD'S TRIANGLE\n\n";
for(i=1;i<=r;i++){
for(j=1;j<=i;j++,k++)
cout<<" "<<k;
cout<<"\n";
}
33
return 0;
}
Sample output:
Enter the range: 10
FLOYD'S TRIANGLE
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31 32 33 34 35 36
37 38 39 40 41 42 43 44 45
46 47 48 49 50 51 52 53 54 55
#include<iostream>
using namespace std;
long fact(int);
int main(){
int line,i,j;
for(i=0;i<line;i++){
for(j=0;j<line-i-1;j++)
cout<<" ";
for(j=0;j<=i;j++)
cout<<" "<<fact(i)/(fact(j)*fact(i-j));
cout<<endl;
}
return 0;
}
34
}
Sample output:
#include<iostream>
using namespace std;
int main(){
int r,i,j,k;
cout<<"Enter the number range: ";
cin>>r;
for(i=1;i<=r;i++){
for(j=1;j<=10;j++)
cout<<"i*j="<<i*j<<" ";
cout<<endl;
}
return 0;
}
Sample Output:
Enter the number range: 5
1*1=1 1*2=2 1*3=3 1*4=4 1*5=5 1*6=6 1*7=7 1*8=8 1*9=9 1*10=10
35
C++ program to print hello world without using semicolon
Code 1:
#include<iostream>
using namespace std;
int main(){
if(cout<<("Hello world")){
}
return 0;
}
Code 2:
#include<iostream>
using namespace std;
int main(){
while(cout<<("Hello world")==0){
}
return 0;
}
Code 3:
#include<iostream>
using namespace std;
int main(){
switch(cout<<("Hello world")==0){
}
return 0;
}
Code 4:
#include<iostream>
using namespace std;
int main(){
for(;cout<<("Hello world")==0;){
}
return 0;
}
Code 5:
#include<iostream>
using namespace std;
int main(){
do
{
}
while(cout<<"hello world"==0);
return 0;
}
36
C++ program with numbers
Code 1:
#include<iostream>
using namespace std;
int main(){
int num,r,reverse=0;
while(num){
r=num%10;
reverse=reverse*10+r;
num=num/10;
}
cout<<"Reversed of number:"<<reverse;
return 0;
}
Sample output:
Enter any number: 12
Reversed of number: 21
Code 2:
Reverse very large numbers beyond the range of long int
The Logic is we accept the number as string
#include<iostream>
#define MAX 1000
using namespace std;
int main(){
char num[MAX];
int i=0,j,flag=0;
while(num[i]){
if(num[i] < 48 || num[i] > 57){
cout<<"Invalid integer number";
37
return 0;
}
i++;
}
cout<<"Reverse: ";
for(j=i-1;j>=0;j--)
if(flag==0 && num[j] ==48){
}
else{
cout<<num[j];
flag =1;
}
return 0;
}
Sample output:
#include<iostream>
using namespace std;
int main(){
int num,r,reverse=0;
for(;num!=0;num=num/10){
r=num%10;
reverse=reverse*10+r;
}
cout<<"Reversed of number:"<<reverse;
return 0;
}
Sample output:
Enter any number: 123
Reversed of number: 321
38
using recursion:
#include<iostream>
using namespace std;
int rev(int );
int main(){
int num,reverse;
reverse=rev(num);
cout<<reverse;
return 0;
}
if(num){
r=num%10;
sum=sum*10+r;
rev(num/10);
}
else
{
return 0;
}
return sum;
}
Sample output:
Enter any number: 456
Reverse of number: 654
#include<iostream>
using namespace std;
int main(){
int num,sum=0,r;
cout<<"Enter a number: ";
cin>>num;
while(num){
r=num%10;
39
num=num/10;
sum=sum+r;
}
cout<<"Sum of digits of number:"<<sum;
return 0;
}
Sample output:
Enter a number: 123
Sum of digits of number: 6
#include<iostream>
using namespace std;
int main(){
int num,sum=0,r;
cout<<"Enter a number: ";
cin>>num;
for(;num!=0;num=num/10){
r=num%10;
sum=sum+r;
}
cout<<"Sum of digits of number:"<<sum;
return 0;
}
Sample output:
Enter a number: 567
Sum of digits of number: 18
using recursion:
#include<iostream>
using namespace std;
int getSum(int);
int main(){
int num,sum;
cout<<"Enter a number: ";
cin>>num;
sum = getSum(num);
40
cout<<"Sum of digits of number: "<<sum;
return 0;
}
if(num!=0){
r=num%10;
sum=sum+r;
getSum(num/10);
}
return sum;
}
Sample output:
Enter a number: 45
Sum of digits of number: 9
41
C++ program to add and subtract two numbers
Algorithm:
In C++ ~ is 1's complement operator. This is equivalent
to:
~a = -b + 1
So, a - ~b -1
= a-(-b + 1) + 1
= a + b – 1 + 1
= a + b
#include<iostream>
using namespace std;
int main(){
int a,b;
int sum;
//sum = a - (-b);
sum = a - ~b -1;
return 0;
}
Sample output:
42
To subtract two numbers without using subtraction operator:
#include<iostream>
using namespace std;
int main(){
int a,b;
int sum;
sum = a + ~b + 1;
return 0;
}
Sample Output:
#include<iostream>
using namespace std;
int main(){
int a,b,c;
cout<<endl<<"Enter 3 numbers: ";
cin>>a>>b>>c;
if(a-b>0 && a-c>0)
cout<<endl<<"Greatest is:"<<a;
else
if(b-c>0)
cout<<endl<<"Greatest is:"<<b;
else
cout<<endl<<"Greatest is:"<<c;
return 0;
}
43
To find largest among three numbers using conditional operator:
#include<iostream>
using namespace std;
int main(){
int a,b,c,big;
cout<<endl<<"Enter 3 numbers:";
cin>>a>>b>>c;
big=(a>b&&a>c?a:b>c?b:c);
cout<<endl<<"Greatest Number is:"<<big;
return 0;
}
To find largest of ‘N’ numbers :
#include<iostream>
using namespace std;
int main(){
int n,num,i;
int big;
cout<<"Enter the values of n: ";
cin>>n;
cout<<"Number"<<1<<":";
cin>>big;
for(i=2;i<=n;i++){
cout<<"Number"<<i<<":";
cin>>num;
if(big<num)
big=num;
}
return 0;
}
Sample Output:
Enter the values of n:
Number 1: 12
Number 2: 32
Number 3: 35
Largest number is: 35
44
C++ Program to find out generic root of a number
Code1:
#include<iostream>
using namespace std;
int main(){
while(num>10){
sum=0;
while(num){
r=num%10;
num=num/10;
sum+=r;
}
if(sum>10)
num=sum;
else
break;
}
cout<<"\nSum of the digits in single digit is:"<<sum;
return 0;
}
Code 2:
#include<iostream>
using namespace std;
int main(){
int num,x;
cout<<"Enter any number: ";
cin>>num;
cout<<"Generic root:"<<((x=num%9)?x:9);
return 0;
}
Sample output:
Enter any number: 731
Generic root: 2
45
It sum of digits of a number unit we don't get a single
digit. For example:
Generic root of 456: 4 + 5 + 6 = 15 since 15 is two
digit numbers so 1 + 5 = 6
So, generic root of 456 = 6
#include<iostream>
using namespace std;
int main(){
int num,i=1,j,k;
cout<<"\nEnter a number:";
cin>>num;
while(i<=num){
k=0;
if(num%i==0){
j=1;
while(j<=i){
if(i%j==0)
k++;
j++;
}
if(k==2)
cout<<"\n is a prime factor"<<i;
}
i++;
}
return 0;
}
print(num);
return 0;
}
46
}
Output:
Sample output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
94 95 96 97 98 99 100
#include<iostream>
using namespace std;
int main(){
int num,temp,factor=1;
cout<<"Enter a number: ";
cin>>num;
temp=num;
while(temp){
temp=temp/10;
factor = factor*10;
}
return 0;
}
Sample output:
Enter a number: 123
Each digits of given number are: 1 2 3
47
To count number of digits in a number:
Code 1:
Count the number of digits in c++ programming language
#include<iostream>
using namespace std;
int main(){
int num,count=0;
while(num){
num=num/10;
count++;
}
cout<<"Total digits is:"<<count;
return 0;
}
Sample output:
Enter a number: 23
Total digits is: 2
Code 2:
C++ code to count the total number of digit using for loop
#include<iostream>
using namespace std;
int main(){
int num,count=0;
for(;num!=0;num=num/10)
count++;
Sample output:
Enter a number: 456
Total digits is: 3
48
Code 3:
Count the digits of a given number in c++ language using recursion
#include<iostream>
using namespace std;
int countDigits(int);
int main(){
int num,count;
if(num!=0){
count++;
countDigits(num/10);
}
return count;
}
Sample output:
Enter a number: 1234567
Total digits is: 7
49
LCM program in C++ with two numbers :
Code 1:
#include<iostream>
using namespace std;
int main(){
int n1,n2,x,y;
cout<<"Enter a number: ";
cin>>n1>>n2;
x=n1,y=n2;
while(n1!=n2){
if(n1>n2)
n1=n1-n2;
else
n2=n2-n1;
}
cout<<"L.C.M="<<x*y/n1;
return 0;
}
Code 2:
#include<iostream>
using namespace std;
int lcm(int,int);
int main(){
int a,b,l;
cout<<"Enter any two positive integers ";
cin>>a>>b;
if(a>b)
l = lcm(a,b);
else
l = lcm(b,a);
return 0;
}
int temp = a;
while(1){
if(temp % b == 0 && temp % a == 0)
break;
temp++;
}
return temp;
}
50
LCM program in C++ with multiple numbers :
#include<iostream>
using namespace std;
int lcm(int,int);
int main()
{
int a,b=1;
cout<<"Enter positive integers. To quit press zero.";
while(1){
cin>>a;
if(a<1)
break;
else if(a>b)
b = lcm(a,b);
else
b = lcm(b,a);
}
cout<<"LCM is "<<b;
return 0;
}
int temp = a;
while(1){
if(temp % b == 0 && temp % a == 0)
break;
temp++;
}
return temp;
}
51
Logic of HCF or GCD of any two numbers:
int x,y,m,i;
cin>>x>>y;
if(x>y)
m=y;
else
m=x;
for(i=m;i>=1;i--){
if(x%i==0&&y%i==0){
cout<<"\nHCF of two number is : "<<i ;
break;
}
}
return 0;
}
52
code 2:
#include<iostream>
int main(){
int n1,n2;
cout<<"\nEnter two numbers:";
cin>>n1>>n2;
while(n1 != n2){
if(n1>n2-1)
n1=n1-n2;
else
n2=n2-n1;
}
cout<<"\nGCD="<<n1;
return 0;
}
#include<iostream>
using namespace std;
int gcd(int ,int );
int main(){
int x,y=-1;
cout<<"Insert numbers. To exit insert zero: ";
while(1){
cin>>x;
if(x<1)
break;
else if(y==-1)
y=x;
else if (x<y)
y=gcd(x,y);
else
y=gcd(y,x);
}
cout<<"GCD is "<<y;
return 0;
}
53
}
}
return i;
}
Swapping
using Temp
#include<iostream>
using namespace std;
int main(){
int a,b,temp;
cout<<"Enter any two integers: ";
cin>>a>>b;
cout<<"Before swapping a="<<a<<"and b="<<b;
temp = a;
a = b;
b = temp;
cout<<"\nAfter swapping a="<<a<<"and b="<<b;;
return 0;
}
#include<iostream>
using namespace std;
int main(){
int a=5,b=10;
//process one
a=b+a;
b=a-b;
a=a-b;
cout<<"a= "<<a<<" b="<<b<<endl;
54
//process two
a=5;b=10;
a=a+b-(b=a);
cout<<"a= "<<a<<" b="<<b<<endl;
//process three
a=5;b=10;
a=a^b;
b=a^b;
a=b^a;
cout<<"a= "<<a<<" b="<<b<<endl;
//process four
a=5;b=10;
a=b-~a-1;
b=a+~b+1;
a=a+~b+1;
cout<<"a= "<<a<<" b="<<b<<endl;
//process five
a=5,b=10;
a=b+a,b=a-b,a=a-b;
cout<<"a= "<<a<<" b="<<b;
return 0;
}
#include<iostream>
using namespace std;
int main(){
int a[10],b[10],c[10],i;
cout<<"Enter First array->";
for(i=0;i<10;i++)
cin>>a[i];
cout<<"\nEnter Second array->";
for(i=0;i<10;i++)
cin>>b[i];
cout<<"Arrays before swapping";
cout<<"\nFirst array->";
for(i=0;i<10;i++){
cout<<" "<<a[i];
}
cout<<"\nSecond array->";
for(i=0;i<10;i++){
cout<<" "<<b[i];
}
for(i=0;i<10;i++){
//write any swapping technique
c[i]=a[i];
a[i]=b[i];
55
b[i]=c[i];
}
cout<<"\nArrays after swapping";
cout<<"\nFirst array->";
for(i=0;i<10;i++){
cout<<" "<<a[i];
}
cout<<"\nSecond array->";
for(i=0;i<10;i++){
cout<<" "<<b[i];
}
return 0;
}
#include<iostream>
using namespace std;
int main(){
string a[10],b[10],c[10];
int i;
cout<<"Enter First array string->"<<endl;
for(i=0;i<10;i++)
cin>>a[i];
cout<<"\nEnter Second array string->"<<endl;
for(i=0;i<10;i++)
cin>>b[i];
cout<<"Arrays of string before swapping"<<endl;
cout<<"\nFirst array string->";
for(i=0;i<10;i++){
cout<<" "<<a[i]<<endl;
}
cout<<"\nSecond array string->";
for(i=0;i<10;i++){
cout<<" "<<b[i]<<endl;
}
for(i=0;i<10;i++){
//write any swapping technique
c[i]=a[i];
a[i]=b[i];
b[i]=c[i];
}
cout<<"\nArrays of string after swapping"<<endl;
cout<<"\nFirst array string->"<<endl;
for(i=0;i<10;i++){
cout<<" "<<a[i]<<endl;
}
cout<<"\nSecond array string->";
for(i=0;i<10;i++){
cout<<" "<<b[i]<<endl;
}
return 0;
}
56
Conversion ( Number System )
C++ Program to convert decimal to binary
#include<iostream>
using namespace std;
int main(){
int binaryNumber[100],i=1,j;
while(quotient!=0){
binaryNumber[i++]= quotient % 2;
quotient = quotient / 2;
}
return 0;
Sample output:
Algorithm:
57
Following steps describe how to convert decimal to
binary
#include<iostream>
using namespace std;
int main(){
int octalNumber[100],i=1,j;
while(quotient!=0){
octalNumber[i++]= quotient % 8;
quotient = quotient / 8;
}
58
cout<<"Equivalent octal value of decimal number "<<decimalNumber<<"= ";
for(j = i -1 ;j> 0;j--)
cout<<octalNumber[j];
return 0;
Sample output:
quotient = decimalNumber;
while(quotient!=0){
temp = quotient % 16;
hexadecimalNumber[i++]= temp;
quotient = quotient / 16;
}
return 0;
}
59
Sample output:
60
C++ program to convert Octal to binary
#include<iostream>
#define MAX 1000
using namespace std;
int main(){
char octalNumber[MAX];
long int i=0;
cout<<"Enter any octal number: ";
cin>>octalNumber;
return 0;
}
Sample output:
Algorithm:
Octal to binary conversion method:
Octal Binary
0 000
61
1 001
2 010
3 011
4 100
5 101
6 110
7 111
Octal to binary table
So (65201)8 = (110101010000001)2
C++ program to convert Octal to decimal conversion
#include<iostream>
#include<cmath>
using namespace std;
int main(){
while(octal!=0){
decimal = decimal + (octal % 10) * pow(8,i++);
octal = octal/10;
}
return 0;
}
Sample output:
62
Algorithm:
Octal to decimal conversion method:
Step 1: 1 * 8 ^0 = 1*1 =1
Step 2: 0 * 8 ^1 = 0*8 =0
Step 3: 4 * 8 ^2 = 4*64 =256
Step 4: 3 * 8 ^3 = 3*512 =1536
#include<iostream>
#define MAX 1000
using namespace std;
int main(){
char binaryNumber[MAX],hexaDecimal[MAX];
long int i=0;
63
case '9': cout<<"1001"; break;
case 'A': cout<<"1010"; break;
case 'B': cout<<"1011"; break;
case 'C': cout<<"1100"; break;
case 'D': cout<<"1101"; break;
case 'E': cout<<"1110"; break;
case 'F': cout<<"1111"; break;
case 'a': cout<<"1010"; break;
case 'b': cout<<"1011"; break;
case 'c': cout<<"1100"; break;
case 'd': cout<<"1101"; break;
case 'e': cout<<"1110"; break;
case 'f': cout<<"1111"; break;
default: cout<<"\nInvalid hexadecimal digit "<<hexaDecimal[i];
return 0;
}
i++;
}
return 0;
}
Sample output:
Algorithm:
Hexadecimal to binary conversion method:
Hexadecimal Binary
0 0000
1 0001
2 0010
3 0011
4 0100
5 0101
6 0110
7 0111
8 1000
64
9 1001
A 1010
B 1011
C 1100
D 1101
E 1110
F 1111
Hexadecimal to binary table
Hexadecimal number: 6 5 B 2
Binary values: 0110 0101 1011 0010
So (65B2)16 = (0110010110110010)2
#include<iostream>
using namespace std;
int main(){
while(binaryNumber!=0){
remainder=binaryNumber%10;
octalNumber=octalNumber+remainder*j;
j=j*2;
binaryNumber=binaryNumber/10;
}
return 0;
}
65
Sample output:
Alogrithm:
Binary to octal conversion method:
So (1011010101001101)2 = (132515)8
66
C++ program to convert binary number to decimal number
#include<iostream>
using namespace std;
int main(){
while(binaryNumber!=0){
remainder=binaryNumber%10;
decimalNumber=decimalNumber+remainder*j;
j=j*2;
binaryNumber=binaryNumber/10;
}
return 0;
}
Sample output:
Algorithm:
Binary number system: It is base 2 number system which
uses the digits from 0 and 1.
67
Binary number to decimal conversion with example:
#include<iostream>
using namespace std;
int main(){
while(binaryNumber!=0){
remainder=binaryNumber%10;
hexadecimalNumber=hexadecimalNumber+remainder*j;
j=j*2;
binaryNumber=binaryNumber/10;
}
return 0;
}
Sample output:
68
String
C++ program to convert uppercase to lowercase
#include<iostream>
using namespace std;
int main(){
char str[20];
int i,j=0;
cout<<"Enter any string->";
cin>>str;
cout<<"The string is->"<<str;
while(str[j] !='\0')
{
j++;
}
for(i=0;i<=j;i++){
if(str[i]>=65&&str[i]<=90)
str[i]=str[i]+32;
}
cout<<endl<<"The string in lower case is->"<<str;
return 0;
}
Algorithm:
ASCII value of 'A' is 65 while 'a' is 97. Difference
between them is 97 – 65 = 32
So if we will add 32 in the ASCII value of 'A' then it
will be 'a' and if will we subtract 32 in ASCII value
of 'a' it will be 'A'. It is true for all alphabets.
In general rule:
Upper case character = Lower case character – 32
Lower case character = Upper case character + 32
C++ program to convert lowercase in to upper case
#include<iostream>
using namespace std;
int main(){
char str[20];
int i,j=0;
cout<<"Enter any string->";
cin>>str;
cout<<"The string is->"<<str;
while(str[j] !='\0')
{
j++;
}
for(i=0;i<=j;i++){
if(str[i]>=97&&str[i]<=122)
str[i]=str[i]-32;
69
cout<<endl<<"The string in upper case is->"<<str;
return 0;
}
Algorithm:
70
int i=0,j=0;
char str1[20],str2[20];
cout<<"Enter first string\n";
cin>>str1;
cout<<"Enter second string\n";
cin>>str2;
cout<<"Before concatenation the strings are\n";
cout<<str1<<endl;
cout<<str2<<endl;
while(str1[i]!='\0'){
i++;
}
while(str2[j]!='\0'){
str1[i++]=str2[j++];
}
str1[i]='\0';
cout<<"After concatenation the strings are\n";
cout<<str1;
return 0;
}
Code 2:
#include<iostream>
using namespace std;
void stringConcat(char[],char[]);
int main(){
char str1[100],str2[100];
int compare;
stringConcat(str1,str2);
return 0;
}
while(str1[i]!='\0'){
i++;
}
while(str2[j]!='\0'){
71
str1[i] = str2[j];
i++;
j++;
}
str1[i] = '\0';
}
Sample output:
Enter first string: cquestionbank
Enter second string: @blogspot.com
String after concatenation: [email protected]
C++ program to reverse a string
Using strrev:
#include<iostream>
#include<cstring>
using namespace std;
int main(){
char str[50];
char *rev;
cout<<"Enter any string : ";
cin>>str;
rev = strrev(str);
return 0;
}
while(str[++i]!='\0');
while(i>=0)
rev[j++] = str[--i];
rev[j]='\0';
return 0;
72
}
Sample output:
Enter any string : cquestionbank.blogspot.com
Reverse of string is : moc.topsgolb.knabnoitseuqc
using recursion :
#include<iostream>
#define MAX 100
using namespace std;
char* getReverse(char[]);
int main(){
char str[MAX],*rev;
rev = getReverse(str);
if(*str){
getReverse(str+1);
rev[i++] = *str;
}
return rev;
}
Sample output:
73
char str1[100],str2[100];
int compare;
compare = stringCompare(str1,str2);
if(compare == 1)
cout<<"Both strings are equal.";
else
cout<<"Both strings are not equal";
return 0;
}
Sample output:
Enter first string: cquestionbank.blogspot.com
Enter second string: cquestionbank.blogspot.com
Both strings are equal.
#include<iostream>
using namespace std;
void stringCopy(char[],char[]);
74
int main(){
char str1[100],str2[100];
cout<<"Enter first string\n";
cin>>str1;
cout<<"Enter second string\n";
cin>>str2;
stringCopy(str1,str2);
i++;
}
str2[i]='\0';
}
Sample output:
Enter any string: cquestionbank.blogspot.com
After copying: cquestionbank.blogspot.com
75
Matrix
C++ program to add two matrices
#include<iostream>
using namespace std;
int main(){
int a[3][3],b[3][3],c[3][3],i,j;
cout<<"Enter the First 3 X 3 matrix->";
for(i=0;i<3;i++)
for(j=0;j<3;j++)
cin>>a[i][j];
cout<<"\nEnter the Second 3 X 3 matrix->";
for(i=0;i<3;i++)
for(j=0;j<3;j++)
cin>>b[i][j];
cout<<"\nThe First matrix is\n";
for(i=0;i<3;i++){
cout<<"\n";
for(j=0;j<3;j++)
cout<<"\t"<<a[i][j];
}
Algorithm:
76
Suppose two matrices A and B is of same size m X n
For example:
Suppose two matrices A and B of size of 2 X 3 is as
follow:
77
cout<<"\t"<<a[i][j];
}
For example:
Suppose two matrixes A and B of size of 3 X 2 is as
follow:
78
C++ program to multiply two matrices
#include<iostream>
using namespace std;
int main(){
int a[5][5],b[5][5],c[5][5],i,j,k,sum=0,m,n,o,p;
cout<<"\nEnter the row and column of first matrix";
cin>>m>>n;
cout<<"\nEnter the row and column of second matrix";
cin>>o>>p;
if(n!=o){
cout<<"Matrix mutiplication is not possible";
cout<<"\nColumn of first matrix must be same as row of second matrix";
}
else{
cout<<"\nEnter the First matrix->";
for(i=0;i<m;i++)
for(j=0;j<n;j++)
cin>>a[i][j];
cout<<"\nEnter the Second matrix->";
for(i=0;i<o;i++)
for(j=0;j<p;j++)
cin>>b[i][j];
cout<<"\nThe First matrix is\n";
for(i=0;i<m;i++){
cout<<"\n";
for(j=0;j<n;j++){
cout<<"\t"<<a[i][j];
}
79
}
cout<<"\nThe Second matrix is\n";
for(i=0;i<o;i++){
cout<<"\n";
for(j=0;j<p;j++){
cout<<"\t"<<b[i][j];
}
}
for(i=0;i<m;i++)
for(j=0;j<p;j++)
c[i][j]=0;
for(i=0;i<m;i++){ //row of first matrix
for(j=0;j<p;j++){ //column of second matrix
sum=0;
for(k=0;k<n;k++)
sum=sum+a[i][k]*b[k][j];
c[i][j]=sum;
}
}
}
cout<<"\nThe multiplication of two matrix is\n";
for(i=0;i<m;i++){
cout<<"\n";
for(j=0;j<p;j++){
cout<<"\t"<<c[i][j];
}
}
return 0;
}
Alogrithm:
80
Where 1 ≤ i ≤ m and 1 ≤ j ≤ n
For example:
Suppose two matrixes A and B of size of 2 x 2 and 2 x 3
respectively:
81
C++ program to find the sum of the diagonal matrix
#include<iostream>
using namespace std;
int main(){
int a[10][10],i,j,sum=0,m,n;
for(i=0;i<m;i++){
cout<<"\n";
for(j=0;j<m;j++){
cout<<"\t"<<a[i][j];
}
}
for(i=0;i<m;i++){
for(j=0;j<n;j++){
if(i==j)
sum=sum+a[i][j];
}
}
cout<<"\n\nSum of the diagonal elements of a matrix is:"<<sum;
return 0;
}
Sample output:
82
Sum of the diagonal elements of a matrix is: 16
Alogrithm:
Sum of diagonal element of matrix:
for(i=0;i<m;i++){
cout<<"\n";
for(j=0;j<m;j++){
cout<<"\t"<<a[i][j];
}
}
83
for(i=0;i<m;i++)
for(j=0;j<n;j++)
b[i][j]=0;
for(i=0;i<m;i++){
for(j=0;j<n;j++){
b[i][j]=a[j][i];
cout<<"\n"<<b[i][j];
}
}
cout<<"\n\nTraspose of a matrix is -> ";
for(i=0;i<m;i++){
cout<<"\n";
for(j=0;j<m;j++){
cout<<"\t"<<b[i][j];
}
}
return 0;
}
#include<iostream>
using namespace std;
int main(){
int a[3][3],i,j,muL1,muL2;
float determinant=0;
for(i=0;i<3;i++)
{
muL1=a[1][(i+1)%3]*a[2][(i+2)%3];
muL2=a[1][(i+2)%3]*a[2][(i+1)%3];
determinant = determinant + a[0][i]*(muL1 - muL2);
}
cout<<"\nInverse of matrix is: \n\n";
for(i=0;i<3;i++){
for(j=0;j<3;j++)
{
muL1=a[(i+1)%3][(j+1)%3] * a[(i+2)%3][(j+2)%3] ;
84
muL2=a[(i+1)%3][(j+2)%3]*a[(i+2)%3][(j+1)%3];
cout<<"\t"<<(muL1- muL2)/ determinant;
}
cout<<"\n";
}
return 0;
}
The matrix is
3 5 2
1 5 8
3 9 2
Inverse of matrix is:
85
C++ program to display lower triangular matrix
#include<iostream>
using namespace std;
int main(){
int a[3][3],i,j;
cout<<"Enter the 9 elements of matrix: ";
for(i=0;i<3;i++)
for(j=0;j<3;j++)
cin>>a[i][j];
return 0;
}
The matrix is
1 2 3
4 5 6
7 8 9
Setting zero in lower triangular matrix
86
1 2 3
0 5 6
0 0 9
int a[3][3],i,j;
cout<<"Enter the 9 elements of matrix: ";
for(i=0;i<3;i++)
for(j=0;j<3;j++)
cin>>a[i][j];
return 0;
}
Sample output:
Enter the 9 elements of matrix: 1
2
3
4
5
6
7
8
9
87
The matrix is
1 2 3
4 5 6
7 8 9
Setting zero in upper triangular matrix
1 0 0
4 5 0
7 8 9
88
m5= (a[0][0]+a[0][1])*b[1][1];
m6= (a[1][0]-a[0][0])*(b[0][0]+b[0][1]);
m7= (a[0][1]-a[1][1])*(b[1][0]+b[1][1]);
c[0][0]=m1+m4-m5+m7;
c[0][1]=m3+m5;
c[1][0]=m2+m4;
c[1][1]=m1-m2+m3+m6;
return 0;
}
Sample output:
Enter the 4 elements of first matrix: 1
2
3
4
Enter the 4 elements of second matrix: 5
6
7
8
1 2
3 4
The second matrix is
5 6
7 8
After multiplication using
19 22
43 50
89
C++ program to find determinant of a matrix
C++ code for Determinant of 2X2 matrix:
#include<iostream>
using namespace std;
int main(){
int a[2][2],i,j,muL1,muL2;
int determinant=0;
return 0;
}
The matrix is
4 8
3 9
90
C++ code for Determinant of 3X3 matrix:
#include<iostream>
using namespace std;
int main(){
int a[3][3],i,j,muL1,muL2;
int determinant=0;
for(i=0;i<3;i++)
{
muL1=a[1][(i+1)%3]*a[2][(i+2)%3];
muL2=a[1][(i+2)%3]*a[2][(i+1)%3];
determinant = determinant + a[0][i]*(muL1 - muL2);
}
cout<<"\nDeterminant of 3 X 3matrix is: "<<determinant;
return 0;
}
Sample output:
Enter the 9 elements of matrix: 1
2
3
4
5
6
7
8
9
The matrix is
1 2 3
4 5 6
7 8 9
91
Alogrithm:
Determinant is possible only for square matrixes i.e. n
by n matrixes.
Determinant of 2 by 2 matrix:
Determinant of 3 by 3 matrix:
Series
C++ program to find out the sum of series 1 + 2 + …. + n
#include<iostream>
using namespace std;
int main(){
int n,i;
int sum=0;
sum = (n * (n + 1)) / 2;
92
cout<<"Sum of the series: ";
return 0;
}
Sample output:
Mathematical Formula:
#include<iostream>
using namespace std;
int main(){
int n,i;
int sum=0;
sum = (n * (n + 1) * (2 * n + 1 )) / 6;
return 0;
}
Sample output:
93
Enter the n i.e. max values of series: 5
Sum of the series: 1^2 + 2^2 + 3^2 + 4^2 + 5^2 = 55
Mathematical Formula:
int n,i;
int sum=0;
return 0;
}
Sample output:
Enter the n i.e. max values of series: 3
Sum of the series: 1^3 + 2^3 + 3^3 = 36
Mathematical Formula:
Sum of the series 13 + 23 + 33 + … + n3 = (n (n+1)/2)2
C++ program to find out the sum of in A.P. series
#include<iostream>
#include<cmath>
using namespace std;
int main(){
int a,d,n,i,tn;
94
int sum=0;
sum = ( n * ( 2 * a + ( n -1 ) * d ) )/ 2;
tn = a + (n-1) * d;
cout<<"Sum of the series A.P.: ";
for(i=a;i<=tn; i= i + d ){
if (i != tn)
cout<<i<<"+ ";
else
cout<<i<<"= "<<sum;
}
return 0;
}
Sample output:
95
Tn term of A.P. series:
Tn = a + (n-1) d
int a,d,r,i,n,tn;
int sum=0;
return 0;
}
Sample output:
96
Example of G.P. series:
2 4 8 16 32 64
Here common difference is 2 since ratio any two
consecutive numbers for example 32 / 16 or 64/32 is 2.
Tn = arn-1
Sn = a/(1-r) if 1 > r
= a/(r-1) if r > 1
C++ program to find out the sum of infinite G.P. series: geometric progression
#include<iostream>
using namespace std;
int main(){
float a,r;
float sum=0;
if(1 > r)
sum = a/(1-r);
else
sum = a/(r-1);
return 0;
}
Sample output:
2 4 8 16 32 64
Here common difference is 2 since ratio any two
consecutive numbers for example 32 / 16 or 64/32 is 2.
Tn = arn-1
Sn = a/(1-r) if 1 > r
= a/(r-1) if r > 1
Array
C++ program to find the largest element in an array
#include<iostream>
using namespace std;
int main(){
int a[50],size,i,big;
cout<<"\nEnter the size of the array: ";
cin>>size;
cout<<"\nEnter "<<size<<" elements in to the array:";
for(i=0;i<size;i++)
cin>>a[i];
big=a[0];
for(i=1;i<size;i++){
if(big<a[i])
98
big=a[i];
}
cout<<"\nBiggest:"<<big;
return 0;
}
secondbig=a[size-j-1];
for(i=1;i<size;i++){
if(secondbig <a[i] && j != i)
secondbig =a[i];
}
99
if(i==j){
continue;
}
else if(*(p+i)==*(p+j)){
k=j;
size--;
while(k < size){
*(p+k)=*(p+k+1);
k++;
}
j=0;
}
}
}
cout<<"\nThe array after removing duplicates is: ";
for(i=0;i < size;i++){
cout<<endl<<arr[i];
}
return 0;
}
C++ program to delete an element at desired position from an array
#include<iostream>
using namespace std;
int main(){
int a[50],i,pos,size;
cout<<"\nEnter size of the array: ";
cin>>size;
i=0;
while(i!=pos-1)
i++;
while(i<10){
a[i]=a[i+1];
i++;
}
size--;
for(i=0;i<size;i++)
cout<<endl<<a[i];
return 0;
}
#include<iostream>
100
using namespace std;
int main(){
int a[50],size,i,big,small;
big=a[0];
for(i=1;i<size;i++){
if(big<a[i])
big=a[i];
}
cout<<"Largest element: "<<big<<endl;
small=a[0];
for(i=1;i<size;i++){
if(small>a[i])
small=a[i];
}
cout<<"Smallest element: "<<small;
return 0;
}
Sample Output:
Enter the size of the array: 4
Enter 4 elements in to the array: 2 7 8 1
Largest element: 8
Smallest element: 1
Sorting
Bubble sort using C++ program
#include<iostream>
using namespace std;
int main(){
int s,temp,i,j,a[20];
101
//Bubble sorting algorithm
for(i=s-2;i>=0;i--){
for(j=0;j<=i;j++){
if(a[j]>a[j+1]){
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
return 0;
}
Output:
Enter total numbers of elements: 5
Enter 5 elements: 6 2 0 11 9
After sorting: 0 2 6 9 11
Insertion sort using C++ program
#include<iostream>
using namespace std;
int main(){
int i,j,s,temp,a[20];
cout<<"Enter total elements: ";
cin>>s;
for(i=1;i<s;i++){
temp=a[i];
j=i-1;
while((temp<a[j])&&(j>=0)){
a[j+1]=a[j];
j=j-1;
}
a[j+1]=temp;
}
return 0;
}
102
Output:
Enter total elements: 5
Enter 5 elements: 3 7 9 0 2
After sorting: 0 2 3 7 9
Selection sort using C++ program
#include<iostream>
using namespace std;
int main(){
int s,i,j,temp,a[20];
for(i=0;i<s;i++){
for(j=i+1;j<s;j++){
if(a[i]>a[j]){
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
return 0;
}
Output:
Enter total elements: 5
Enter 5 elements: 4 5 0 21 7
The array after sorting is: 0 4 5 7 21
Quick sort using C++ program
#include<iostream>
using namespace std;
void quicksort(int [10],int,int);
int main(){
int x[20],size,i;
103
cout<<"Enter size of the array: ";
cin>>size;
cout<<"Enter "<<size<<" elements: ";
for(i=0;i<size;i++)
cin>>x[i];
quicksort(x,0,size-1);
cout<<"Sorted elements: ";
for(i=0;i<size;i++)
cout<<" "<<x[i];
return 0;
}
void quicksort(int x[10],int first,int last){
int pivot,j,temp,i;
if(first<last){
pivot=first;
i=first;
j=last;
while(i<j){
while(x[i]<=x[pivot]&&i<last)
i++;
while(x[j]>x[pivot])
j--;
if(i<j){
temp=x[i];
x[i]=x[j];
x[j]=temp;
}
}
temp=x[pivot];
x[pivot]=x[j];
x[j]=temp;
quicksort(x,first,j-1);
quicksort(x,j+1,last);
}
}
Output:
Enter size of the array: 5
Enter 5 elements: 3 8 0 1 2
Sorted elements: 0 1 2 3 8
Merge sort program using C++ program
#include<iostream>
using namespace std;
#define MAX 50
104
int main(){
int merge[MAX],i,n;
partition(merge,0,n-1);
return 0;
}
int mid;
if(low<high){
mid=(low+high)/2;
partition(arr,low,mid);
partition(arr,mid+1,high);
mergeSort(arr,low,mid,high);
}
}
int i,m,k,l,temp[MAX];
l=low;
i=low;
m=mid+1;
while((l<=mid)&&(m<=high)){
if(arr[l]<=arr[m]){
temp[i]=arr[l];
l++;
}
else{
temp[i]=arr[m];
m++;
}
i++;
}
105
if(l>mid){
for(k=m;k<=high;k++){
temp[i]=arr[k];
i++;
}
}
else{
for(k=l;k<=mid;k++){
temp[i]=arr[k];
i++;
}
}
for(k=low;k<=high;k++){
arr[k]=temp[k];
}
}
Sample output:
106
Recursion
107
Sum of digits of a number using recursion in C++
#include<iostream>
using namespace std;
int findsum(int);
int main(){
int num,x;
cout<<"\nEnter a number: ";
cin>>num;
x=findsum(num);
cout<<"Sum of the digits of "<<num<<" is: "<<x;
return 0;
}
int r,s;
int findsum(int n){
if(n){
r=n%10;
s=s+r;
findsum(n/10);
}
else
return s;
}
108
Area and volume
C++ program to calculate the area of circle
#include<iostream>
using namespace std;
#define PI 3.141
int main(){
float r, a;
cout<<"Radius: ";
cin>>r;
a = PI * r * r;
cout<<"\n"<<a;
return 0;
}
Pie = 22/7 or
3.14159265358979323846264338327950288419716939937510...
109
C++ program to find the area of a triangle
Area = √(s*(s-a)*(s-b)*(s-c))
Where s = (a + b + c)/2
C++ code:
#include<iostream>
#include<cmath>
using namespace std;
int main(){
float a,b,c;
float s,area;
s = (a+b+c)/2;
area = sqrt(s*(s-a)*(s-b)*(s-c));
return 0;
}
Sample output:
110
C++ program to find the area of an equilateral triangle
C++ code:
#include<iostream>
#include<cmath>
using namespace std;
int main(){
float a;
float area;
area = sqrt(3)/4*(a*a);
cout<<"Area of equilateral triangle is: "<<area;
return 0;
}
Sample output:
111
C++ program to find the area of a right angled triangle
C++ code:
#include<iostream>
using namespace std;
int main(){
float h,w;
float area;
area = 0.5 * h * w;
return 0;
}
Sample output:
112
C++ program to find the area of a rectangle
C++ code:
#include<iostream>
using namespace std;
int main(){
float l,w;
float area;
area = l * w;
cout<<"Area of rectangle is: "<<area;
return 0;
}
Sample output:
Area = (1/2) * (a + b) * h
C++ code:
113
#include<iostream>
using namespace std;
int main(){
float b1,b2,h;
float area;
cout<<"Enter the size of two bases and height of the trapezium : ";
cin>>b1>>b2>>h;
area = 0.5 * ( b1 + b2 ) * h ;
return 0;
}
Sample output:
114
C++ code:
#include<iostream>
using namespace std;
int main(){
float a;
float surface_area,volume;
surface_area = 6 * (a * a);
volume = a * a * a;
return 0;
}
Sample output:
115
Surface_area = 2 *(w*l + l*h + h*w)
C++ code:
#include<iostream>
#include<cmath>
using namespace std;
int main(){
float w,l,h;
float surface_area,volume,space_diagonal;
return 0;
}
Sample output:
116
C++ program for area of a cylinder
C++ code:
#include<iostream>
#include<cmath>
using namespace std;
int main(){
float r,h;
float surface_area,volume;
return 0;
}
117
Sample output:
float r,h;
float surface_area,volume;
118
cout<<"Surface area of cone is: "<<surface_area;
cout<<"\nVolume of cone is : "<<volume;
return 0;
}
Sample output:
119