0% found this document useful (0 votes)
18 views6 pages

List of Array Programm

The document provides examples of C++ programs demonstrating one-dimensional and two-dimensional arrays, as well as conversions between binary and decimal numbers. It includes code snippets for initializing arrays, calculating sums and averages, and converting between number systems. Each example is accompanied by explanations of the logic and structure of the code.

Uploaded by

Jitendra Dangra
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views6 pages

List of Array Programm

The document provides examples of C++ programs demonstrating one-dimensional and two-dimensional arrays, as well as conversions between binary and decimal numbers. It includes code snippets for initializing arrays, calculating sums and averages, and converting between number systems. Each example is accompanied by explanations of the logic and structure of the code.

Uploaded by

Jitendra Dangra
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

C++ One Dimensional Array Example

/* C++ One Dimensional Array */

#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int arr[5] = {1, 2, 3, 4, 5};
int i;
for(i=0; i<5; i++)
{
cout<<"arr["<<i<<"] = "<<arr[i]<<"\n";
}
getch();
}

Here is another C++ example, also demonstrating one dimension array in C++

/* C++ One Dimensional Array */

#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int arr[10];
int i;
int sum=0, avg=0;
cout<<"Enter 10 array elements: ";
for(i=0; i<10; i++)
{
cin>>arr[i];
sum = sum + arr[i];
}
cout<<"\nThe array elements are: \n";
for(i=0; i<10; i++)
{
cout<<arr[i]<<" ";
}
cout<<"\n\nSum of all elements is: "<<sum;
avg = sum/10;
cout<<"\nAnd average is: "<<avg;
getch();
}

C++ Two Dimensional Array Example

Here are some C++ programs, demonstrating two dimensional array in C++

/* C++ Two Dimensional Array */

#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int arr[5][2] = { {1, 2}, {1, 3}, {1, 4}, {1, 5}, {1, 6} };
int i, j;
for(i=0; i<5; i++)
{
for(j=0; j<2; j++)
{
cout<<"arr["<<i<<"]["<<j<<"] = "<<arr[i][j]<<"\n";
}
}
getch();
}

Here is the sample output of the above C++ program:

Example: C++ example to convert binary number to


decimal number
Let’s take a look at the program logic:

1. First, extract digits from the right side of the number.


2. The extracted digit is then multiplied by the proper base (power of 2).
3. Finally, these digits are added together to get an equivalent decimal number.
//C++ program to convert binary to decimal
#include <iostream>
#include <cmath>

using namespace std;

int main()
{
long long num;
int decimalNum, i, rem;

cout << "Enter any binary number: ";


cin >> num;

decimalNum = 0;
i = 0;

//converting binary to decimal


while (num != 0)
{
rem = num % 10;
num /= 10;
decimalNum += rem * pow(2, i);
++i;
}

cout << "Equivalent Decimal number: " << decimalNum << endl;

return 0;
}
Output

Decimal to Binary number program in C++


Here, we are going to convert decimal number to binary number.
Let’s take a look at program logic:

1. The remainder is stored in a variable when it is divided by 2


2. Divide the number by 2
3. Repeat these steps till the number is greater than zero

Example: C++ program to convert the decimal number to the binary


//C++ program to convert decimal to binary

#include <iostream>
#include <cmath>
using namespace std;
int main()
{ int num, binaryNum = 0;
int i = 1, rem;
cout << "Enter any decimal number: ";
cin >> num;
//while loop to convert decimal to binary
while (num != 0)
{
rem = num % 2;
num /= 2;
binaryNum += rem * i;
i *= 10;
}
cout << "Equivalent Binary Number: " << binaryNum << endl;
return 0;
}

You might also like