Essential C++ Programs: From Compound Interest to Structures
COMPOUND INTEREST
#include
#include
#include
int main()
{
float P,R,T,CI;
cout<“enter principal,=”” rate=”” and=”” time=”” :=””>”enter>
cin>>P>>R>>T;
CI=P*pow((1+R/100),T) – P;
cout<“compound interest=”” is=”” :=””>”compound><>
getch();
return 0;
}
FACTORIAL OF A NUMBER
#include
#include
void main()
{
int number,factorial=1;
cout < “enter=”” number=”” to=”” find=”” its=“” factorial:=”” “;=”” cin=””>>number;
for(int i=1;i<>
{
factorial=factorial*i;
}
cout<“factorial of=”” given=”” number=”” is:=”” “=””>”factorial>< factorial=””><>
getch();
}
REVERSE OF A NUMBER
#include
#include
void main()
{ clrscr();
int n, reversedNumber = 0, remainder;
cout < “enter=”” an=”” integer:=””>
cin >> n;
while(n != 0)
{
remainder = n%10;
reversedNumber = reversedNumber*10 + remainder;
n /= 10;
}
cout < “reversed=”” number=” << reversedNumber;
getch();
}
SUM OF DIGITS OF A NUMBER
#include
#include
void main()
{ clrscr();
int n,t,r,sum=0;
cout<<” enter=”” any=”” number=”” :=””>
cin>>n;
t=n;
while(t>0)
{
r=t%10;
sum+=r;
t=t/10;
}
cout<“sum of=”” digits=”” of=”” number=””>”sum><><” is=””>”><>
getch();
}
SUM OF N NATURAL NUMBERS
#include
#include
void main()
{ clrscr();
int i=1,sum=0,n;
cout<“enter no=”” of=”” natural=”” numbers:=””>”enter>
cin>>n;
while(i<>
{
sum+=i;
i++;
}
cout<“sum:>”sum:><>
getch();
}
PRINT RIGHTANGLED TRIANGLE
#include
#include
void main()
{ clrscr();
int rows;
cout < “enter=”” number=”” of=”” rows:=””>
cin >> rows;
for(int i = 1; i <= rows;=””>=>
{
for(int j = 1; j <= i;=””>=>
{
cout < “*=””>
}
cout <>
}
getch();
}
PRINT EQUILATERAL TRIANGLE
#include
#include
void main()
{ clrscr();
int space, rows;
cout <“enter number=”” of=”” rows:=””>”enter>
cin >> rows;
for(int i = 1, k = 0; i <= rows;=”” ++i,=”” k=””>=>
{
for(space = 1; space <= rows-i;=””>=>
{
cout <” >” >
}
while(k != 2*i-1)
{
cout < “*=””>
++k;
}
cout <>
}
getch();
}
FIBONACCI SERIES
#include
#include
void main()
{ clrscr();
int f=0,s=1,t,n;
cout<“enter number=”” of=”” terms=”” of=”” series=”” :=””>”enter>
cin>>n;
cout<><“>”><><“>”>
for(int i=3;i<>
{
t=f+s;
cout<><“>”>
f=s;
s=t;
}
getch();
}
PASCALS TRIANGLE
#include
#include
void main()
{ clrscr();
int rows, coef = 1;
cout < “enter=”” number=”” of=”” rows:=””>
cin >> rows;
for(int i = 0; i < rows;=””>
{
for(int space = 1; space <= rows-i;=””>=>
cout <” >” >
for(int j = 0; j <= i;=””>=>
{
if (j == 0 || i == 0)
coef = 1;
else
coef = coef*(i-j+1)/j;
cout < coef=””>< ” =””>
}
cout <>
}
getch();
}
DIAMOND
#include
#include
void main()
{ clrscr();
int i, j, k;
for(i=1;i<>
{
for(j=i;j<>
{
cout<“>”>
}
for(k=1;k<>
{
cout<>
}
cout<>
}
for(i=4;i>=1;i–)
{
for(j=5;j>i;j–)
{
cout<“>”>
}
for(k=1;k<>
{
cout<>
}
cout<>
}
getch();
}
CALCULATOR
#include
#include
void main()
{ clrscr();
char op;
float num1, num2;
cout < “enter=”” operator=”” either=”” +=”” or=”” -=”” or=”” *=”” or=””>
cin >> op;
cout < “enter=”” two=”” operands:=””>
cin >> num1 >> num2;
switch(op)
{
case ‘+’:
cout <>
break;
case ‘-‘:
cout <>
break;
case ‘*’:
cout <>
break;
case ‘/’:
cout <>
break;
default:
// If the operator is other than +, -, * or /, error message is shown
cout < “error!=”” operator=”” is=”” not=””>
break;
}
getch();
}
LENGTH OF STRING WITHOUT INBUILT FUNCTION
#include
#include
#include
void main()
{
int i,count=0;
char ch[20];
clrscr();
cout<“enter any=”” string:=””>”enter>
gets(ch);
for(i=0;ch[i]!=’\0′;i++)
{
count++;
}
cout<“string length:=””>”string><>
getch();
}
CHECK IF A PALLINDROME
#include
#include
void main()
{ clrscr();
char str[80];
cout<“enter string:=””>”enter>
cin.Getline(str, 80);
int l; //Hold length of string
//finding length of string
for(l = 0; str[l] != ‘\0’; l++);
//Comparing first element with last element till middle of string
int i;
for(i = 0; (i < l/2)=”” &&=”” (str[i]=”=” str[l=”” -=”” i=”” -=”” 1]);=””>
if(i == l/2)
cout <>
else
cout < “not=”” a=””>
getch();
}
CONCATENATE TWO STRINGS
#include
#include
#include
void main()
{ clrscr();
char s1, s2, result;
cout < “enter=”” string=”” s1:=””>
cin.Getline(s1, 80) ;
cout < “enter=”” string=”” s2:=””>
cin.Getline(s2, 80);
result = s1 + s2;
cout < “resultant=”” string=”<< result;
getch();
}
COPY ONE STRING TO ANOTHER WITHOUT INBUILT FUNCTION
#include
#include
#include
void main()
{
char s1[10], s2[10];
clrscr();
cout<<” enter=”” string=”” s1:=””>
cin>>s1;
strcpy(s2, s1);
cout<“string s2:=””>”string><>
getch();
}
REVERSE A STRING
#include
#include
void main( )
{
char str[80];
cout<“enter string:=””>”enter>
cin.Getline(str, 80);
int l; //Hold length of string
for(l = 0; str[l] != ‘\0’; l++); //finding length of string
int temp;
for(int i = 0, j = l – 1; i < l/2;=”” i++,=””>
{
temp = str[i];
str[i] = str[j];
str[j] = temp;
}
cout < “reverse=”” string:=”” “=””>< str=””><>
getch();
}
NO OF WORDS IN A STRING
#include
int main( )
{
char str[80];
cout < “enter=”” a=”” string:=””>
cin.Getline(str,80);
int words = 0; // Holds number of words
for(int i = 0; str[i] != ‘\0’; i++)
{
if (str[i] == ‘ ‘) //Checking for spaces
{
words++;
}
}
cout < “the=”” number=”” of=”” words=” << words+1 << endl;
return 0;
}
REVERSE INDIVIDUAL WORDS IN A STRING
#include
using namespace std;
int main()
{
string s;
int begin,end,i,j=0,len,temp,count=0;
cout<“enter string:=””>”enter>
getline(cin,s);
//To find the length of string
len=s.Length();
//To reverse whole string
for(i=0;i<(len>(len>
{
temp=s[i];
s[i]=s[len-1-i];
s[len-1-i]=temp;
}
//To reverse each word seperately
for(i=0;i<>
{
if(s[i]==’ ‘ || s[i]==’\0’)
{
for(begin=j,end=i-1 ; begin<(i+j) ;=””>(i+j)>
{
temp=s[begin];
s[begin]=s[end];
s[end]=temp;
}
j=i+1;
}
}
cout<><“>”>
return 0;
}
USE STRING COMPARE FUNCTION
#include
using namespace std;
int main( )
{
char str1[80], str2[80];
cout<“enter first=”” string:=””>”enter>
gets(str1);
cout<“enter second=”” string:=””>”enter>
gets(str2);
int i;
for (i = 0; str1[i] == str2[i] && str1[i]!= ‘\0’ && str2[i] != ‘\0’; i++);
if(str1[i] – str2[i] == 0)
cout < “strings=”” are=””>
else
cout < “strings=”” are=”” not=””>
return 0;
}
ADDITION OF MATRICES
#include
using namespace std;
int main()
{
int r, c, a[100][100], b[100][100], sum[100][100], i, j;
cout < “enter=”” number=”” of=”” rows=”” (between=”” 1=”” and=”” 100):=””>
cin >> r;
cout < “enter=”” number=”” of=”” columns=”” (between=”” 1=”” and=”” 100):=””>
cin >> c;
cout < endl=””>< “enter=”” elements=”” of=”” 1st=”” matrix:
=”” “=””><>
// Storing elements of first matrix entered by user.
for(i = 0; i < r;=””>
for(j = 0; j < c;=””>
{
cout < “enter=”” element=”” a”=””>< i=”” +=”” 1=””>< j=”” +=”” 1=””>< “=”” :=””>
cin >> a[i][j];
}
// Storing elements of second matrix entered by user.
cout < endl=””>< “enter=”” elements=”” of=”” 2nd=”” matrix:=”” “=””><>
for(i = 0; i < r;=””>
for(j = 0; j < c;=””>
{
cout < “enter=”” element=”” b”=””>< i=”” +=”” 1=””>< j=”” +=”” 1=””>< “=”” :=””>
cin >> b[i][j];
}
// Adding Two matrices
for(i = 0; i < r;=””>
for(j = 0; j < c;=””>
sum[i][j] = a[i][j] + b[i][j]
// Displaying the resultant sum matrix.
cout < endl=””>< “sum=”” of=”” two=”” matrix=”” is:=”” “=””><>
for(i = 0; i < r;=””>
for(j = 0; j < c;=””>
{
cout < sum[i][j]=””>< ” =””>
if(j == c – 1)
cout <>
}
return 0;
}
SUBTRACTION OF MATRICES
#include
using namespace std;
int main()
{
int m1[3][3], m2[3][3], i, j, m3[3][3];
cout<“\n enter=”” first=”” matrix=”” elements=”” :=””>”\n>
for(i=0; i<3;>3;>
{
for(j=0; j<3;>3;>
{
cout<“>”>
cin>>m1[i][j];
}
}
cout<“\n enter=”” second=”” matrix=”” elements=”” :=””>”\n>
for(i=0; i<3;>3;>
{
for(j=0; j<3;>3;>
{
cout<“>”>
cin>>m2[i][j];
}
}
cout<“\n difference=”” of=”” two=”” matrices=”” :=””>”\n>
for(i=0; i<3;>3;>
{
for(j=0; j<3;>3;>
{
m3[i][j]=m1[i][j]-m2[i][j];
}
}
for(i=0; i<3;>3;>
{
cout<“>”>
for(j=0; j<3;>3;>
{
cout<><>
}
cout<>
}
return 0;
}
MULTIPLICATION OF MATRICES
#include
using namespace std;
int main()
{
int a[10][10], b[10][10], mult[10][10], r1, c1, r2, c2, i, j, k;
cout < “enter=”” rows=”” and=”” columns=”” for=”” first=”” matrix:=””>
cin >> r1 >> c1;
cout < “enter=”” rows=”” and=”” columns=”” for=”” second=”” matrix:=””>
cin >> r2 >> c2;
// If column of first matrix in not equal to row of second matrix,
// ask the user to enter the size of matrix again.
while (c1!=r2)
{
cout < “error!=”” column=”” of=”” first=”” matrix=”” not=”” equal=”” to=”” row=”” of=””>
cout < “enter=”” rows=”” and=”” columns=”” for=”” first=”” matrix:=””>
cin >> r1 >> c1;
cout < “enter=”” rows=”” and=”” columns=”” for=”” second=”” matrix:=””>
cin >> r2 >> c2;
}
// Storing elements of first matrix.
cout < endl=””>< “enter=”” elements=”” of=”” matrix=”” 1:”=””><>
for(i = 0; i < r1;=””>
for(j = 0; j < c1;=””>
{
cout < “enter=”” element=”” a”=””>< i=”” +=”” 1=””>< j=”” +=”” 1=””>< “=”” :=””>
cin >> a[i][j];
}
// Storing elements of second matrix.
cout < endl=””>< “enter=”” elements=”” of=”” matrix=”” 2:”=””><>
for(i = 0; i < r2;=””>
for(j = 0; j < c2;=””>
{
cout < “enter=”” element=”” b”=””>< i=”” +=”” 1=””>< j=”” +=”” 1=””>< “=”” :=””>
cin >> b[i][j];
}
// Initializing elements of matrix mult to 0.
for(i = 0; i < r1;=””>
for(j = 0; j < c2;=””>
{
mult[i][j]=0;
}
// Multiplying matrix a and b and storing in array mult.
for(i = 0; i < r1;=””>
for(j = 0; j < c2;=””>
for(k = 0; k < c1;=””>
{
mult[i][j] += a[i][k] * b[k][j];
}
// Displaying the multiplication of two matrix.
cout < endl=””>< “output=”” matrix:=”” “=””><>
for(i = 0; i < r1;=””>
for(j = 0; j < c2;=””>
{
cout < “=”” “=””><>
if(j == c2-1)
cout <>
}
return 0;
}
TRANSPOSE OF MATRIX
#include
using namespace std;
int main()
{
int a[10][10], trans[10][10], r, c, i, j;
cout < “enter=”” rows=”” and=”” columns=”” of=”” matrix:=””>
cin >> r >> c;
// Storing element of matrix entered by user in array a[][].
cout < endl=””>< “enter=”” elements=”” of=”” matrix:=”” “=””><>
for(i = 0; i < r;=””>
for(j = 0; j < c;=””>
{
cout < “enter=”” elements=”” a”=””>< i=”” +=”” 1=””>< j=”” +=”” 1=””>< “:=””>
cin >> a[i][j];
}
// Displaying the matrix a[][]
cout < endl=””>< “entered=”” matrix:=”” “=””><>
for(i = 0; i < r;=””>
for(j = 0; j < c;=””>
{
cout < “=”” “=””><>
if(j == c – 1)
cout < endl=””><>
}
// Finding transpose of matrix a[][] and storing it in array trans[][].
for(i = 0; i < r;=””>
for(j = 0; j < c;=””>
{
trans[j][i]=a[i][j];
}
// Displaying the transpose,i.E, Displaying array trans[][].
cout < endl=””>< “transpose=”” of=”” matrix:=”” “=””><>
for(i = 0; i < c;=””>
for(j = 0; j < r;=””>
{
cout < “=”” “=””><>
if(j == r – 1)
cout < endl=””><>
}
return 0;
}
FIND ROW SUM AND COLUMN SUM, ADDITION OF TWO DIAGNOLS
#include
#include
void main()
{ clrscr();
int a[3][3];
int i,j,s=0,sum=0;
cout<“enter 9=”” elements=”” of=”” 3*3=”” matrix=””>”enter>
for(i=0;i<>
for(j=0;j<>
cin>>a[i][j];
cout<“matrix entered=”” by=”” you=”” is=””>”matrix>
for(i=0;i<>
{for(j=0;j<>
cout<><“>”>
cout<>
}
for(i=0;i<>
{for(j=0;j<>
s=s+a[i][j];
cout<“sum>”sum><><” row=””>”><>
s=0;
cout<>
}
cout<>
for(i=0;i<>
{for(j=0;j<>
s=s+a[j][i];
cout<“sum>”sum><><” column=””>”><>
s=0;
cout<>
}
cout<>
for(i=0;i<>
sum=sum+a[i][i];
cout<“sum of=”” diagnols=”” elements=”” is=””>”sum><>
getch();
}
ADD MIDDLE ELEMENTS OF MATRIX
#include
using namespace std;
int main()
{ cout<><>;>
int size;
cout<“enter the=”” order=”” matrix=”” (=”” order=”” shoud=”” b=”” in=”” odd=”” demension)=”” :=””>”enter>
cin>>size;
int A[size][size];
for(int i=0;i;i++) >
{
for(int j=0;j<>
{
cout<“enter the=””>”enter><>+1><” value=”” of=”” matrix=”” :=””>”>
cin>>A[i][j];
}
}
cout<“middle row=”” is:=”” “; =”” printing=”” the=”” mid=””>”middle>
for (int i=0;i<>
{
for(int j=0;j<>
{
if (i== size/2)
cout<><>
}
}
cout<“\nmiddle column=”” is:”; =”” printing=”” the=”” mid=””>”\nmiddle>
for (int i=0;i<>
{
for(int j=0;j<>
{
if(j==size/2)
cout<><>
}
}
}
READ THREE STRINGS AND FIND LARGEST AND SMALLEST STRING
#include
#include
#include
void main()
{
char str1[30], srt2[30], str3[30];
cout<“\n enter=”” 3=”” strings=”” ords=”” one-by-one(press=”” enter=”” each=”” time)=””>”\n>
gets(str1);
gets(str2);
gets(str3);
/* Find Longest String */
if(strcmp(str1, str2) > 0)
{
if(strcmp(str1,str3)>0)
cout <“\n the=”” longest=”” string=”” is=”” :=”” “=””>”\n><>
else
cout <“\n the=”” longest=”” string=”” is=”” :=”” “=””>”\n><>
}
else
{
if(strcmp(str2,str3)>0)
cout <“\n the=”” longest=”” string=”” is=”” :=”” “=””>”\n><>
else
cout <“\n the=”” longest=”” string=”” is=”” :=”” “=””>”\n><>
}
/* Find smallest String */
if(strcmp(str1, str2) <>
{
if(strcmp(str1,str3)<>
cout <“\n the=”” smallest=”” string=”” is=”” :=”” “=””>”\n><>
else
cout <“\n the=”” smallest=”” string=”” is=”” :=”” “=””>”\n><>
}
else
{
if(strcmp(str2,str3)<>
cout <“\n the=”” smallest=”” string=”” is=”” :=”” “=””>”\n><>
else
cout <“\n the=”” smallest=”” string=”” is=”” :=”” “=””>”\n><>
}
getch();
}
OCCOURENCE OF A GIVEN CHARACTER IN A STRING
// C++ program to count occurrences of a given
// character
#include
#include
usingnamespacestd;
// Function that return count of the given
// character in the string
intcount(string s, charc)
{
// Count variable
intres = 0;
for(inti=0;i<>
// checking character in string
if(s[i] == c)
res++;
returnres;
}
// Driver code
intmain()
{
string str= “geeksforgeeks”;
charc = ‘e’;
cout < count(str,=”” c)=””><>
return0;
}
UPPER TRIANGLE OF A MATRIX & LOWER TRIANGLE OF A MATRIX
// CPP program to print Lower
// triangular and Upper triangular
// matrix of an array
#include
usingnamespacestd;
// Function to form lower triangular matrix
voidlower(intmatrix[3][3], introw, intcol)
{
inti, j;
for(i=0; i;>
{
for(j=0; j;>
{
if(i<>
{
matrix[i][j] = 0;
}
cout < matrix[i][j]=””>< “=””>
}
cout <>
}
}
// Function to form upper triangular marix
voidupper(intmatrix[3][3], introw, intcol)
{
inti, j;
for(i=0; i;>
{
for(j=0; j;>
{
if(i>j)
{
matrix[i][j] = 0;
}
cout < matrix[i][j]=””>< “=””>
}
cout <>
}
}
// Driver function
intmain()
{
intmatrix[3][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
introw = 3, col = 3;
cout < “lower=”” triangular=”” matrix:=””>
lower(matrix, row, col);
cout < “upper=”” triangular=”” matrix:=””>
upper(matrix, row, col);
return0;
}
STRUCTRE OF STUDENTS READ MARKS AND DISPLAY NAMES
#include
using namespace std;
struct student
{
char name[50];
int roll;
float marks;
} s[10];
int main()
{
cout < “enter=”” information=”” of=”” students:=”” “=””><>
// storing information
for(int i = 0; i < 10;=””>
{
s[i].Roll = i+1;
cout < “for=”” roll=”” number”=””>< s[i].Roll=””>< “,”=””><>
cout < “enter=”” name:=””>
cin >> s[i].Name;
cout < “enter=”” marks:=””>
cin >> s[i].Marks;
cout <>
}
cout < “displaying=”” information:=”” “=””><>
// Displaying information
for(int i = 0; i < 10;=””>
{
cout < “\nroll=”” number:=”” “=””>< i+1=””><>
cout < “name:=”” “=””>< s[i].Name=””><>
cout < “marks:=”” “=””>< s[i].Marks=””><>
}
return 0;
}