Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 75

Chapter 3

Example 1 :
#include<iostream.h>
void main( )
{
int a;
a = 50;
a = 100 ;
cout<<"\n a = "<<a ;
}

Example 2: Program to illustrates processing of an Array


#include<iostream.h>
#include<conio.h>
void main( )
{
double A[3];
// Array Declaration
// Stores Data in Array
clrscr();
A[2] = 22.22;
A[0] = 11.11;
A[1] = 33.33;
//Read Data from an Array
cout << " A[0] = "<<A[0]<<endl;
cout << " A[1] = "<<A[1]<<endl;
cout << " A[2] = "<<A[2]<<endl;
getch( );
}

Example 3: Program to illustrates processing of an Array


#include<iostream.h>
#include<conio.h>
void main( )
{
const int SIZE = 5; //defines the size N for 5 elements
int A[SIZE] ;
// declares the array's elements as type integer
cout<<"\n Enter "<<SIZE<<" Numbers : \t" ;
for ( int i = 0; i < SIZE; i++)

cin>>A[i];
cout<<"\n In Reverse Order : ";
for( i = SIZE - 1; i >= 0 ; i--)
cout<<"\t"<<A[i];
getch( );
}

Example 4: Program to illustrates Array Index out of Bounds


#include<iostream.h>
#include<conio.h>
void main( )
{
const int SIZE = 4;
float A[SIZE]= { 22.22, 44.40, 12.13, 14.15 };
clrscr();
for(int i = 0; i<7;i++)
//ERROR : index is out of bounds !
cout<<"\t A["<<i<<" ] = " << A[i]<<endl;
getch();
}

Example 5: Program to illustrates unhandled Exceptions


#include<iostream.h>
#include<conio.h>
void main( )
{
float A[4]= { 22.22, 44.40, 12.13, 14.15 };
float x = 12.12;
cout<<" x = "<<x << endl;
A[5555] = 18.77;
//ERROR : index is out of bounds.
cout<<" x = "<<x << endl;
}

Example 6: Program to illustrate Passing Array Elements to a Function (Call by


Value)
#include<iostream.h>

#include<conio.h>
void show( int ) ;
void main( )
{
int number[ ]= { 11, 22, 33, 44, 55 } ;
for( int i = 0; i <= 4; i++)
show(number[i]); //passing element of an array to the function
getch( );
}
void show(int x)
{
cout<<"\t"<<x;
}

Example 7: Program to illustrates Passing Array Elements to a Function (Call by


Reference)
#include<iostream.h>
#include<conio.h>
void display( int* ) ;
void main( )
{
int number[ ]= { 11, 22, 33, 44, 55 } ;
for( int i = 0; i <= 4; i++)
//passing address of an element to the function
display(&number[i]);
getch( );
}
void display (int *x)
{
cout<<"\t"<<*x;
}

Example 8 : Program to illustrates Pointers & Arrays


#include<iostream.h>
#include<conio.h>
void main( )
{
int A [ ] = { 11, 33, 55, 77, 99 } ;
for ( int i = 0; i <= 4; i++)

{
cout<<"\n Element "<< i <<"\t" ;
cout<<" Number "<< A[i] <<"\t" ;
cout<<" Address = "<<&A[i];
}
getch( );
}

Example 9 : Program to illustrates Accessing Array elements using pointers


#include<iostream.h>
#include<conio.h>
void main( )
{
int A [ ] = { 11, 33, 55, 77, 99 } ;
int *j ;
j = &A[0] ;
//assign address of 0th element
for ( int i = 0; i <= 4; i++)
{
cout<<"\n Address = "<< j<< "\t" ;
cout<<" Element = "<< *j ;
j ++ ;
// increment pointer to point next location
}
getch( );
}

Example 10 : Program to illustrates Passing an entire Array to a Function


#include<iostream.h>
#include<conio.h>
void display( int*, int ) ;
void main( )
{
int number [ ] = { 11, 22, 33, 44, 55 } ;
display (&number[0], 5);
}
void display (int *j , int n)
{
for (int i = 0; i <= n-1 ; i++)
{
cout<<"\n Element : "<< *j ;

j ++;

//increment pointer to point to next location

}
}

Example 11 : Program to print Memory Location of an Array


#include<iostream.h>
#include<conio.h>
void main( )
{
int number [ ] = { 11, 22, 33, 44, 55 } ;
cout<<" Number = "<< number ;
getch( );
}

Example 12 : Program to illustrates Two-dimensional Array


#include <iostream.h>
#include <conio.h>
void main( )
{
int emp[4][2] ;
int i ;
for ( i = 0; i <=3 ; i++)
{
cout<<"\n Enter Employee No. and Salary : ";
cin>>emp[i][0]>>emp[i][1];
}
cout<<"\n Emplyoee Number \t Salary ";
for ( i = 0; i <=3 ; i++)
cout<<"\n"<<emp[i][0]<<"\t\t"<<emp[i][1];
getch( );
}

Example 13 : Program to illustrates, Initializing a Two-Dimensional Array


#include <iostream.h>
#include <conio.h>

void main ()
{
int i , j ;
int digits[4][3] = { {0,1,2},{3,4,5},{6,7,8},{9,10,11} } ;
cout<<"\n The digits are :\n" ;
for (i=0 ; i<4 ; i++)
{
for (j=0 ; j<3 ; j++)
cout<<" \n "<<digits[i][j] ;
}
getch () ;
}

Example 14 : Program to illustrates, Initializing a Two-Dimensional Array


#include <iostream.h>
#include <conio.h>
void main ( )
{
int i , j ;
char letters[6][1] = { {'M'},{'i'},{'c'},{'k'},{'e'},{'y'} } ;
cout<<"\n The letters are :\n" ;
for (i = 0, j = 0 ; i < 6 ; i++)
cout<<letters[i][j] ;
getch ( ) ;
}

Example 15 : Program that adds up the individual elements of 2 D Array


#include <iostream.h>
#include <conio.h>
void main ( )
{
int i , j , sum ;
int elements[3][7] = { {0,2,4,6,8,10,12},
{14,16,18,20,22,24,26},
{28,30,32,34,36,38,40} } ;
for ( i = 0,sum = 0 ; i < 3 ; i++)
{
for (j=0 ; j<7 ; j++)

sum = sum + elements[i][j] ;


}
cout <<"\n The result of addition = "<< sum ;
getch ( ) ;
}

Example 16 : Program that display average of marks for each subject


#include <iostream.h>
#include <conio.h>
void main ( )
{
int i , j ;
int marks[3][5] ={ {65,68,75,59,77},
{62,85,57,66,80},
{71,77,66,63,86} } ;
float avg ;
cout<<" \n\t\t ";
for (i = 0 ; i < 5 ; i++)
cout<<"subj"<<i+1<<"\t";
cout<<"\n" ;
for (i=0 ; i<3 ; i++)
{
cout<<" student"<<i+1<<"\t";
for(j=0 ; j<5 ; j++)
cout<<marks[i][j]<<"\t";
cout<<" \n ";
}
cout<< "\n\nThe Average of each subject is : \n" ;
for (j=0 ; j<5 ; j++)
{
cout<<"Subject"<<j+1<<" : " ;
for (i=0,avg=0 ; i<3 ; i++)
avg = avg + float(marks[i][j]) ;
avg = avg / 3 ;
cout.precision(2);
cout<<avg<<"\n";
}
getch () ;
}

Example 17 : Program for Matrix Addition


#include <iostream.h>
#include <conio.h>
void main ( )
{
int i , j , add[3][3] ;
int set1[3][3] = { {1,3,5},{7,9,11},{13,15,17} } ;
int set2[3][3] = { {2,4,6},{8,10,12},{14,16,18} } ;
cout<<"The first 3X3 matrix is :\n";
for (i=0 ; i<3 ; i++)
{
for (j=0 ; j<3 ; j++)
cout<<set1[i][j]<<"\t";
cout<<"\n" ;
}
cout<<"\n" ;
cout<<"The second 3X3 matrix is :\n";
for (i=0 ; i<3 ; i++)
{
for (j=0 ; j<3 ; j++)
cout<<set2[i][j]<<"\t";
cout<<"\n" ;
}
cout<<"\n" ;
for (i=0 ; i<3 ; i++)
{
for (j=0 ; j<3 ; j++)
add[i][j] = set1[i][j] + set2[i][j] ;
}
cout<<"The resultant addition 3X3 matrix is :\n" ;
for (i=0 ; i<3 ; i++)
{
for (j=0 ; j<3 ; j++)
cout<<add[i][j]<<"\t";
cout<<"\n" ;
}
cout<<"\n" ;
getch ( ) ;
}

Example 18 : Program for Matrix Multiplication


#include <iostream.h>
#include <conio.h>
void main ()
{
int i , j , k ;
int set1[3][2] , set2[2][4] , multi[3][4] ;
cout<<" Enter values of first 3X2 matrix :\n" ;
for (i=0 ; i<3 ; i++)
{
for (j=0 ; j<2 ; j++)
cin >> set1[i][j] ;
}
cout<<" Enter values of second 2X4 matrix :\n" ;
for (i=0 ; i<2 ; i++)
{
for (j=0 ; j<4 ; j++)
cin >> set2[i][j] ;
}
cout<<" The first 3X2 matrix is :\n" ;
for (i=0 ; i<3 ; i++)
{
for (j=0 ; j<2 ; j++)
cout <<set1[i][j]<<"\t" ;
cout <<"\n" ;
}
cout <<"\n" ;
cout<<"The second 2X4 matrix is :\n" ;
for (i=0 ; i<2 ; i++)
{
for (j=0 ; j<4 ; j++)
cout <<set2[i][j]<<"\t" ;
cout <<"\n" ;
}
cout <<"\n" ;
for (i=0 ; i<3 ; i++)
for (k=0 ; k<4 ; k++)
multi[i][k] = 0;
for (i=0 ; i<3 ; i++)
{
for (j=0 ; j<2 ; j++)
{
for (k=0 ; k<4 ; k++)
multi[i][k] += set1[i][j]*set2[j][k] ;
}

}
cout<<"The resulting 3X4 matrix is :\n" ;
for (i=0 ; i<3 ; i++)
{
for (j=0 ; j<4 ; j++)
cout<<multi[i][j]<<"\t";
cout <<"\n" ;
}
getch ( ) ;
}

Example 19 : Program to illustrates Processing a 3-D Arrays


#include <iostream.h>
#include <conio.h>
int numzeros(int a[ ][4][3], int n1, int n2, int n3);
void main( )
{
int a[2][4][3]={ { {5,0,2}, {0,0,9}, {4,1,0}, {7,7,7}},
{ {3,0,0}, {8,5,0}, {0,0,0}, {2,0,9}}
};
cout<<"This Array has "<<numzeros(a,2,4,3)<<" zeros. \n";
getch( );
}
int numzeros (int a[ ][4][3], int n1, int n2, int n3)
{
int count = 0;
for (int i = 0; i < n1; i++)
for (int j= 0; j < n2; j++)
for (int k = 0; k < n3; k++)
if (a[i][j][k] == 0) ++count;
return count;
}

Example 20 : Program to illustrates Automatic Storage Classes


#include<iostream.h>
void main( )
{
auto int i = 1 ;
{

auto int i= 2 ;
{
auto int i = 3;
cout<<"\n"<<i ;
}
cout<<"\n "<<i ;
}
cout<<"\n"<<i ;
}

Example 21 : Program to illustrates Register Storage Classes


#include <iostream.h>
#include <conio.h>
void main( )
{
register int x;
for( x =1 ; x <= 10; x++)
cout<<"\n "<<x;
getch( );
}

Example 22 : Program to illustrate Call by Reference


#include <iostream.h>
#include <conio.h>
void swap2 (int*i, int*j);
// Function prototype
void main( )
{
int a = 5, b =25 ;
swap2 (&a, &b);
// Call by passing addresses of variables
cout<<"\n a = "<<a<<"\t b = "<<b;
getch( );
}
void swap2 (int *x, int *y)
// Function definition
{
int temp;
// To interchange 2 numbers
temp = *x;
*x = *y;
*y = temp ;
}

Example 23 : Program to illustrate Call by Value


#include <iostream.h>
#include <conio.h>
void swap1 (int i, int j);
// Function prototype
void main( )
{
int a = 5, b =25 ;
swap1 (a, b);
// Call by Value
cout<<"\n a = "<<a<<"\t b = "<<b;
getch( );
}
void swap1 (int x, int y)
// Function definition
{
int temp;
// To interchange 2 numbers
temp = x;
x = y;
y = temp ;
cout<<"\n x = "<<x<<"\t y= "<<y;
}
Example 24 : Program to illustrate Default Arguments
#include <iostream.h>
#include <conio.h>
double poly (double, double, double = 0, double = 0, double = 0 );
void main( )
{
double x = 2.0003 ;
cout<<" poly(x,7) = " << poly(x,7) << endl;
cout<<" poly(x,7,6) = " << poly(x,7,6) << endl;
cout<<" poly(x,7,6,5) = " << poly(x,7,6,5) << endl;
cout<<" poly(x,7,6,5,4) = " << poly(x,7,6,5,4) << endl;
}
double poly (double x, double a0, double a1, double a2, double a3 )
{
return a0 +(a1 + (a2 + a3 *x)*x)*x;
}

Example 25 : Program to find Recursive function power(x,y) to find x^y


# include <iostream.h>
# include<conio.h>
int power ( int x, int y);
void main( )
{
int x, y ;
cout<< "\n Enter value of x & y : ";
cin>>x>>y;
cout<<"\n"<<x<<" ^ "<<y<<" = "<< power(x,y);
}
int power (int x, int y)
// Recursive power function
{
if (y ==1)
return x;
else
return(x * power(x,y-1));
}

Example 26 : Program to compute GCD &LCM


# include <iostream.h>
# include<conio.h>
void swap(int &, int &);
long LCM (long, long) ;
long GCD(long, long);
void main( )
{
int m,n;
cout<<"\n Enter two positive integers : ";
cin >> m >> n ;
cout<<"GCD ( " << m <<", "<<n<<" ) = " <<GCD(m,n)<<endl;
cout<<"LCM ( " << m <<", "<<n<<" ) = " <<LCM(m,n)<<endl;
}
void swap(int & x, int & y)
{
//exchange the values of x and y
int temp = x;
x = y;
y = temp;
}
long GCD(long m, long n)

{
if(m<n)
swap(m,n);
while (n > 0)
{
int r = m % n;
m = n;
n=r;
}
return m;
}
long LCM(long m, long n)
{
return m * n/GCD(m, n);
}

Example 27 : Program to perform sorting and to display largest and second largest
elements in the list
# include <iostream.h>
# include<conio.h>
# define TRUE 1
#define FALSE 0
#define MAX 20
void bubble(int x[ ], int n)
{
int hold, j, pass;
int switched = TRUE;
for(pass=0; pass<n-1&&switched==TRUE; pass++)
{ // Outer loop controls the number of passes
switched = FALSE;
// Initially no interchange have been made on this pass
for(j=0; j<n-pass-1; j++)
// Inner loop governs each individual pass
if (x[j] > x[j+1])
{
//Interchange elements if they are out of order
switched = TRUE;
hold=x[j];
x[j] = x[j+1];
x[j+1] = hold;
}
//end if
}
// end for
}
// end bubble function

void main( )
{
int i, n;
int x[MAX];
cout<<"\n Enter numbers of elements :";
cin>>n;
cout<<"\n Enter Numbers in any order : \n";
for(i=0; i<n; i++)
{
cout<<"\n Element["<<i<<"] = ";
cin>>x[i];
}
bubble(x,n);
cout<<"\n \nSorted Elements are : \n";
for(i=0; i<n; i++)
cout<<"\n Element["<<i<<"] = "<<x[i];
// Displays the largest and 2nd largest element from sorted list
cout<<"\n\n Largest Element is : "<<x[n-1];
cout<<"\n Second largest Element is : "<<x[n-2];
getch( );
}

Example 28 :
# include <iostream.h>
#include<math.h>
int sq_root (int);
double sq_root (double);
long int sq_root (long int);
void main (void)
{
int a = 25;
long int b = 225;
double c=225;
cout << "Square root of int value=" << sq_root (a);
cout << "Square root of double value =" <<sq_root (c);
cout << "Square root of long int value =" <<sq_root (b);
}
int sq_root (int a)
{
return (sqrt (a));
}
double sq_root (double c)
{

double x;
x = sqrt(c);
return (x);
}
long int sq_root(long int x)
{
long int z;
z = sqrt (x);
return (z);
}

Example 29 :
#include<iostream.h>
#include<conio.h>
#include<math.h>
double power(double m, int n = 2);
void main()
{
int m,n;
cout << "\n Enter value of m & n";
cin >> m >> n;
cout << " power("<< m <<", "<< n <<") = "
<< power (m, n);
cout << "power(" << m <<", 2)" << power (m);
getch( );
}
double power (double m, int n)
{
return(pow(m, n));
}

Example 30 :
#include<iostream.h>
#include<conio.h>
#include<string.h>
float ADD (float, float);
int ADD (int, int);
char * ADD (char *, char *);
char S1 [50] , S2 [50], S3 [100];
void main ( )

int n = ADD (4, 6);


float d = ADD (4.7, 8.4)
;
cout << " Add (4, 6) = " << n ;
cout << " Add (4.7, 8.4) = " << d << " \n" ;
strcpy (S1, " Object oriented ");
strcpy (S2, " Database model ");
strcpy (S3, ADD (S1, S2));
cout << S1 << " + " << S2 << " = " << S3 ;
getch( ) ;

}
float ADD (float a, float b)
{
return (a + b);
}
int ADD (int a, int b)
{
return (a + b);
}
char * ADD (char* S1, char* S2)
{
strcat (S1, S2) ;
return S1;
}

Example 31 :
//main file funcall.cpp
#include<conio.h>
#include<iostream.h>
#include "log_x10.cpp"
#include"expo.cpp"
void main()
{
logx10();
expo();
}
//logx_10.cpp
# include<math.h>
# include<iostream.h>
void logx_10()
{

int n=0;
cout<<"Enter number;";
cin>>n;
cout<<"The log value is "<<log(n);
}
//expo.cpp
# include<math.h>
# include<iostream.h>
void expo()
{
int n=0;
cout<<"Enter number:";
cin>>n;
cout<<"\n Exponential value="<<exp(n);
}

Example 32 :
# include<iostream.h>
# include<conio.h>
void main()
{
int j=0;
clrscr();
void extend(double[], double[],double[]);
double qty[10]={3,9.7,6.40,4.5,5.6,6.2,7,2.8,15.0,18.0};
double price[10]={10.20,11.30,13.14,16.9,18.1,2.71,7.55,15.12,9.45,17.0};
double amount[10]={1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0};
double tamount[10];
extend(qty,price,amount);
for (j = 0; j <= 9; j++)
{
tamount[j] = amount[j];
}
for(j=0;j<=9;j++)
{
cout<<tamount[j]<<"\n";
}
getch();
}
void extend(double q[], double p[], double amount[])
{
int i=0;
for(i=0;i<=9;i++)

{
amount[i]=q[i]*p[i];
}
}

Example 33 :
# include<iostream.h>
# include<conio.h>
void main()
{
int no=0,pos=0;
void delchar(char[],int,int);
char string[10];
cout<<"Enter string";
cin>>string;
cout<<"Enter no. of characters to delete";
cin>>no;
cout<<"Enter starting position";
cin>>pos;
pos = pos - 1;
delchar(string,no,pos);
getch();
}
void delchar(char s[], int n, int p)
{
int i, c;
for(i=p,c=0;c<n;i++)
{
s[i]=' ' ; c++;
}
while (s[i]!='\0')
{
s[p] = s[i];
p++; i++;
}
s[p]='\0';
cout<<"\nNow string is :" << s;
}

Example 34 :
#include<iostream.h>
#include<conio.h>
void main ( )
{
char a [20], ch ;
int i , count ;
cout<< "\n Enter a String : " ;
cin>>a;
cout<<"\nEnter a character to check its frequency in given string :";
cin>>ch;
i=0;
while (a[i]!='\0')
{
if ( ch == a [ i ] )
count = count +1 ;
i=i+1;
}
cout<< "\nFrequency of given letter is "<<count ;
getch ( ) ;
}

Example 35 : Program to find s2 in s1 where s1 is the main string and s2 is substring


#include<iostream.h>
#include<string.h>
#include<conio.h>
#include<stdio.h>
void main ( )
{
char s [ 20 ], s1 [ 20 ], s2 [ 20 ], *p = " " ;
int i ;
clrscr();
cout<< "\n Input string :" ;
gets ( s );
cout<< "\n Enter second string " ;
gets ( s2 );
strcpy ( s1, s );
p = strtok ( s2, " " ) ;
for ( i = 0; p ; i ++ )

{
if ( strcmp ( p, s2 ) == 0 )
cout << "\n string found " ;
p = strtok ( NULL, " " ) ;
}
getch ( ) ;
}

Example 36 :
#include<iostream.h>
#include<conio.h>
#include<string.h>
void main ( )
{
char s [ 15 ] ;
int i , p ;
cout<< "\nEnter any string : ";
cin>>s;
p = strlen ( s ) ;
for ( i = 0; i < p ; i ++ )
cout<< "\nElement at position "<<i+1<<" is"<< s[i] ;
getch ( ) ;
}

Example 37 :
#include<iostream.h>
#include<conio.h>
void main()
{
int x[100],i=1,avg=0,max=0,min=0,sum=0;
cout<<"\nEnter the values in the array X";
for(i=0; i<100; i++)
cin>>x[i];
for(i=0;i<100;i++)
cout << "Element ["<< i + 1 <<" ] = " << x[i];
for(i=0;i<100;i++)
sum=sum+x[i];
cout<<"\n Sum of array elements is : "<<sum;

avg=sum/100;
cout<<"\n Average of array elements is : "<<avg;
for(i=0; i<100; i++)
{
if (x[i]>avg) max++;
else min++;
}
cout<<"\n Total "<<max<<" numbers are present in array X ";
cout<<"\nwhich are greater than average";
cout<<"\nTotal "<<min<<" numbers are present in array X ";
cout<<"\nwhich are less than average";
getch( );
}

Example 38 :
#include<iostream.h>
#include<math.h>
#define MAX 5
#include<conio.h>
void main( )
{
float mean=0,variance=0;
int i,x[MAX];
clrscr();
for(i=0;i<MAX;i++)
{
cout<<"\n Enter array elements: ";
cin>>x[i] ;
mean = mean+x[i];
}
mean = mean/MAX;
cout<<"\n Mean : "<<mean ;
for(i=0;i<MAX;i++)
variance = variance + (x[i]-mean)*(x[i]-mean);
variance = variance/MAX;
cout<<"\nVariance : "<<variance ;
cout<<"\n Standard Deviation : "<<sqrt(variance);
}

Example 39 :
#include <iostream.h>
#include <math.h>
#include<conio.h>
int ack (int m, int n);
void main ( )
{
int x, y, z;
cout<< "\n Enter values of x and y" ;
cin>>x>>y;
z = ack (x, y);
cout<<z;
getch ( );
}
int ack ( int m, int n )
{
int p;
if ( m == 0 )
return ( n + 1 );
else if ( n == 0 && m > 0 )
p = ack ( m - 1, 1 );
else if ( m >= 0 && n >= 0 )
p = ack ( m - 1, ack ( m, n - 1 ) );
return (p);
}
Example 40 : Program to compute combination Function
# include <iostream.h>
# include<conio.h>
long comb( int n, int r);
void main( )
{
for ( int i = -1; i < 6; i++)
{
for( int j = -1; j <= i+1; j++)
cout<<" "<<comb(i,j);
cout<<endl;
}
}
long fact( int n);
long comb( int n, int r)
{
if ( n < 0 || r < 0 || r > n)

return 0;
return fact(n) / ( fact (r) * fact (n -r));
}
long fact (int n)
{
if (n < 2) return 1;
long f = 1;
for (int i = 2; i <= n; i++)
f *= i;
return f;
}

Example 41 :
#include<iostream.h>
#include<conio.h>
#include<string.h>
struct sirname
{
char name[30];
}sn[10];
void main()
{
int i,j;
char temp[30];
clrscr();
for(i=0;i<5;i++)
{
cout<<"\nEnter "<<i+1<<" Sirname :";
cin>>sn[i].name;
}
for(i=0;i<5;i++)
{
for(j=i;j<5;j++)
{
if(strcmp(sn[i].name,sn[j].name)>0)
{
strcpy(temp,sn[i].name);
strcpy(sn[i].name,sn[j].name);
strcpy(sn[j].name,temp);
}
}

}
for(i=0;i<5;i++)
cout<<"\n"<<sn[i].name;
getch();
}

Example 42 :
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
#define MAX 5
int main()
{
int a[MAX][MAX],i,j;
cout<<"\nEnter the matrix row wise :";
for(i=0;i<MAX;i++)
{
for(j=0;j<MAX;j++)
{
cout<<"\n Enter a["<<i<<"]["<<j<<"]element : ";
cin>>a[i][j];
}
}
for(i=0;i<MAX;i++)
{
for(j=0;j<MAX;j++)
{
if(i!=j && a[i][j]!=0)
{
cout<<"\nMatrix is not diagonal matrix";
getch(); exit(0);
}
}
}
cout<<"\nMatrix is diagonal matrix";
getch();
return(0);
}

Example 43 : Program to illustrates External Storage Classes


#include<iostream.h>
int i ;
//External variable
void decre( );
void incre( ) ;
void main( )
{
cout << "\n i = "<<i ;
incre( );
incre( );
decre( );
decre( );
}
void incre( )
{
i = i + 1;
cout << "\n On incrementing i = " << i ;
}
void decre( )
{
i = i - 1;
cout << "\n On decrementing i = " << i ;
}

Example 44 : Program to illustrates Function Overloading


#include <iostream.h>
// Function prototypes
int add (int, int);
float add (float, float, float);
void main( )
{
int a=5, b=10;
float c=4.5, d=2.5, e=8.7;
cout << " First addition result = " << add (a,b);
cout << "\n Second addition result = " << add (c,d,e);
}

int add (int p, int q)


{
return (p+q);
}
float add (float p, float q, float r)
{
return (p+q+r);
}

Example 45 : Program to find area of rectangle and circle using Function


Overloading
#include <iostream.h>
float area (float, float);
// prototypes
float area (float);
void main( )
{
cout << "Area of a rectangle = " << area(1.5, 3.6);
cout << "\nArea of a circle = " << area(2.5) << "\n";
}
float area(float length, float width) // Definition - 1
{
return(length * width);
}
float area(float radius)
// Definition - 2
{
return(3.142 * radius * radius);
}

Example 46 : Program to find volume of cube, cylinder and rectangle


#include <iostream.h>
#include <conio.h>
//Declaration (Prototypes)
int volume (int);
double volume (double, int) ;
long volume (long, int, int) ;
int main( )

{
cout << volume (10) <<endl;
cout <<volume (2.5, 8) <<endl;
cout <<volume (100L, 75, 15) <<endl;
getch( );
return 0;
}
int volume (int x)
// volume of cube
{
return (x*x*x);
}
double volume(double r, int h)
// volume of cylinder
{
return (3.14519*r*r*h) ;
}
long volume(long l, int b, int h)
// volume of rectangular
{
return (l*b*h);
}

Example 47 : Program to illustrate Function prototype


#include <iostream.h>
void find_minimum ( int i, int j, int k ) ;
// Function prototype
void main ( )
{
int a, b, c ;
cout << "\n Input three numbers : " ;
cin >>a ;
cin >>b ;
cin >>c ;
find_minimum ( a, b, c ) ;
// Function call
}
void find_minimum ( int i, int j, int k )
// Function definition
{
int min ;
min = i ;
if ( min > j )
min = j ;
if ( min > k )
min = k ;
cout<< "\n The minimum number is :" << min ;
}

Example 48 : Program to illustrate sending & receiving values between Function


#include<iostream.h>
#include<conio.h>
int calculate_sum(int x, int y)
{
int z ;
z=x+y;
return (z) ;
}
void main( )
{
int a, b, sum ;
cout <<"\n Enter Two Number : ";
cin >> a;
cin >> b;
sum = calculate_sum(a,b);
cout<<"\n Sum is : "<<sum;
getch();
}

Example 49 :
#include<iostream.h>
void function( int b)
{
b = 100 ;
cout<<"\n value :" <<b;
}
void main( )
{
int a = 50;
function(a);
cout<<"\n=value : "<<a;
}

Example 50 : Program to illustrate void main( )


#include<iostream.h>
void fun ( )
{
cout<<"\n Welcome to the world of C++ ";
}
void main( )
{
cout<<"\n Welcome to the world of Programming..";
fun ( );
}
Example 51 : Program to illustrate Function
#include<iostream.h>
#include<conio.h>
void function1 ( )
{
cout<<"\n I am in function1 ";
}
void function2 ( )
{
cout<<"\n I am in function2 ";
}
void function3 ( )
{
cout<<"\n I am in function3 ";
}
void main( )
{
cout<<"\n I am in main ";
function1 ( ) ;
function2 ( ) ;
function3 ( ) ;
getch ( );
}

Example 52 : Program to illustrate Function

#include<iostream.h>
void fun ( )
{
cout<<"\n Welcome to the world of C++ ";
}
int main( )
{
cout<<"\n Welcome to the world of Programming..";
fun ( );
return 0;
}

Example 53 : Program to illustrate Global Variables


#include<iostream.h>
#include<conio.h>
int a, b, c ;
// Global Variable Declaration
void find_minimum ()
{
int min ;
min = a ;
if ( min > b )
min = b ;
if ( min > c )
min = c ;
cout << "\nThe minimum number is : "<<min ;
}
void main ()
{
cout<< "\n Input three numbers : " ;
cin>>a;
cin>>b;
cin>>c;
find_minimum () ;
getch();
}

Example 54 : Program to illustrates Inline Functions


#include <iostream.h>
#include <conio.h>

inline int square (int a)

// inline function definition

{
return (a*a);
}
void main(void)
{
cout << "Square(10) = " << square(10);
cout << "\n Square(10+10) = " << square(10+10);
getch( );
}

Example 55 : Program to illustrate isalnum( ) function


#include <iostream.h>
#include <ctype.h>
#include <conio.h>

//defines the cout, cin object


// defines the isalnum( ) function

void main()
{
char c,ans;
int a;
do
{
cout<<"\nEnter a character -";
cin >> c;
if(isalnum(c)>0)
cout<<"\nThe character is an alphanumeric character.";
else
cout<<"The character is not an alphanumeric character.";
cout<<"\nDo you want to continue ? (y or n) : \n " ;
cin>>ans;
}
while((ans=='y')||(ans=='Y'));
}

Example 56 : Program to illustrate isalpha( ) function


#include <iostream.h>
#include <ctype.h>

//defines the cout, cin object


// defines the isalpha( ) function

#include <conio.h>
void main ( )
{
char c , ans ;
int a ;
do
{
cout<<"\n Enter a character -" ;
cin >>c ;
if (isalpha (c) > 0)
cout<<"\n The character is an alphabet." ;
else
cout<<"\n The character is not an alphabet." ;
cout<<"\n Do you want to continue ? (y or n) : ";
cin >> ans;
}
while ( (ans == 'y') || (ans == 'Y') ) ;
getch();
}

Example 57 : Program to illustrate iscntrl ( ) function


#include <iostream.h>
#include <ctype.h>
#include <conio.h>

//defines the cout, cin object


// defines the iscntrl( ) function

void main ()
{
char c , ans ;
int a ;
do
{
cout<<"\n Enter a character -" ;
cin >>c ;
if (iscntrl (c) > 0)
cout<<"\n The character is a control character ." ;
else
cout<<"\n The character is not a control character." ;
cout<<"\n Do you want to continue ? (y or n) : ";
cin >> ans;
}
while ( (ans == 'y') || (ans == 'Y') ) ;

getch();
}

Example 58 : Program to illustrate isdigit( ) function


#include <iostream.h>
#include <ctype.h>
#include <conio.h>
void main ()
{
char c , ans ;
int a ;

// defines the isdigit( ) function

do
{
cout<<"\n Enter a character -" ;
cin >>c ;
if (isdigit (c) > 0)
cout<<"\n The character is digit ." ;
else
cout<<"\n The character is not a digit ." ;
cout<<"\n Do you want to continue ? (y or n) : ";
cin >> ans;
}
while ( (ans == 'y') || (ans == 'Y') ) ;
getch();
}

Example 59 : Program to illustrate islower( ) function


#include <iostream.h>
#include <conio.h>
#include <ctype.h>

// header file for islower( )

void main ( )
{
char c , ans ;
int a ;
do
{
cout<<"\n Enter a character -" ;

cin >>c ;
if (islower (c) > 0)
cout<<"\n The character is a lower case character ." ;
else
cout<<"\n The character is not a lower case character." ;
cout<<"\n Do you want to continue ? (y or n) : ";
cin >> ans;
}
while ( (ans == 'y') || (ans == 'Y') ) ;
getch();
}
Example 60 : Program to illustrate toupper( ) function
#include <iostream.h>
#include <conio.h>
#include <ctype.h>
// header file for toupper( )
void main ( )
{
int i , character ;
cout<<"Lower Case Series \t Upper Case Series \n" ;
for ( i = 481 ; i < 507 ; i++)
{
character = toascii (i) ;
cout<<"\t"<< char(character) <<"\t\t\t"
<< char (toupper(character));
cout<<"\n";
}
getch ( ) ;
}

Example 61 : Program to illustrate sqrt ( ) function


#include <iostream.h>
//defines the cout, cin object
#include <math.h>
// defines the sqrt( ) function
#include <conio.h>
void main ( )
{
// Tests the sqrt( ) function
for(int i =0; i < 6 ; i++)
cout<<"\t" << i << "\t" <<sqrt(i)<<endl;
getch();
}

Example 62 : Program to illustrate the abs( ) function


#include <iostream.h>
#include <math.h>
// defines the abs( ) function
#include <conio.h>
void main ( )
{
int i ;
i = -20 ;
cout<< "\n i = "<<i ;
cout<<"\n Absolute value of i = "<<abs( i ) ;
i = 20 ;
cout<< "\n i = "<<i ;
cout<<"\n Absolute value of i = "<<abs( i ) ;
getch ( ) ;
}
Example 63 : Program to illustrate the acos( ) function
#include <iostream.h>
#include <math.h>
// defines the acos( ) function
#include <conio.h>
void main ()
{
double result ;
double i = 0.5 ;
result = acos ( i ) ;
cout<<"\n The arc cosine of "<< i << " is "<< result;
getch () ;
}

Example 64 : Program to illustrate the atan( ) function


#include <iostream.h>
#include <math.h>
#include <conio.h>
void main ( )
{
double result ;

// defines the atan( ) function

double i = 0.5 ;
result = atan ( i ) ;
cout<<"\n The arc tangent of "<< i << " is "<< result;
getch () ;
}

Example 65 : Program to illustrate the ceil( ) function


#include <iostream.h>
#include <math.h>
#include <conio.h>

// defines the ceil( ) function

void main ( )
{
double number = 29.92 ;
double result ;
result = ceil ( number ) ;
cout<< "\n Original number = "<< number ;
cout<< "\n Number rounded up = "<< result ;
getch () ;
}

Example 66 : Program to illustrate the fabs( ) function


#include <iostream.h>
#include <math.h>
#include <conio.h>

// defines the fabs( ) function

void main ( )
{
float i = -20.56;
cout.precision(2);
cout<<" i = "<<i;
cout<<"\n Absolute value of i = "<<fabs(i);
i = 20.56 ;
cout<<" i = "<<i;
cout<<"\n Absolute value of i = "<<fabs(i);
getch () ;

Example 67 : Program to illustrate the exp( ) function


#include <iostream.h>
#include <math.h>
#include <conio.h>

// defines the exp( ) function

void main ( )
{
double result;
double x = 2.0 ;
result = exp ( x ) ;
cout<< "\n The result is : "<< result ;
getch ( ) ;
}

Example 68 : Program to illustrate the pow ( ) function


#include <iostream.h>
#include <math.h>
// defines the pow ( ) function
#include <conio.h>
void main ( )
{
double result;
double x = 10, y = 2 ;
result = pow ( x, y ) ;
cout<<"\n"<<x<<" to the power "<<y<<" = "<<result ;
getch ( ) ;
}

Example 69 : Program to illustrate the log ( ) function


#include <iostream.h>
#include <math.h>
// defines the log ( ) function
#include <conio.h>
void main ( )
{
double result;
double x = 0.5 ;

result = log ( x ) ;
cout<<" \n The natural log of "<< x <<" is "<<result ;
getch () ;
}
Example 70 : Program to illustrate the log10 ( ) function
#include <iostream.h>
#include <math.h>
// defines the log10 ( ) function
#include <conio.h>
void main ( )
{
double result;
double x = 10 ;
result = log10( x ) ;
cout<<" \n The natural log of "<< x <<" is "<<result ;
getch () ;
}

Example 71 : Program to illustrate the abort( )function */


#include <iostream.h>
#include <process.h>
// defines the abort( )function
#include <conio.h>
void main ( )
{
cout<< "\n Calling abort( )\n" ;
abort( ) ;
getch ( ) ;
/* This is never reached */
}

Example 72 : Program to illustrate the exit( ) function


#include <iostream.h>
#include <process.h>
// defines the exit( ) function
#include <conio.h>
void main ( )
{
cout<< "\n Calling exit( ) \n" ;
exit(0);

getch ( ) ;

/* This is never reached */

}
Example 73 : Program to illustrate the atoi( ) function
#include<iostream.h>
#include<conio.h>
#include<stdlib.h >
// defines the atoi( ) function
void main()
{
int i ;
char *string = "145.97" ;
i = atoi ( string ) ;
cout<< "string = "<<string<<"\t integer = "<<i;
getch ( ) ;
}

Example 74 : Program to illustrate the atol( ) function


#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
// defines the atol( ) function
void main()
{
long i ;
char *string = "123459" ;
i = atol ( string ) ;
cout<< "string = "<<string<<"\t integer = "<<i;
getch () ;
}
Example 75 : Program to illustrate the itoa( ) function
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
void main ( )
{
int i = 102 ;

// defines the itoa( ) function

char *string ;
itoa ( i, string, 10 ) ;
cout<<"integer = "<<i<<"\t string = "<<string;
getch ( ) ;
}
Example 76 : Program to illustrate the labs( ) function
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
// defines the labs( ) function
void main ( )
{
long i ;
i = -12345678L ;
cout<<"\n i = "<<i ;
cout<<"\n Absolute value of i = "<<labs(i);
i = 12345678L ;
cout<<"\n\n i = "<<i ;
cout<<"\n Absolute value of i = "<<labs(i);
getch() ;
}

Example 77 : Program to illustrate the ltoa( ) function


#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
// defines the ltoa( ) function
void main ( )
{
long i = 50223 ;
char *string ;
ltoa ( i, string, 10 ) ;
cout << "\n integer = " << i << "\t string = " << string;
getch () ;
}

Example 78 : Program to illustrate the max( ) function


#include<iostream.h>
#include<conio.h>

#include<stdlib.h>
// defines the max( ) function
void main()
{
int x = 5;
int y = 6;
int z;
z=max(x,y);
cout<<"\nThe maximum number is "<<z ;
getch() ;
}
Example 79 : Program to illustrate the min( ) function
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
// defines the min( ) function
void main ( )
{
int x = 1, y = 7, z ;
z=min(x,y) ;
cout<<"\nThe minimum number is "<<z ;
getch();
}

Example 80 : Program to illustrate the rand( ) function


#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
// defines the rand( ) function
void main ( )
{
int z ;
z = rand ( ) ;
cout<< "\n The 1st random number is "<<z ;
z = rand () ;
cout<< "\n The 2nd random number is "<<z ;
getch ( ) ;
}

Example 81 : Program to illustrate Local Variables


#include<iostream.h>
void show(int j)
{
int k = 45 ;
cout<<"\n value of j : "<<j;
cout<<"\n value of k : "<<k;
}
void main( )
{
int i =200;
show( i);
}

// Local Variable Declaration

// Local Variable Declaration

Example 82 : Program to illustrate Pointer


#include <iostream.h>
void main( )
{
int i = 5 ;
cout <<" \n Address of i = "<< &i ;
cout <<" \n Value of i = "<< i ;
}
Example 83 : Program to illustrate Pointer
#include <iostream.h>
#include<conio.h>
void main( )
{
int i = 5 ;
cout <<" \n Address of i = " << &i ;
cout <<" \n Value of i = " << i ;
cout <<" \n Value of i = " << *(&i) ;
getch();
}

Example 84 : Program to illustrate Pointer variable


#include <iostream.h>

void main( )
{
int i = 5 ;
int * j ;
// Declaration of Pointer Variable
j = &i ;
// Assigning address of variable i to variable j
cout <<" \n Address of i = " << &i ;
cout <<" \n Address of i = " << j ;
cout <<" \n Value of i = " << i ;
cout <<" \n Value of i = " << *(&i) ;
cout <<" \n Value of i = " << *j ;
}

Example 85 : Program for Macro with Arguments


#include <iostream.h>
#include <conio.h>
/* macro definition */
#define xyz(a)
b=a*2;
void main()
{
int x = 3, b ;
clrscr () ;
xyz ( x ) ;
/* call to macro 'xyz' */
cout << "After first call to the macro 'xyz'...\n" ;
cout << "b =" << b ;
xyz ( x + 5 ) ;
/* call to macro 'xyz' */
cout <<"\n\nAfter second call to the macro 'xyz'...\n";
cout << "b = " << b ;
getch () ;
}

Example 86 : Program for Use of '#ifdef'


#include <iostream.h>
#include <conio.h>
#define print 1
/* macro 'print' definition */
void main()
{
int a = 10 ;
clrscr () ;
a=a+1;
#ifdef print
/* if macro 'print' defined */
cout << "a = " << a ;

#endif
getch () ;
}

Example 87 : Program for use of '#ifdef' with '#else'


#include <iostream.h>
#include <conio.h>
/* macro named 'print' is not defined */
void main()
{
int a = 10 ;
clrscr () ;
a=a+1;
#ifdef print
/* if macro 'print' defined */
cout << "a = " << a ;
#else
/* if macro 'print' is not defined */
cout << "The macro 'print' is not defined" ;
#endif
getch () ;
}
Example 88 : Program for Use of '#if'
#include <iostream.h>
#include <conio.h>
#define pi 3.142
void main()
{
clrscr () ;
#if pi == 3.142
cout << "The value of pi = " << pi ;
#else
cout << "The value of pi is not defined as 3.142" ;
#endif
getch () ;
}

Example 89 : Program for Use of '#if' with '#elif'


#include <iostream.h>
#include <conio.h>

#define m 2
void main()
{
clrscr () ;
#if m == 1
cout << " 'm' is One" ;
#elif m == 2
cout << " 'm' is Two" ;
#elif m == 3 || m == 4 || m == 5
cout << " 'm' is Between Three and Five" ;
#elif m >= 6 && m <= 10
cout << " 'm' is between Six and Ten" ;
#else
cout << " 'm' is not in the range [1-10]" ;
#endif
getch () ;
}
Example 90 : Program for Use of '#if' as '#ifdef'
#include <iostream.h>
#include <conio.h>
#define print 1
/* macro 'print' definition */
void main()
{
int a = 10 ;
clrscr () ;
a=a+1;
#if defined (print) /* if macro 'print' defined */
cout << "a = " << a ;
#endif
getch () ;
}

Example 91 : Program for Use of '#undef'


#include <iostream.h>
#include <conio.h>
#define pi 3.14
void main()
{
clrscr () ;
#if pi == 3.142
cout << "The value of pi = " << pi ;
#else
#undef pi
/* remove the definition of 'pi' */
#define pi 3.142
/* redefine 'pi' */
cout << "The modified value of pi = " << pi ;
#endif
getch ();
}

Example 92 : Program for Use of '#pragma startup' and '#pragma exit'


#include <iostream.h>
#include <conio.h>
/* functions declared before the 'pragma' lines */
void start ( void ) ;
void end ( void ) ;
/* 'pragma' directives */
#pragma startup start
#pragma exit end
void main()
{
cout << "\n\nProgram Body\n\n\n" ;
getch () ;
}
void start (void) /* function to be called before 'main()' */
{
clrscr () ;
cout << "PROGRAM STARTS HERE...\n" ;
getch () ;
}
void end (void) /* function to be called before program termination */
{

cout << "PROGRAM ENDS HERE...\n" ;


getch () ;
}

Example 93 : Program for Use of '#define' for constant definition


#include <iostream.h>
#include <conio.h>
#define size 5
/* constant definition */
void main()
{
int a[size] ;
/* use of synonym 'size' */
int i ;
clrscr () ;
for ( i = 0; i < size; i++ )
/* use of synonym 'size' */
a[i] = i ;
cout<< "The array elements are :\n" ;
for ( i = 0; i < size; i++ ) /* use of synonym 'size' */
cout <<a[i];
getch () ;
}

Example 94 : Program for Use of '#define' for macro definition


#include <iostream.h>
#include <conio.h>
/* macro definition */
/* macro definition if requires more than one program lines as
below, it can be continued on the next line with the help of
a special character used by '#define' which is '\' written
at the end of each program line of the macro. */
#define xyz \
cout<<"\nThe array elements are :\n"; \
for ( i = 0; i < 5; i++ ) \
cout << a[i];
void main()
{
int a[5], i ;
clrscr () ;
for ( i = 0; i < 5; i++ )

a[i] = i ;
xyz ;
/* use of call to a macro */
for ( i = 0; i < 5; i++ )
a[i] -= 1 ;
xyz ;
/* use of call to a macro */
getch () ;
}

Example 95 : Program for Use of '#ifndef'


#include <iostream.h>
#include <conio.h>
/* macro named 'print' is not defined */
void main()
{
int a = 10 ;
clrscr () ;
a=a+1;
#ifndef print
/* if macro 'print' is not defined */
cout << "The macro 'print' is not defined" ;
#else
/* if macro 'print' is defined */
cout << "a = " << a ;
#endif
getch () ;
}

Example 96 : Program that returns the average of 4 numbers


# include <iostream.h>
double avg ( double, double, double, double) ;
void main( )
{
int w, x, y, z;
cout<<"\n Enter four integers : ";
cin>>w>>x>>y>>z;
cout<<"\n Their average is "<<avg(w,x,y,z)<<endl;
}
double avg ( double n1, double n2, double n3, double n4)
{
return ( n1 + n2 + n3 + n4) /4.0 ;
}

Example 97 : Program that returns the minimum of 4 integers


# include <iostream.h>
int min (int, int, int, int);
void main( )
{
int w, x, y, z;
cout<<"\n Enter four integers : ";
cin>>w>>x>>y>>z;
cout<<"\n Their minimum is "<<min(w,x,y,z)<<endl;
}
int min ( int n1, int n2, int n3, int n4)
{
int min = n1;
if (n2 < min) min = n2 ;
if (n3 < min) min = n3 ;
if (n4 < min) min = n4 ;
return min;
}

Example 98 : Program to find factorial of -1 to 6 numbers


#include <iostream.h>
#include <conio.h>
long factorial (int i);
//Declaration (Prototype)
void main( )
{
for( int i = -1 ; i < 7; i++)
cout<<"fact ( " << i << " ) = "<<fact(i)<<endl;
getch( );
}
long factorial ( int n)
{
if (n < 2) return 1;
long f = 1;
for ( int i = 2; i < = n ; i++)
f*=i;
return (f );
}

Example 99 : Program for Permutation Function


# include <iostream.h>
long perm (int n, int k);
void main( )
{
for (int i = -1; i < 6; i++)
{
for (int j = -1 ; j <= i+1; j++)
cout<<" " <<perm (i, j) ;
cout<<endl;
}
}
long perm( int n, int k)
{
if (n < 0 || k < 0 || k > n) return 0;
int p = 1;
for (int i = 1; i <=k; i++ , n--)
p *= n ;
return p ;
}

Example 100 : Program for combination Function


# include <iostream.h>
long comb (int , int );
// Function Declaration
void main( )
{
for (int i = -1; i < 9; i++)
{
for (int j = -1 ; j <= i+1; j++)
cout<<" " <<comb (i, j) ;
cout<<endl;
}
}
long perm( int , int );
// Function Declaration
long fact (int);
long comb(int n, int k)
{

if (n < 0 || k < 0 || k > n) return 0;


return perm(n,k)/fact(k);
}
long perm( int n, int k)
{
if (n < 0 || k < 0 || k > n) return 0;
int p = 1;
for (int i = 1; i <=k; i++ , n--)
p *= n ;
return p ;
}
long fact ( int n)
{
if (n < 2) return 1;
long f = 1;
for ( int i = 2; i <= n ; i++)
f *= i ;
return (f );
}

Example 101 : Program to print Pascal's Triangle


#include <iostream.h>
#include<iomanip.h>
long comb (int , int );
// Function Declaration
void main( )
{
const int m = 9;
int j;
for (int i = 0; i < m; i++)
{
for (j = 1 ; j < m - i; j++)
cout<<setw(2)<<" ";
//print whitespace
for (j = 0 ; j <= i; j++)
cout << setw(4) << comb( i, j);
cout<<endl;
}
}
long comb(int n, int k)
{
if (n < 0 || k < 0 || k > n) return 0;
long c = 1;

for ( int i =1; i <= k; i++, n--)


c = c * n/i ;
return c;
}

Example 102 : Program that extracts a digit from an integer


# include <iostream.h>
# include<conio.h>
int digit (long, int);
void main( )
{
int n , k;
cout<<"\n Integer : " ;
cin>>n;
do
{
cout << "\n Digit :";
cin >> k;
cout<<"\nDigit number " << k <<" of "<< n
<<" is "<< digit(n, k) << endl;
}while (k > 0);
}
int digit (long n, int k)
{
for( int i = 0; i < k; i++)
n /= 10;
//remove right-most digit
return (n % 10);
}

Example 103 : Program that determines whether the given integer is a square
number
# include <iostream.h>
# include<conio.h>
int isSquare (int);
int main( )
{
const int MAX = 20;

for ( int i =0; i < MAX; i++)


if (isSquare(i) )
cout<<i<<" is square. \n";
else
cout<<i<<"is not square. \n";
getch( );
return 0;
}
int isSquare( int n)
{
int i =0;
while ( i*i < n)
++i;
if ( i* i == n) return 1;
else return 0 ;
}

Example 104 : Program to compute Area & circumference of circle


# include <iostream.h>
# include<conio.h>
void computecircle(float*,float*, float );
void main( )
{
float a,c,r;
cout<<"\n Enter the radius : ";
cin>>r;
computecircle(&a,&c,r);
cout<<"n The area of a circle of radius "<<r<<" is "<<a
<<"\n and its circumference is " <<c<<endl;
}
void computecircle(float *area, float *circ, float r )
{
const double PI=3.141592653589793;
*area = PI*r*r;
*circ = 2*PI*r;
}

Example 105 : Program to compute Sum & average of elements in array


# include <iostream.h>

# include<conio.h>
int sum (int[ ], int);
float avg (int[ ], int);
void main( )
{
int a[ ]={11,33,55,77};
int size = sizeof(a) /sizeof(int);
cout<<"Sum (a, size) = "<<sum(a,size)<<endl;
cout<<"Average (a, size) = "<<avg(a,size)<<endl;
}
int sum(int a[ ], int n)
{
int sum1 = 0;
for(int i = 0; i < n; i++)
sum1= sum1+a[i] ;
return sum1;
}
float avg (int a[ ], int n)
{
float sum = 0.0;
for(int i = 0; i < n; i++)
sum= sum +a[i] ;
return sum/n;
}
Example 106 : Program to search a given integer in an array of integers
# include <iostream.h>
# include<conio.h>
int search(int[], int);
void main( )
{
int arr[10], num, i, subscript;
cout<<"\n Input 5 array elements : ";
for(i=0;i<5;i++)
cin>>arr[i];
cout<<"\n Enter number to be searched :";
cin>>num;
if((subscript = search(arr,num)) == -1)
cout<<"\n No such element !! ";
else
cout<<"\n Element found at : "<<subscript;
getch( );

}
int search(int arr[], int num)
{
int i ;
for(i=0; i<5; i++)
{
if (arr[i] == num)
return i +1;
}
return -1;
}

Example 107 : Program to implement Binary Search


# include <iostream.h>
# include<conio.h>
void main( )
{
int list[20], number, result = 0;
int i, n, low ,mid, high;
cout<<"\n Enter the number of elements : ";
cin>>n;
cout<<"\n Enter elements in ascending order : \n";
for(i=0; i<n; i++)
cin>>list[i];
cout<<"\n Enter the number to be searched : ";
cin>>number;
low = 0;
high = n-1;
while(low <= high)
{
mid = (low+ high) / 2;
if (number < list [mid])
high = mid - 1;
else if(number > list[mid])
low = mid + 1;
else
{
result = list[mid];
break;
}
}

if (result == 0)
cout<<"\n No match found";
else if(result != 0)
{
cout<<"\n Match found \n";
cout<< "Position of number "<< number<<" is : "<<mid +1;
}
}

Example 108 : Program to Check for symmetric matrix


# include <iostream.h>
# include<conio.h>
void main( )
{
int i, j, matrix[3][3], flag = 0;
cout<<"\n Input 3 3 matrix elements :\n";
for (i=0;i<3;i++)
for(j=0;j<3;j++)
cin>>matrix[i][j];
for (i=0;i<3;i++)
for(j=0;j<3;j++)
if (matrix[i][j] != matrix[j][i])
flag = 1;
if (flag == 1)
cout<<"\n Matrix is not symmetric";
else
cout<<"\n Matrix is symmetric";
}

Example 109 : Program to Sort the matrix in ascending order


# include <iostream.h>
# include<conio.h>
void main( )
{
int matrix[3][3], sort[9], i, j, temp, k=0;
cout<<"\n Input 3 3 matrix elements : \n";

for (i=0;i<3;i++)
for (j=0;j<3;j++)
cin>>matrix[i][j];
for (i=0; i<3; i++)
// copy into an array for sorting
for (j=0; j<3; j++)
sort[k++] = matrix[i][j];
for (i=0; i<9; i++)
//sort the copied array
for (j=i+1; j<9; j++)
{
if (sort[i] > sort[j])
{
temp = sort[i];
sort[i] = sort[j];
sort[j] = temp;
}
}
cout<<"\n Sorted matrix is : \n ";
for ( k=0, i=0, j=0; k<9; k++)
//copy back to the matrix
{
if (j == 3)
{
j = 0;
i ++;
cout<<"\n";
}
matrix[i][j] = sort[k];
cout << matrix[i][j];
//print sorted matrix.
j++;
}
getch( );
}

Example 110 : Program to count number of times the word 'the' appears in the
string
# include <iostream.h>
# include<conio.h>
#include<string.h>
#include<stdio.h>
void main( )
{
char s[20], s1[20], *p = " ";

int i , count = 0;
cout<<"\n Input the String : \n";
gets(s);
strcpy(s1,s);
p = strtok(s1," ") ;
// get the first word
for( i = 0 ;p;i++)
{
if (strcmp (p,"the") == 0 )
count++;
p = strtok(NULL," " );
//get the next word
}
cout<<"\n Count is : "<<count ;
}

Example 111 : Program to check for a palindrome number


# include <iostream.h>
# include<conio.h>
#include<string.h>
#include<stdlib.h>
void main( )
{
int num, n, flag = 0, i=0;
char s[10];
cout<<"\n Input an integer : ";
cin>>num;
itoa (num,s,10);
//convert int to string
n = strlen(s) -1;
while(s[i] != '\0')
{
if (s[i] != s[n-i])
{
flag = 1;
break;
}
i++;
}
if (flag == 1)
cout<<"\n Number is not Palindrome";
else

cout<<"\n Number is Palindrome";


getch( );
}

Example 112 : Program to check whether entered string is a palindrome or not


# include <iostream.h>
# include<conio.h>
#include<string.h>
int is_palindrome(char* str)
{
int n, flag, i =0;
n= strlen (str);
while(*(str+i)!='\0')
{
if(*(str+i)!=*(str+(n-i-1)))
{
flag = 1;
break;
}
i++;
}
if(flag == 1)
return 0;
else
return 1;
}
void main( )
{
char s[20];
cout<<"\n Enter a String : ";
cin>>s;
if (is_palindrome(s))
cout<<"\n Input string is a palindrome String ";
else
cout<<"\n Input string is not a palindrome String ";
getch( );
}

Example 113 : Program to accept a 3 digit number and print it out digit by digit as a
series of words.
# include <iostream.h>
# include<conio.h>
void main( )
{
int n;
char s[10][6]={ "Zero", "One", "Two", "Three", "Four", "Five", "Six", "Seven",
"Eight", "Nine"};
cout<<"\n Enter 3 digit number : ";
cin >> n;
cout<< "\n The Number is : " <<n ;
cout<<"\n"<<s[n/100];
n = n % 100;
cout<<" "<<s[n/10];
n = n % 10;
cout<<" "<<s[n/1];
getch( );
}

Example 114 : Program which concatenates a string to the left of a given string
# include <iostream.h>
# include<conio.h>
#include<string.h>
void main( )
{
char *s1="Good", *s2= "Day", *s3;
s3 = s2 + (strlen(s2)-1); // s3 points to last character of s2
while(*s2 != '\0')
{
*--s1=*s3--;
s2++;
}
cout<<s1;
//print concatenated string
getch( );
}

Example 115 : Program to find factorial of number using non-recursive function


#include <iostream.h>
#include <conio.h>
int factorial (int i);
//Declaration (Prototype)
void main( )
{
int a, fact;
cout<<"\n Enter any Number : " ;
cin>>a;
fact = factorial( a ) ;
cout<<"\n Factorial value : " << fact ;
getch( );
}
int factorial ( int x)
{
int f =1, i ;
for (i = x; i >= 1; i--)
f=f*i;
return (f );
}

Example 116 : Program to find factorial of number using Recursive Function


#include <iostream.h>
#include <conio.h>
int recur (int i);

//Declaration (Prototype)

void main( )
{
int a, fact;
cout<<"\n Enter any Number : " ;
cin>>a;
if ( a < 0)
cout<<"\n Number should be positive !! ";
else
{
fact = recur ( a ) ;
cout<<"\n Factorial value : " << fact ;

}
getch( );
}
int recur ( int n)
{
if ( n == 1 )
return (1);
else
return (n * recur ( n - 1 ) );
}

Example 117 : Program to illustrates Recursion


#include <iostream.h>
int addition ( int n ) ;
void main ()
{
int n ;
cout<< "\n Input n : ";
cin >> n;
if ( n <= 0 )
cout<< "\n Number should be greater than zero !!!";
else
cout<< "\n Sum of 1 to "<<n<< " = "<< addition ( n ) ;
}
int addition ( int n )
{
if ( n == 1 )
return ( 1 ) ;
else
return ( n + addition ( n - 1 ) ) ;
}

Example 118 : Program to illustrates passing by reference


#include<iostream.h>
#include<conio.h>
void swap (int&, int&) ;
void main( )
{
int a = 11, b = 22;
cout <<"\n Before swap a = "<<a<<" and b = "<<b;

swap(a, b);
cout <<"\n After swap a = "<<a<<"and b = "<<b;
}
void swap(int& x, int& y)
{
//exchange the values of x and y
int temp = x;
x = y;
y = temp;
}
Example 119 : Program to illustrates Static Storage Classes
#include<iostream.h>
void incre( ) ;
void main( )
{
incre( );
incre( );
incre( );
incre( );
}
void incre( )
{
static int i = 1;
cout << "\n" <<i ;
i = i + 1;
}

Example 120 : Program to illustrates strlen( ) function


#include <iostream.h>
#include <conio.h>
#include <string.h>
void main( )
{
char s[ ] = "Hello";
cout<<" strlen ( "<<s<<" ) = "<<strlen(s) <<endl;
cout<<" strlen ( \"\" ) = "<<strlen("") <<endl;
char buff[20];
cout<<"Enter String : " ;
cin>>buff;
cout<<" strlen ( "<<buff <<" ) = "<<strlen(buff) <<endl;
getch( );

Example 121 : Program to illustrates strcpy ( ) function


#include <iostream.h>
#include <conio.h>
#include <string.h>
void main( )
{
char s1[20 ] = "Hello";
char s2[ ] = "Hi";
cout<<"Before strcpy (s1,s2) : \n";
cout<<" \t s1 = [ " <<s1 << "] , length = " <<strlen(s1)<<endl;
cout<<" \t s2 = [ " <<s2 << "] , length = " <<strlen(s2)<<endl;
strcpy(s1,s2) ;
cout<<"After strcpy (s1,s2) : \n";
cout<<" \t s1 = [ " <<s1 << "] , length = " <<strlen(s1)<<endl;
cout<<" \t s2 = [ " <<s2 << "] , length = " <<strlen(s2)<<endl;
getch( );
}
Example 122 : Program to illustrates strcat ( ) function
#include <iostream.h>
#include <conio.h>
#include <string.h>
void main( )
{
char s1[20 ] = "Hello";
char s2[ ] = "Sir";
cout<<"Before strcat (s1,s2) : \n";
cout<<" \t s1 = [ " <<s1 << "] , length = " <<strlen(s1)<<endl;
cout<<" \t s2 = [ " <<s2 << "] , length = " <<strlen(s2)<<endl;
strcat(s1,s2) ;
cout<<"After strcat (s1,s2) : \n";
cout<<" \t s1 = [ " <<s1 << "] , length = " <<strlen(s1)<<endl;
cout<<" \t s2 = [ " <<s2 << "] , length = " <<strlen(s2)<<endl;
getch( );
}

Example 123 : Program to illustrates strcat ( ) function


#include <iostream.h>
#include <conio.h>
#include <string.h>
void main( )
{
char s1[20 ] = "Hello";
char s2[ ] = "Sir";
cout<<"Before strcat (s1,s2) : \n";
cout<<" \t s1 = [ " <<s1 << "] , length = " <<strlen(s1)<<endl;
cout<<" \t s2 = [ " <<s2 << "] , length = " <<strlen(s2)<<endl;
strcat(s1,s2) ;
cout<<"After strcat (s1,s2) : \n";
cout<<" \t s1 = [ " <<s1 << "] , length = " <<strlen(s1)<<endl;
cout<<" \t s2 = [ " <<s2 << "] , length = " <<strlen(s2)<<endl;
getch( );
}
Example 124 : Program to illustrates strcmp( ) function
#include <iostream.h>
#include <conio.h>
#include <string.h>
void main( )
{
char s1[] = "Merry";
char s2[] = "Herry";
int i, j, k;
i = strcmp(s1,"Merry");
j = strcmp(s2, "Herry");
k = strcmp(s1,s2);
cout<<"\n"<<i<<"\t"<<j<<"\t"<<k;
getch( );
}

Example 125 : Program to illustrates strtok( ) functions


#include <iostream.h>
#include <conio.h>
#include <string.h>

void main ( )
{
char source [] = "Hello, Have a Nice Day" ;
char *p;
cout<< "\n Source String is : "<<source ;
/* strtok( ) places a NULL terminator
in front of the token, if found */
p = strtok ( source, "," ) ;
if ( p )
cout<< "\n First Token : "<<p;
/* A second call to strtok( ) using a NULL
as the first parameter returns a pointer
to the character following the token */
p = strtok ( NULL, " " ) ;
if ( p )
cout<< "\n Second Token : "<<p;
/* Similarly other tokens can be obtained */
p = strtok ( NULL, " " ) ;
if ( p )
cout<< "\n Third Token : "<<p;
getch ( ) ;
}

Example 126 : Program to illustrates strncat( ), strnset( ) and strncmp( ) functions


#include <iostream.h>
#include <conio.h>
#include <string.h>
void main()
{
char s1[] = "Apple" , s2[] = "mango" , c = 'v';
int n = 3 , m ;
puts ("String 1 - ") ;
puts (s1) ;
strncat (s1,s2,n) ;
puts ("Result of strncat( ) - ") ;
puts (s1) ;

puts ("Set String 1 - ") ;


strnset (s1,c,n) ;
puts (s1) ;
puts ("String 2 - ") ;
puts (s2) ;
n=4;
m = strncmp (s2,s1,n) ;
puts ("Result of strncmp( ) - ") ;
cout<<m ;
getch ( ) ;
}

Example 127 : Program to illustrates External Storage Classes


#include<iostream.h>
int i ;
//External variable
void decre( );
void incre( ) ;
void main( )
{
cout << "\n i = "<<i ;
incre( );
incre( );
decre( );
decre( );
}
void incre( )
{
i = i + 1;
cout << "\n On incrementing i = " << i ;
}
void decre( )
{
i = i - 1;
cout << "\n On decrementing i = " << i ;
}

Example 128 : Program to illustrates String


#include <iostream.h>
#include <conio.h>
void main( )
{
char str[ ] = "Hello";
int i = 0;
while (i<=4)
{
cout << str[i];
i++;
}
getch( );
}

Example 129 : Program to illustrates String


#include <iostream.h>
#include <conio.h>
void main( )
{
char str[ ] = "Hello";
int i = 0;
while (str[i]!='\0')
{
cout << str[i];
i++;
}
getch( );
}

Example 130 : Program to illustrates String


#include <iostream.h>
#include <conio.h>
void main( )
{
char str[ ] = "Hello";
char *ptr ;
ptr = str;
//store base address of string
while (*ptr !='\0')
{
cout << *ptr;
ptr++;
}
getch( );
}

Example 131 : Program to illustrates String


#include <iostream.h>
#include <conio.h>
void main( )
{
char str[] = "Hello";
cout<<str;
getch( );
}

Example 132 : Program to illustrates String


#include <iostream.h>
#include <conio.h>
void main( )
{
char str[20] ;
cout<<"\n Enter any string :";
cin>>str;
cout<<"\n The string is :"<<str;
getch( );
}

Example 133 : Program to illustrates gets( ) & puts( ) function


#include <iostream.h>
#include <conio.h>
#include<stdio.h>
void main( )
{
char str[20] ;
cout << "\n Enter any string :" ;
gets(str);
puts( " The string is : " ) ;
puts(str) ;
getch( );
}
/*
Output:
Enter any string : Hello
The string is :
Hello
*/

Example 134 : Program to calculate Length of String


#include <iostream.h>
#include <conio.h>
#include<stdio.h>
char line[40] ;
int calculate_length ( ) ;
void main ()
{
int length ;
cout <<" Enter a string : " ;
gets(line) ;
length = calculate_length ( ) ;
cout<<" \nThe string length is "<<length ;
getch ( ) ;
}
int calculate_length ( )

{
int count = 0 ;
while (line[count] != '\0')
count++ ;
return (count) ;
}
Example 135 : Program to Copy a String
#include <iostream.h>
#include <conio.h>
#include<stdio.h>
void main ( )
{
int i ;
char sentence[40] , copy[40] ;
cout<<"Enter any string :" ;
gets (sentence) ;
i=0;
for (i=0 ; i<40 ; i++)
copy[i] = 0 ;
i=0;
while (sentence[i] != '\0')
{
copy[i] = sentence[i] ;
i++ ;
}
cout<<"\nThe copied string is : " ;
puts (copy) ;
getch ( ) ;
}
Example 136 : Program to perform Concatenation of Strings
#include <iostream.h>
#include <conio.h>
#include<stdio.h>
void concatenate (char str1[40],char str2[40]) ;
void main ()
{
char str1[60] , str2[40] ;
puts ("Enter the first string :") ;
gets (str1) ;
puts ("\nEnter the second string :") ;

gets (str2) ;
concatenate (str1,str2) ;
puts ("\nThe concatenated string is :") ;
puts (str1) ;
getch ( ) ;
}
void concatenate (char str1[40], char str2[40])
{
int i, j;
i=0;
while (str1[i] != '\0')
i++ ;
str1[i] = ' ' ;
i++ ;
j=0;
while (str2[j] != '\0')
{
str1[i] = str2[j] ;
i++ ;
j++ ;
}
}

Example 137 : Program to perform Comparisons of Two Strings


#include <iostream.h>
#include <conio.h>
#include<stdio.h>
int compare (int i,char str1[],char str2[]);
void main ( )
{
int i , j , count = 0 ;
char str1[40],str2[40] ;
for (i=0 ; i<40 ; i++)
{
str1[i] = '\0' ;
str2[i] = '\0' ;
}
puts ("Enter the first string :") ;
gets (str1);
puts ("\nEnter the second string :") ;
gets (str2);
count = compare (i,str1,str2);

if ( (str1[count] == '\0')&&(str2[count]=='\0'))
puts ("\nThe two strings are equal.") ;
else
puts ("\nThe two strings are unequal.") ;
getch ( ) ;
}
int compare (int i,char str1[],char str2[])
{
i=0;
while ((str1[i]==str2[i])&&(str1[i]!='\0')&&(str2!='\0'))
++i ;
return (i) ;
}

Example 138 : Program to perform Reversal of String


#include <iostream.h>
#include <conio.h>
#include<stdio.h>
void main ( )
{
int count , i ;
char line[40] , reverse[40] ;
puts ("Enter A String - ") ;
gets (line) ;
count = 0 ;
while (line[count] != '\0')
count++ ;
count-- ;
for(i=0 ; i<40 ; i++)
reverse[i] = 0 ;
i=0;
while (count >= 0)
{
reverse[count] = line[i] ;
count-- ;
i++ ;
}
puts ("\nThe reversed string is - ") ;
puts (reverse) ;
getch ( ) ;
}

Example 139 : Program to illustrate Difference between Call by Value & Call by
Reference
#include <iostream.h>
#include <conio.h>
void f (int, int &);
void main( )
{
int a = 22, b = 44;
cout<<" a = "<<a <<",
f(a,b);
cout<<" a = "<<a <<",
f(2*a-3,b);
cout<<" a = "<<a <<",
getch();
}
void f(int x, int *y)
{
x = 88;
* y = 99;
}

b = " <<b<<endl;
b = " <<b<<endl;
b = " <<b<<endl;

You might also like