C++ Programs Examples With Output [2021 CPP Examples]

C++ programing

This post include list of basic/simple C++(CPP) programming logic examples with output.This is a basic small and simple C programs tutorial for beginner to help for practicing and learn for interviews.This post include list of C++ programs,explanation with output.

This post also include basic C++ programs examples PDF if you want than download C++ programming examples with output PDF from download section or Click here.

Numeric Array Programs

C++ provides a data structure such as Array, Array is a collection of data of same types stored in sequential memory location. Array reduce length of code in some extend. The elements in an array is accessed using an index .This section contains C++ programming examples on Arrays with explanation.

List of all C++ program based on numeric array are listed below.

1.Write a program to convert the given decimal number in to binary number.

#include<iostream.h>
#include<conio.h>
#define MAXSIZE 100
void main()
{
int number=0,i=0,Binary[MAXSIZE],index=0;
clrscr();
cout<<"\n Enter the decimal number you want to convert : ";
cin>>number;
while (number > 0)
{
Binary[index] = number % 2;
number = number / 2;
index++;
}
cout<<"\n Binary of given decimal number is : ";
for (i = index - 1; i >= 0; i--)
{
cout<<Binary[i];
}
getch();
}

2.Write a program to delete an element from array.

#include<iostream.h>
#include<conio.h>
#define MAXSIZE 100
void main()
{
int n,a[MAXSIZE],i=0,k=0,position;
clrscr();
cout<<"\n Enter size of array \n";
cin>>n;
cout<<"\n Enter array elements \n";
for (i = 0; i < n; i++) {
cin>>a[i];
}
cout<<"\n\n Array elements are : \n";
for (i = 0; i < n; i++) {
cout<<a[i]<<"\t";
}
cout<<"\n\n Enter the position of element to be deleted \n";
cin>>position;
position = position - 1;
for (i = 0; i < n; i++) {
if (i != position) {
a[k] = a[i];
k++;
}
}
cout<<"\n Array elements are : \n";
for (i = 0; i < k; i++) {
cout<<a[i]<<"\t";
}
getch();
}

3.Write a program to insert an element in array.

#include<iostream.h>
#include<conio.h>
#define MAXSIZE 100
void main()
{
int n,a[MAXSIZE],i=0,position,element;
clrscr();
cout<<"\n Enter size of array \n";
cin>>n;
cout<<"\n Enter array elements \n";
for (i = 0; i < n; i++)
{
cin>>a[i];
}
cout<<"\n\n Array elements are : \n";
for (i = 0; i < n; i++)
{
cout<<a[i]<<"\t";
}
cout<<"\n\n Enter the element to be inserted ";
cin>>element;
cout<<"\n\n Enter the position where "<<element<<" is to be inserted ";
cin>>position;
position = position - 1;
for (i = n - 1; i >= position; i--)
{
a[i + 1] = a[i];
}
a[position] = element;
cout<<"\n Array elements are : \n";
for (i = 0; i < n + 1; i++)
{
cout<<a[i]<<"\t";
}
getch();
}

4.Write a program to remove consecutive duplicates from an array.

#include<iostream.h>
#include<conio.h>
#define MAXSIZE 100
void main()
{
int n,a[MAXSIZE],i=0,k=1,element;
clrscr();
cout<<"\n Enter size of array \n";
cin>>n;
cout<<"\n Enter array elements \n";
for (i = 0; i < n; i++)
{
cin>>a[i];
}
element = a[0];
for (i = 1; i < n; i++) {
if (a[i] != element) {
a[k] = a[i];
element = a[i];
k++;
}
}
cout<<"\n Array without consecutive duplicate \n";
for (i = 0; i < k; i++) {
cout<<a[i]<<"\t";
}
cout<<"\n";
getch();
}

5.Write a program to search an element in an array.

#include<iostream.h>
#include<conio.h>
#define MAXSIZE 100
void main()
{
int n,a[MAXSIZE],location[MAXSIZE],i=0,k=0,ele;
clrscr();
cout<<"\n Enter size of array \n";
cin>>n;
cout<<"\n Enter array elements \n";
for (i = 0; i < n; i++)
{
cin>>a[i];
}
cout<<"\n Enter an element to be searched ";
cin>>ele;
for (i = 0; i < n; i++)
{
if (a[i] == ele)
{
location[k] = (i + 1);
k++;
}
}
if (k > 0) {
cout<<"\n Element "<<ele<<" is present at positions :";
for(i = 0; i < k; i++)
{
if(i < k-1) {
cout<<location[i]<<",";
}
else
{
cout<<location[i];
}
}
}
else {
cout<<"\n Element "<<ele<<" not found";
}
getch();
}

6.Write a program to print array without duplicate numbers.

#include<iostream.h>
#include<conio.h>
#define MAXSIZE 100
void main()
{
int n,a[MAXSIZE],b[MAXSIZE],i=0,j=0,k=0,flag=1;
clrscr();
cout<<"\n Enter size of array \n";
cin>>n;
cout<<"\n Enter array elements \n";
for (i = 0; i < n; i++)
{
cin>>a[i];
}
for (i = 0; i < n; i++) {
for (j = 0; j < k; j++) {
if (a[i] == b[j]) {
flag = 0;
break;
}
else {
flag = 1;
}
}
if (flag == 1) {
b[k] = a[i];
k++;
}
}
cout<<"\n Array without duplicates : \n";
for (i = 0; i < k; i++) {
cout<<b[i]<<"\t";
}
cout<<"\n";
getch();
}

7.Write a program to find series of factorial number.

#include<iostream.h>
#include<conio.h>
#define MAX 500
int multiply(int x, int res[], int res_size);
void factorial(int n)
{
int res[MAX],i=0,x=0,res_size = 1;;
res[0] = 1;
for (x=2; x<=n; x++)
{
res_size = multiply(x, res, res_size);
}
for (i=res_size-1; i>=0; i--)
{
cout<<res[i];
}
}
int multiply(int x, int res[], int res_size)
{
int carry = 0,i=0,prod;
for (i=0; i<res_size; i++)
{
prod = res[i] * x + carry;
res[i] = prod % 10;
carry  = prod/10;
}
while (carry)
{
res[res_size] = carry%10;
carry = carry/10;
res_size++;
}
return res_size;
}
void main()
{
int n,i=0;
clrscr();
cout<<"\n Enter a number to which you want to print factorials of every number : ";
cin>>n;
cout<<"Factorial of every number till given number is \n";
for(i=1; i<=n; i++)
{
factorial(i);
cout<<"\n";
}
getch();
}

8.Write a program to sort an array in descending order.

#include<iostream.h>
#include<conio.h>
#define MAXSIZE 100
void main()
{
int n,i=0,j=0,a[MAXSIZE],t;
clrscr();
cout<<"\n Enter size of array \n";
cin>>n;
cout<<"\n Enter array elements \n";
for (i = 0; i < n; i++) {
cin>>a[i];
}
for (i = 0; i < n; i++) {
for (j = i + 1; j < n; j++) {
if (a[i] < a[j]) {
t = a[i];
a[i] = a[j];
a[j] = t;
}
}
}
cout<<"\n Array elements in descending order: \n";
for (i = 0; i < n; i++) {
cout<<a[i]<<"\t";
}
getch();
}

9.Write a program to sort an array in ascending order.

#include<iostream.h>
#include<conio.h>
#define MAXSIZE 100
void main()
{
int n,i=0,j=0,a[MAXSIZE],t;
clrscr();
cout<<"\n Enter size of array \n";
cin>>n;
cout<<"\n Enter array elements \n";
for (i = 0; i < n; i++) {
cin>>a[i];
}
for (i = 0; i < n; i++) {
for (j = i + 1; j < n; j++) {
if (a[i] > a[j]) {
t = a[i];
a[i] = a[j];
a[j] = t;
}
}
}
cout<<"\n Array elements in ascending order: \n";
for (i = 0; i < n; i++) {
cout<<a[i]<<"\t";
}
getch();
}

Leave a Reply

Your email address will not be published. Required fields are marked *