PP Lab Name: Om Dharmik Roll No : 20070124049
Practical No 1 Date: 06/09/2021
Title: Assignment 1: Write a menu driven C++ program using functions to convert any base to
decimal and vice-versa:
i. Decimal to binary
ii. Binary to decimal
iii. Decimal to octal
iv. Octal to decimal
v. Decimal to hexadecimal
vi. Hexadecimal to decimal
Description:
In this menu driven program, we aim to perform various conversion operations using C++
programming.
Program Code:
#include<iostream>
#include<math.h>
#include<string.h>
using namespace std;
void menu()
{
cout<<"1. Decimal to binary";
cout<<"\n2. Binary to decimal";
cout<<"\n3. Decimal to Octal";
cout<<"\n4. Octal to Decimal";
cout<<"\n5. Decimal to Hexadecimal";
cout<<"\n6. Hexadecimal to Decimal";
}
void dtb()
{
int ar[16],n,x=0;
cout<<"\nEnter the number to be converted: ";
cin>>n;
do
PP Lab Name: Om Dharmik Roll No : 20070124049
{
if(n%2==0)
{
ar[x]=0;
x++;
n=n/2;
}
else if(n%2==1)
{
ar[x]=1;
x++;
n=(n-1)/2;
}
}while(n!=0);
x--;
cout<<"Binary Equivalent: ";
for(x;x>=0;x--)
{
cout<<ar[x];
}
}
void btd()
{
int ar[16],x=0,n;
cout<<"\nEnter the number to be converted: ";
cin>>n;
while(n!=0)
{
if(n%10==0)
{
ar[x]=0;
x++;
n=n/10;
}
else
{
ar[x]=1;
x++;
n=(n-1)/10;
PP Lab Name: Om Dharmik Roll No : 20070124049
}
}
int i=0,sum=0;
while(i<x)
{
if(ar[i]==1)
{
sum = sum + pow(2,i);
}
i++;
}
cout<<"Decimal Equivalent: "<<sum;
void dto()
{
int octalNum[100];
int n;
cout<<"Enter the number to be converted to octal: ";
cin>>n;
int i = 0;
while (n != 0)
{
octalNum[i] = n % 8;
n = n / 8;
i++;
}
cout<<"Ocatal Equivalent:";
for (int j = i - 1; j >= 0; j--)
cout<<octalNum[j];
}
void otd()
{
int n;
cout<<"\nEnter the number to be converted to decimal: ";
cin>>n;
int num = n;
int dec_value = 0;
PP Lab Name: Om Dharmik Roll No : 20070124049
int base = 1;
int temp = num;
while (temp)
{
int last_digit = temp % 10;
temp = temp / 10;
dec_value += last_digit * base;
base = base * 8;
}
cout<<"Decimal equivalent:"<<dec_value;
}
void dth()
{
char hexaDeciNum[100];
int n;
cout<<"\nEnter the number to be converted to hexadecimal: ";
cin>>n;
int i = 0;
while (n != 0)
{
int temp = 0;
temp = n % 16;
if (temp < 10) {
hexaDeciNum[i] = temp + 48;
i++;
}
else {
hexaDeciNum[i] = temp + 55;
i++;
}
n = n / 16;
}
cout<<"Hexadecimal equivalent: ";
for (int j = i - 1; j >= 0; j--)
cout << hexaDeciNum[j];
}
PP Lab Name: Om Dharmik Roll No : 20070124049
void htd()
{
char num[50];
cout<<"Enter the number to be converted to decimal: ";
cin>>num;
int len = strlen(num);
int base = 1;
int temp = 0;
for (int i=len-1; i>=0; i--)
{
if (num[i]>='0' && num[i]<='9')
{
temp += (num[i] - 48)*base;
base = base * 16;
}
else if (num[i]>='A' && num[i]<='F')
{
temp += (num[i] - 55)*base;
base = base*16;
}
}
cout<<"\nDecimal equivalent: "<<temp;
}
int main()
{
int a;
menu();
cout<<"\n\nEnter your choice: ";
cin>>a;
switch(a)
{
case 1:
dtb();
break;
case 2:
btd();
break;
case 3:
dto();
PP Lab Name: Om Dharmik Roll No : 20070124049
break;
case 4:
otd();
break;
case 5:
dth();
break;
case 6:
htd();
break;
default:
cout<<"Invalid input!!";
break;
}
//getch();
}
Input and Output
PP Lab Name: Om Dharmik Roll No : 20070124049
PP Lab Name: Om Dharmik Roll No : 20070124049
Conclusion:
In this program we performed various operations like decimal to binary, decimal to octal, decimal
to hexadecimal and vice-versa.
Practice programs: Practice Program: C++ program to print pyramid pattern:
*
* *
* * *
* * * *
* * * * *
#include <iostream>
using namespace std;
PP Lab Name: Om Dharmik Roll No : 20070124049
// Function to demonstrate printing pattern
void triangle(int n)
{ int k = 2 * n - 2;
// Outer loop to handle number of rows
// n in this case
for (int i = 0; i < n; i++) {
// Inner loop to handle number spaces
// values changing acc. to requirement
for (int j = 0; j < k; j++)
cout << " ";
// Decrementing k after each loop
k = k - 1;
// Inner loop to handle number of columns
// values changing acc. to outer loop
for (int j = 0; j <= i; j++) {
// Printing stars
cout << "* ";
}
// Ending line after each row
cout << endl;
}
}
// Driver Code
PP Lab Name: Om Dharmik Roll No : 20070124049
int main()
{
int n = 5;
// Function Call
triangle(n);
return 0;
}