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

Practical No: 1 Date: 31/07/2023 Title: Write A C++ Program Using Functions To Convert Decimal To Any Base and Vice Versa. Description

this is a research on how trading works

Uploaded by

singhdakshit.20
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)
7 views6 pages

Practical No: 1 Date: 31/07/2023 Title: Write A C++ Program Using Functions To Convert Decimal To Any Base and Vice Versa. Description

this is a research on how trading works

Uploaded by

singhdakshit.20
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
You are on page 1/ 6

PP Lab PRN-23070122078

Practical No: 1 Date: 31/07/2023

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 function_name( parameter list ) {


body of the function
}
A C++ function definition consists of a function header and a function body. Here are all
the parts of a function −

● 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.

● Parameters − A parameter is like a placeholder. When a function is invoked, you


pass a value to the parameter. This value is referred to as actual parameter or
argument. The parameter list refers to the type, order, and number of the
parameters of a function. Parameters are optional; that is, a function may contain
no parameters.

● 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);
}

Input and Output


PP Lab PRN-23070122078

Conclusion: Thus we have implemented the functions in C++.

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.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy