C++ Examples Program
C++ Examples Program
PROGRAMMING
Question#1
Write a C++ program to find the sum of array?
#include<iostream>
void main()
int array[10];
int sum = 0;
cout << "enter the value of which you want to find the sum" << endl;
Output:
Question#2
Write a C++ program to reverse an array?
#include<iostream>
void main()
int a[5];
Output:
Question#3
Write a C++ program to find the maximum number in an array?
#include<iostream>
void main()
int array[10];
}
RIDA FATIMA BSE223046
NTRODUCTION TO
PROGRAMMING
int largest = 0;
largest = array[i];
OUTPUT:
Question#4
Write a C++ program to find the linearly search an element in an array?
#include<iostream>
void main()
int array[10];
int search ;
cout << "enter the number you want to search" << endl;
if (search == array[i])
OUTPUT:
Question#5
Write a C++ program to find the smallest and second smallest element in an
array?
#include<iostream>
int main()
int arr[10];
small(arr, 10);
int n = 0, y[10];
n = x[i];
x[i] = x[j];
x[j] = n;
cout << "Second smallest number in the array is " << x[1];
Output:
Question#6
Write a C++ program for median of two sorted arrays?
#include<iostream>
int main()
median(a, 5, b, 6);
c[i] = x[i];
c[size + i] = y[i];
p = 0;
p = c[i];
c[i] = c[j];
c[j] = p;
}
RIDA FATIMA BSE223046
NTRODUCTION TO
PROGRAMMING
m = (size + size1) / 2;
if ((size + size1) % 2 == 0)
else
median = median / 2;
return median;
Output:
Question#7
Write a C++ program to multiply two matrixes?
#include<iostream>
void main()
int matrixA[2][2];
int matrixB[2][2];
int mult;
cout<<matrixA[i][j]<<" ";
}
RIDA FATIMA BSE223046
NTRODUCTION TO
PROGRAMMING
Output:
Question#8
Write a C++ program for the majority element?
#include<iostream>
void main()
int array[10];
int majoritycount = 1;
int count = 1;
if (array[i]==array[j])
count++;
majority=array[i];
majoritycount=count;
OUTPUT:
Question#9
Write a C++ program to find the number occurring odd number of times?
#include<iostream>
void main()
int array[10];
int oddcount = 1;
int count = 1;
if (array[i]==array[j])
count++;
odd=array[i];
oddcount=count;
OUTPUT:
Question#10
Write a C++ program for given an array and a number x, check for pair in A[] with
sum as x?
#include<iostream>
void main()
int a[5];
int p = 0;
int y = 0;
if (a[i] + a[j] == y)
p = 1;
if (p == 1)
else
Output:
Question#11
Write a C++ program to find the number occurring odd and number of times?
#include<iostream>
int main()
int A[10];
odd(A, 10);
int n,count=1,y[10];
n = x[i];
x[i] = x[j];
x[j] = n;
count = 1;
count++;
y[i] = count;
x[j] = -1;
int p=0;
if (y[i] % 2 != 0 )
cout << x[i] << " occur odd number of times" << endl;
p = 1;
if (p == 0)
cout << "No number occurs odd number of time" << endl;
Output:
Question#12
Write a C++ program to find the missing number?
RIDA FATIMA BSE223046
NTRODUCTION TO
PROGRAMMING
#include<iostream>
int main()
int A[10],n;
n = A[i];
A[i] = A[j];
A[j] = n;
int p = 0;
RIDA FATIMA BSE223046
NTRODUCTION TO
PROGRAMMING
if (A[i] - A[i + 1] == 1)
p = 0;
else
p = 1;
if (p == 0)
OUPUT:
Question#13
Write a C++ program for largest sum contiguous subarray?
#include<iostream>
return sum;
int main()
return 0;
Output:
Question#14
Write a C++ program for merge an array of size n into another array of size m+n?
#include<iostream>
#define NA -1
int j = size - 1;
if (mPlusN[i] != NA)
mPlusN[j] = mPlusN[i];
j--;
int i = n;
int j = 0;
int k = 0;
mPlusN[k] = mPlusN[i];
k++;
i++;
else
mPlusN[k] = N[j];
k++;
i++;
int main()
int N[] = { 5, 7, 9, 25 };
moveToEnd(mPlusN, m + n);
merge(mPlusN, N, m, n);
printArray(mPlusN);
return 0;
Output:
Question#15
Write a C++ program to sort element by frequency?
#include<iostream>
int main()
int freq[n] = { 0 };
freq[array[i]]++;
if (freq[i] != 0)
return 0;
Output: