Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

1.) a.) Write a program C++ that takes a given number less than 256 in DECIMAL a

ID: 674351 • Letter: 1

Question

1.) a.) Write a program C++ that takes a given number less than 256 in DECIMAL and converts it to BINARY.

b.)Write a program C++ that takes the output above in BINARY and converts it to DECIMAL.

2.) Write an algorithm for A through D below that has all of the six (6) properties listed above. Feel free to use text or a flow-chart to describe your algorithm. THEN, WRITE THE PROGRAM IN YOUR CHOSEN LANGUAGE.

a.) Three people ate dinner at a restaurant and want to split the bill. The total is $35.27,   and they want to leave a 15 percent tip. How much should each person pay?

b.)Calculate the area and perimeter of a rectangular room, 12.5 meters by 16.7 meters.

c.) Convert temperatures from Fahrenheit to Celsius. The formula for that is: C = 5 / 9 * (F - 32). (Hint: Watch out for the integer-division gotcha!)

   d.) You’re on a long car trip and arrive at a gas station. It’s 200 km to the next station.

Write a program to figure out if you need to buy gas here, or if you can wait for the next station. The program should ask these questions:

How big is your tank, in liters?

How full is your tank (in percent—for example, half full = 50)?

How many km per liter does your car get?

Explanation / Answer

1)(a)

/*C++ program that prompts user to enter a number
0 to 255 and prints the number in binary format */

#include<iostream>
using namespace std;
int main()
{

   //declare variables of size and input
   const int SIZE=8;
   short input;
   short binary[SIZE];


   cout<<"Enter an input number ( 0 -255 ) : ";
   //read input
   cin>>input;

   //convert to binary
   for(int index=SIZE-1;index>=0;index--)
   {
       if(input%2==0)
           binary[index]=0;
       else if(input%2==1)
           binary[index]=1;

       input=input/2;

   }


   cout<<"Binary Number : "<<endl;
   for(int index=0;index<SIZE;index++)
       cout<<binary[index];

   system("pause");
   return 0;
}

Sample Output:

Enter an input number ( 0 -255 ) : 15
Binary Number :
00001111
------------------------------------------------------------------------------------------------------------------------------


/*C++ program that prompts user to enter a binary number
and prints the number in decimal format */

#include<iostream>
#include<math.h>
using namespace std;
int main()
{

   //declare variables of size and input
   int binary=0, i=0, rem;
   int decimal=0;


   cout<<"Enter binary number: ";
   //read input
   cin>>binary;
  
   while (binary!=0)
   {
       rem = binary%10;
       binary=binary/10;
       decimal += rem*pow(2.0,i);
       ++i;
   }

   cout<<"Decimal Number : ";
   cout<<decimal<<endl;


   cout<<endl;
   cout<<endl;
   system("pause");
   return 0;
}

Sample Output:

Enter binary number: 00001111
Decimal Number : 15

------------------------------------------------------------------------------------------------------------------------------------------

2(a)

/*C++ program that splits the bill into three members with a tip of 15 perceange*/
#include<iostream>
#include<math.h>
using namespace std;
int main()
{
  
   //double type of value
   double totalBill=35.27;

   //calculate the tip value
   double TIP=totalBill*0.15;

   //add tip to bill

   double newbill=totalBill+TIP;

   //split the new bill into three persons


   double share=newbill/3;

   cout<<"Bill: "<<totalBill<<endl;
   cout<<"Tip: "<<TIP<<endl;
   cout<<"New Bill with tip :"<<newbill<<endl;
   cout<<"Three persons share of bill with Tip: "<<share<<endl;

   cout<<endl;
   cout<<endl;
   cout<<endl;

   system("pause");
   return 0;


}

Sample Output:

Bill: 35.27
Tip: 5.2905
New Bill with tip :40.5605
Three persons share of bill with Tip: 13.5202

------------------------------------------------------------------------------------------------------------------

(b)

/*C++ program that calculate the area and perimter
of the rectangle of lenght =12.5 and widht =16.7*/
#include<iostream>
#include<math.h>
using namespace std;
int main()
{

   double lenght=12.5;
   double widht=16.7;


   double area=lenght*widht;
   double perimeter=2*(lenght+widht);


   cout<<"Lenght: "<<lenght<<endl;
   cout<<"Widht: "<<widht<<endl;
   cout<<"area: "<<area<<endl;
   cout<<"perimeter: "<<perimeter<<endl;

   system("pause");
   return 0;

}

Sample Output:

Lenght: 12.5
Widht: 16.7
area: 208.75
perimeter: 58.4

------------------------------------------------------------------------------------------------------------------

(C)

/**C++ program that prompts to enter the fahrenheit temperature
to celcius temperature and print the fahrenheit and equivalent celsius */
#include<iostream>
#include<math.h>
using namespace std;
int main()
{


   //declare variables of double
   double fahrenheit;
   double celcius;

   cout<<"Enter fahrenheit tempeature :";
   //read fahrenheit temperature
   cin>>fahrenheit;


   //convert the fahrenheit to celcius using the below formula
   celcius=(5.0/9.0)*(fahrenheit-35);

   //print fahrenheit and celcius temperature
   cout<<"Fahrenheit Tempeature :"<<fahrenheit<<endl;
   cout<<"Celcius Temperature : "<<celcius<<endl;
  
   system("pause");
   return 0;

}

Sample Output:

Enter fahrenheit tempeature :80
Fahrenheit Tempeature :80
Celcius Temperature : 25

------------------------------------------------------------------------------------------------------------------

(d)

/**C++ proram that prompts tank size, how full the tank
is in percentage and number of kilometers per liter.
Then finds if buy the pertrol at current gas statuion
or wait for next gas station is at 200kms away*/
#include<iostream>
#include<math.h>
using namespace std;
int main()
{

   //declare variables
   int tankSize;
   double percentageOfTank;
   double kmPerLitre;

   cout<<"How big is your tank, in liters?";
   cin>>tankSize;
   cout<<"How full is your tank (in percent—for example, half full = 50)?";
   cin>>percentageOfTank;
   cout<<"How many km per liter does your car get?";
   cin>>kmPerLitre;

   //calculate the number of liters in the tank
   int numLitresInTank=tankSize*(percentageOfTank/100);

   //calculate the total number of kms per liter
   int totalKms=numLitresInTank*kmPerLitre;


   //subtract the next 200 kms from the totalkms then check if it is >0
   //then wait for next station oterwise print buy here.
   if(totalKms-200>0)
       cout<<"Wait For next station"<<endl;
   else
       cout<<"Buy here."<<endl;

   system("pause");
   return 0;
}

Sample Output:

How big is your tank, in liters?200
How full is your tank (in percentùfor example, half full = 50)?50
How many km per liter does your car get?5
Wait For next station