Practical No: 1 Date: 31/07/2023 Title: Write A C++ Program Using Functions To Convert Decimal To Any Base and Vice Versa. Description
Practical No: 1 Date: 31/07/2023 Title: Write A C++ Program Using Functions To Convert Decimal To Any Base and Vice Versa. Description
Title: Write a C++ program using functions to convert decimal to any base and vice versa.
Description:
The general form of a C++ function definition is as follows −
● Return Type − A function may return a value. The return_type is the data type of
the value the function returns. Some functions perform the desired operations
without returning a value. In this case, the return_type is the keyword void.
● Function Name − This is the actual name of the function. The function name and
the parameter list together constitute the function signature.
● Function Body − The function body contains a collection of statements that define
what the function does.
Program Code:
#include<stdio.h>
#include<math.h>
PP Lab PRN-23070122078
void binary_to_decimal();
void decimal_to_binary();
void decimal_to_hexadecimal();
void decimal_to_octal();
void hexadecimal_to_decimal();
void octal_to_decimal();
int main() {
printf("Enter 1 for binary to decimal conversion\n");
printf("Enter 2 for decimal to binary conversion\n");
printf("Enter 3 for decimal to hexadecimal conversion\n");
printf("Enter 4 for decimal octal conversion\n");
printf("Enter 5 for hexadecimal to decimal conversion\n");
printf("Enter 6 for octal to decimal conversion\n"); int
choice;
printf("Enter choice: ");
scanf("%d",&choice);
switch(choice){
case 1: binary_to_decimal();
break;
case 2: decimal_to_binary();
break;
case 3: decimal_to_hexadecimal();
break;
case 4: decimal_to_octal();
break;
case 5: hexadecimal_to_decimal();
break;
case 6: octal_to_decimal();
break;
default: printf("Enter valid choice");
break;
}
return 0;
}
void binary_to_decimal(){
char ch[100];
int sum=0,len=0;
printf("Enter number: ");
scanf("%s",&ch);
for(int i=0;ch[i]!=0;i++){
PP Lab PRN-23070122078
len++;
}
for(int i=0;ch[i]!='\0';i++){
int z=ch[i]-'0';
sum+=z*pow(2,len-1-i);
}
printf("After Conversion: %d",sum);
}
void decimal_to_binary(){
int num,sum=0;
printf("Enter number: ");
scanf("%d",&num);
char str[100];
if(num==0){
printf("After conversion: 0");
}else{
for(int i=0;num>0;i++){
str[i]='0'+num%2;
num/=2;
sum++;
}
printf("After conversion: ");
for(int j=sum-1;j>=0;j--){
printf("%c",str[j]);
}
}
}
void decimal_to_hexadecimal(){
int n,sum=0;
printf("Enter number: ");
scanf("%d",&n);
char ch[100];
if(n==0){
printf("After conversion: 0");
}
else{
for(int i=0;n>0;i++){
if(n%16<10){
ch[i]='0'+n%16;
PP Lab PRN-23070122078
}
else{
ch[i]='A'+n%16-10;
}
n=n/16;
sum++;
}
printf("After conversion: ");
for(int j=sum-1;j>=0;j--){
printf("%c",ch[j]);
}
}
}
void decimal_to_octal(){
int n,sum=0;
printf("Enter number: ");
scanf("%d",&n);
char ch[100];
if(n==0){
printf("After conversion: 0");
}else{
for(int i=0;n>0;i++){
ch[i]='0'+n%8;
n=n/8;
sum++;
}
printf("After conversion: ");
for(int j=sum-1;j>=0;j--){
printf("%c",ch[j]);
}
}
}
void hexadecimal_to_decimal() {
char ch[100];int z;
int sum=0,len=0;
printf("Enter number: ");
scanf("%s",&ch);
for(int i=0;ch[i]!=0;i++){
len++;
PP Lab PRN-23070122078
}
for(int i=len-1;i>=0;i--){
if(ch[i]>='0' && ch[i]<='9'){
z=ch[i]-'0';
}else{
z=ch[i]-'7';
}
sum+=z*pow(16,len-1-i);
}
printf("After conversion: %d",sum);
}
void octal_to_decimal() {
char ch[100];
int sum=0,len=0;
printf("Enter number: ");
scanf("%s",&ch);
for(int i=0;ch[i]!=0;i++){
len++;
}
for(int i=len-1;i>=0;i--){
int z=ch[i]-'0';
sum+=z*pow(8,len-1-i);
}
printf("After conversion: %d",sum);
}
Practice programs:
1. Write a program to convert degree to Fahrenheit using functions.
2. Write a function to calculate GCD and LCM of the numbers entered.
3. Write a program to calculate different statistics of the data entered like mean, median etc.
using functions.