Hello, I want to make a code in c++ programming that do Multiplication and Addit
ID: 3766215 • Letter: H
Question
Hello,
I want to make a code in c++ programming that do Multiplication and Addition in binary.
Also, to convert from decimal to binary and binary to decimal
I need you to use with code Switch statement For example : chose 1 to do mutiplication in binary, chose to do Addition in binary and chose 3 to convert from decimal to binary, chose 4 to convert from binary to decimal :
I need it to included prototype, fumction definition ::
plese before send to me the code, make sure the code is correct. Some Expert answer me with a wrong code
i need it to be like this format ::
#include<iostream.h>
#include<stdio.h>
int cube(int) ; // this is prototype
int square(int) ;
main()
{
int input, on = 1, choice, result ;
while(on==1)
{
cout<<" Enter 1 to find cube, 2 to find square, 3 to exit " ;
cin>> choice ;
switch(choice)
{
case 1:
cout<< " Enter number to find cube " ;
cin>> input ;
result = cube(input) ;
cout<<" Cube of number is "<< result ;
break ;
case 2:
cout<< " Enter number to find square " ;
cin>> input ;
result = square(input) ;
cout<<" Square of number is "<< result ;
break ;
case 3:
cout<<" Program terminated on user request " ;
on = 0 ;
break ;
default:
cout<<" You pressed a wrong option " ;
} // end switch
} // end while
getchar() ;
} // end main funciton
int cube(int a) // this is function definition
{
int c = a * a * a ;
return c ;
}
int square(int a) // this is function definition
{
int c = a * a ;
return c ;
}
Thank you so much
Explanation / Answer
#include<iostream>
using namespace std;
int binaryproduct(int, int);
int main()
{
int choice ;
long binary3, binary4, multiply;
int digit, factor = 1;
long i=1,sum=0;
long bin, dec = 0, rem, num, base = 1;
long binary1, binary2;
int j = 0, remainder = 0, sum1[20];
while (1)
{
cout<<"1. Binary Multiplication ";
cout<<"2. Binary addition ";
cout<<"3. Binary to Decimal ";
cout<<"4. Decimal to Binary ";
cout<<"5. exit ";
cout<<" Your choice ?";
cin>>choice ;
switch ( choice)
{
case 1 :
multiply=0;
cout<<"Enter the first binary number: ";
cin>>binary1;
cout<<"Enter the second binary number: ";
cin>>binary2;
while (binary2 != 0)
{
digit = binary2 % 10;
if (digit == 1)
{
binary1 = binary1 * factor;
multiply = binaryproduct(binary1, multiply);
}
else
binary1 = binary1 * factor;
binary2 = binary2 / 10;
factor = 10;
}
cout<<"Product of two binary numbers: "<< multiply;
break;
case 2 :
cout<<"Enter the first binary number: ";
cin>>binary1;
cout<<"Enter the second binary number: ";
cin>>binary2;
while (binary1 != 0 || binary2 != 0)
{
sum1[j++] =(binary1 % 10 + binary2 % 10 + remainder) % 2;
remainder =(binary1 % 10 + binary2 % 10 + remainder) / 2;
binary1 = binary1 / 10;
binary2 = binary2 / 10;
}
if (remainder != 0)
sum1[j++] = remainder;
--j;
cout<<"Sum of two binary numbers: ";
while (j >= 0)
cout<<sum1[j--];
break;
case 3 :
cout << "Enter the binary number(1s and 0s) : ";
cin >> num;
bin = num;
while (num > 0)
{
rem = num % 10;
dec = dec + rem * base;
base = base * 2;
num = num / 10;
}
cout << "The decimal equivalent of " << bin << " : " << dec << endl;
break;
case 4 :
cout<<"Enter the decimal to be converted:";
cin>>dec;
do
{
rem=dec%2;
sum=sum + (i*rem);
dec=dec/2;
i=i*10;
}while(dec>0);
cout<<"The binary of the given number is:"<<sum<<endl;
break;
case 5 :
exit(0) ;
default :
cout<<"Enter valid choice ";
break;
}
cout<<" ";
}
}
int binaryproduct(int binary1, int binary2)
{
int i = 0, remainder = 0, sum[20];
int binaryprod = 0;
while (binary1 != 0 || binary2 != 0)
{
sum[i++] =(binary1 % 10 + binary2 % 10 + remainder) % 2;
remainder =(binary1 % 10 + binary2 % 10 + remainder) / 2;
binary1 = binary1 / 10;
binary2 = binary2 / 10;
}
if (remainder != 0)
sum[i++] = remainder;
--i;
while (i >= 0)
binaryprod = binaryprod * 10 + sum[i--];
return binaryprod;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.