void main()
{
int x, y, z;
cout<<”Enter two numbers “;
cin>>x>>y;
z=add(x,y); // Function call
cout<<”Addition of two numbers is “;
cout<<z;
}
void main()
{ int m=50; // Local variable declared
show(); // Used in this fun only
}
#include<iostream.h>
int a,b; // Global variable used in any fun
void min()
{ int min=a;
if (a>b)
min=b;
cout<<”Minimum is “<<min;
}
void main()
{ cout<<”Enter the 2 values “;
cin>>a>>b;
min();
}
4. Function Prototypes: (What is function prototype?)
Prototype describes the function interface to the compiler by giving details
such argument passed, their types & return type.
Ex.
Return-Type Fun-Name(Arg-List);
#include<iostream.h>
void max(int x,int y); // Prototype
void main()
{ int a,b;
cout<<”Enter the values of “;
cin>>a>>b;
cout<<”\n Max No is “;
max(a,b); //Fucntion call
}
void max(int x,int y)
{ if(x>y)
return(x);
else
return(y);
}
inline function-Name(Arg-List)
{
Fun Body;
}
#include<iostream.h>
inline int square(int x)
{ return(x*x);
}
void main()
{
cout<<”Square of 10 is “<<square(10);
}
7. Default arguments:
Default values can be provided to arguments through prototypes.
Rules:
Default values should be provided from right to left side.
// Fun Overloading
#include<iostream.h>
int add(int x,int y);
float add(float ,float);
void main()
{
cout<<Addition of 10 & 15 “<<endl;
cout<<add(10,15);
cout<<Addition of 10.5 & 15.5 “<<endl;
cout<<add(10.5,15.5);
}
int add(int x,int y)
{
return(x+y);
}
float add(float x, float y)
{
return(x+y);
}
#include<iostream.h>
int fact(int x);
void main()
{
cout<<”Factorial of a Number “<<endl;
cout<<”Enter a no “<<endl;
cin>>no;
cout<<fact(no);
}
int fact(int n)
{
if (n==1)
return(1);
else
return(n*fact(n-1));
}
// Calculate 1+2+3+………+n-1+n
#include<iostream.h>
int sum(int x);
void main()
{
cout<<”Sum upto n “<<endl;
cout<<”Enter a no “<<endl;
cin>>no;
cout<<sum(no);
}
int sum(int n)
{
if (n==1)
return(1);
else
return(n+sum(n-1));
Register
Scope is local to fun. Initial value undefined. This is used to store the value
directly into CPU registers so that speed of execution increased.
#include<iostream.h>
void main()
{
register int I=5, J=5;
cout<<I<<endl;
cout<<J<<endl;
}
Static
Storage is CPU register. Default value is 0. Scope local to fun.
#include<iostream.h>
void incr();
void main()
{ Output if i Non static
incr(); 1 1
incr(); 2 1
incr(); 3 1
}
void incr()
{ static int i=1;
cout<<i<<endl;
i++;
}
External
Storage is memory. Default value is 0. Scope is global.
#include<iostream.h>
int x=21;
void main()
{ extern int x;
cout<<x<<endl;
}
#include<iostream.h>
void main()
{
int RollNo[3];
RollNo[0]=100;
RollNo[1]=101;
RollNo[2]=102;
cout<<RollNo[0]<<endl;
cout<<RollNo[1]<<endl;
cout<<RollNo[2]<<endl;
}
// Passing array elements to function
void display(int);
void main()
{
int RollNo[]={11,13,14,15,17};
for(int i=0;i<5;i++)
display(RollNo[i]);
}
void display(int x)
{
cout<<”\t”<<x;
}
//Accessing Array by Pointers
void main()
{
int Roll[ ]={11,12,13,14,15};
int *ptr;
ptr=&Roll[0];
for(int i=0;i<5;i++)
{
cout<<”\t”<<*ptr
j++;
Functions gets & puts can be used for strings to read & display.
// Copy a one string into other
#include<iostream.h>
void main()
{
char string1[50],string2[50];
int i=0;
cout<<”Enter first string “<<endl;
gets(string1);
while(string[i] !=’\0’)
{ string2[i]=string1[i];
i++;
}
cout<<”The Copied string is “;
puts(string2);
}
// Concatenation of two strings into one
#include<iostream.h>
void main()
{
char string1[50],string2[50],string3[100];
int i=0,j=0;
cout<<”Enter first string “<<endl;
gets(string1);
cout<<”Enter Second string “<<endl;
gets(string2);
while(string1[i] !=’\0’)
{ string3[i]=string1[i];
i++;
}
#include<iostream.h>
#include<string.h>
void main()
{
char str1[20]=”Dnyanesh”;
char str2[]=”Thombre”;
//Display string
cout<<str1<<” “<<str2<<endl;
cout<<”Chars in str1 & str2 “<<endl
cout<<strlen(str1)<<” “<<strlen(str2)<<endl;
cout<<”Concatenation “<<endl;
strcat(str1,str2);
cout<<str1<<endl;
strcpy(str1,str2); // string copy
cout<<str1<<endl;
cout<<strcmp(str1,str2); //Compare strings
}
#ifdef print
cout<<”This will be displayed if print is defined”<<endl;
cout<<”End of ifdef stmt “;
#else
cout<<”This will be displayed if print is not defined”<<endl;
cout<<”End of else stmt “;
#endif
#define print
similar are #if #else #endif
// Program for
#define pi 3.14
void main()
{
#if pi==3.14
cout<<”PI is defined “ <<pi<<endl;
#else
cout<<”PI is undefined “;
#endif
}
Ex1.
/* 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));
}
Ex2
/* 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);
}
Ex3.
Ex.7
# 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];
}
}
Ex.8
# 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;
}
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);
}