You are on page 1of 7

1. C++ program to find the sum of two numbers using function call by value.

#include<iostream>

using namespace std;

float sum(float,float);

int main()

float a,b;

cout<<"Enter two numbers: ";

cin>>a>>b;

float s=sum(a,b);

cout<<"The sum is: "<<s;

float sum(float p, float q)

return(p+q);

2. C++ program to find the sum of two numbers using function call by address.

#include<iostream>

using namespace std;

float sum(float *,float *);

int main()

float a,b;
cout<<"Enter two numbers: ";

cin>>a>>b;

float s=sum(&a,&b);

cout<<"The sum is: "<<s;

float sum(float *p, float *q)

return(*p + *q);

3. C++ program to find the sum of two numbers using function call by reference.

#include<iostream>

using namespace std;

float sum(float &,float &);

int main()

float a,b;

cout<<"Enter two numbers: ";

cin>>a>>b;

float s=sum(a,b);

cout<<"The sum is: "<<s;

}
float sum(float &p, float &q)

return(p+q);

4. C++ Program to display the array elements.

#include<iostream>

using namespace std;

int main()

int arr[20],n;

cout<<"Enter the size of the array: ";

cin>>n;

cout<<"\n Enter the array elements: ";

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

cin>>arr[i];

cout<<"The array elements are: ";

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

cout<<arr[i]<<"\t";

5. C++ Program to find the largest and smallest element in an array.

#include<iostream>

using namespace std;

int main()
{

int arr[20],n;

cout<<"Enter the size of the array: ";

cin>>n;

cout<<"\n Enter the array elements: ";

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

cin>>arr[i];

int max=arr[0], min=arr[0];

for(int i=1;i<n;i++)

if(arr[i] > max)

max=arr[i];

for(int i=1;i<n;i++)

if(arr[i] < min)

min=arr[i];

cout<<"\n The largest element is: "<<max<<endl;

cout<<"The smallest element is: "<<min;

6. C++ Program to print the array elements using pointers.

#include<iostream>

using namespace std;

int main()

int arr[20],n;
cout<<"Enter the size of the array: ";

cin>>n;

cout<<"\n Enter the array elements: ";

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

cin>>arr[i];

int *p=arr;

cout<<"The array elements are: "<<endl<<"Address"<<" "<<"Element"<<endl;

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

cout<<(p+i)<<" "<<*(p+i)<<endl;

7. C++ Program to display the array elements using function.

#include<iostream>

using namespace std;

void disparr(int[],int);

int main()

int arr[20],n;

cout<<"Enter the size of the array: ";

cin>>n;

cout<<"\n Enter the array elements: ";

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

cin>>arr[i];
cout<<"The array elements are: "<<endl;

disparr(arr,n);

void disparr(int a[], int size)

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

cout<<a[i]<<" ";

8. C++ Program to find the largest and smallest element in an array using function.

#include<iostream>

using namespace std;

int largest(int[],int);

int smallest(int[],int);

int main()

int arr[20],n;

cout<<"Enter the size of the array: ";

cin>>n;

cout<<"\n Enter the array elements: ";

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

cin>>arr[i];

int large=largest(arr,n);

int small=smallest(arr,n);
cout<<"\n The largest element is: "<<large<<endl;

cout<<"The smallest element is: "<<small;

int largest(int a[], int sz)

int max=a[0];

for(int i=1;i<sz;i++)

if(a[i] > max)

max=a[i];

return(max);

int smallest(int a[], int sz)

int min=a[0];

for(int i=1;i<sz;i++)

if(a[i] < min)

min=a[i];

return(min);

You might also like