0% found this document useful (0 votes)
53 views

Binary To Decimal and Decimal To Binary Conversion

This document contains code snippets for performing binary to decimal and decimal to binary conversions, searching for a name in a 2D array, and bubble sorting an array. The binary conversion code takes a binary number as input and returns the decimal equivalent. The decimal conversion code takes a decimal number and returns the binary equivalent. The name search code takes a 2D array of names and a search name as input and returns the index if found or not found. The bubble sort code takes an array as input, sorts it in place using the bubble sort algorithm, and outputs the sorted array.

Uploaded by

Random Fibonacci
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
53 views

Binary To Decimal and Decimal To Binary Conversion

This document contains code snippets for performing binary to decimal and decimal to binary conversions, searching for a name in a 2D array, and bubble sorting an array. The binary conversion code takes a binary number as input and returns the decimal equivalent. The decimal conversion code takes a decimal number and returns the binary equivalent. The name search code takes a 2D array of names and a search name as input and returns the index if found or not found. The bubble sort code takes an array as input, sorts it in place using the bubble sort algorithm, and outputs the sorted array.

Uploaded by

Random Fibonacci
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 4

BINARY TO DECIMAL AND DECIMAL TO BINARY

CONVERSION
# include <iostream.h>
#include<math.h>
void main()
{
int ans[100],input[10],x,choice;
while (choice!=-1)
{
cout<<"\n Which of the operations do you want to perform."<<endl;
cout<<"(1) Binary to Decimal Conversion "<<endl;
cout<<"(2) Decimal to Binary Conversion "<<endl;
cout<<"Select the operation or press -1 to exit: ";
cin>>choice;
switch (choice)
{
case 1:
{

cout<<"Enter the binary number of size 10: "<<endl;


for(int j=0;j<10;j++)
cin>>input[j];
int sum=0;
for(int i=0;i<10;i++)
{
sum=sum+(input[i]*pow(2,9-i));
}
cout<<"The decimal equivalent is: "<<sum<<endl;

break;
}
case 2:
{

cout<<"Enter the decimal: "<<endl;


cin>>x;
int cnt=0;
while(x>0)
{
ans[cnt]=x%2;
cnt++;
x=x/2;
}
ans[cnt]=1;
int rev_ans[100];
int cnt1=0;
for(int i=cnt;i>=0;i--)
rev_ans[cnt1]=ans[i];

cout<<"The binary equivalent is: "<<endl;

for(i=0;i<cnt;i++)
cout<<rev_ans[cnt1];

break;
}

}
SEARCHING A NAME IN A 2D ARRAY
# include <iostream.h>
#include<math.h>
#include<string.h>
void main()
{
char name[5][20];
char name1[20];
cout<<"Enter 5 names: "<<endl;
for(int i=0;i<5;i++)
cin>>name[i];
cout<<"Enter the name to be searched: "<<endl;
cin>>name1;
int chk=0;
int ind;
for(i=0;i<5;i++)
{
if(strcmp(name1,name[i])==0)
{
chk=1;
ind=i;
break;
}

}
if(chk==1)
cout<<"The name exists at index: "<<ind<<endl;
else
cout<<"The name doesnt exist"<<endl;

}
BUBBLE SORT

# include <iostream.h>
#include<math.h>
#include<string.h>
void main()
{

int size;
cout<<"Enter the size of the array: "<<endl;
cin>>size;
const int size1=100;
int array[size1];
cout<<"Enter the array elements: "<<endl;
for(int i=0;i<size;i++)
cin>>array[i];
for(i=0;i<size-1;i++)
{
for(int j=i+1;j<size;j++)
{
if(array[i]>array[j])
{
int temp=array[i];
array[i]=array[j];
array[j]=temp;
}

}
cout<<"Array in ascending order: "<<endl;
for(i=0;i<size;i++)
cout<<array[i];
cout<<endl;
}

You might also like