Convert the following C code to C++ Replace all printf( ) and scanf( ) calls wit
ID: 3717704 • Letter: C
Question
Convert the following C code to C++
Replace all printf( ) and scanf( ) calls with cin( ) and cout( ) calls
Replace header files, too
#include <stdio.h>
int main(void)
{
//Declared Variables
int first = 0, second = 0, sum = 0, difference = 0, product = 0;
float quotient = 0.0;
//Step 1: Greet the user. (Tell the user the purpose of the program)
printf(" Welcome.");
printf(" This program will do calculations of two integers.");
//Step 2: Prompt user to enter an integer.
printf(" Please enter your first integer: ");
//Step 3: Read the user's input.
scanf("%d", &first );
// printf(" TEST first integer: %d ", first );
//Step 4: Prompt the user to enter another integer.
printf(" Please enter your second integer: ");
//Step 5: Read the user's second input
scanf("%d" , &second );
//printf(" TEST second integer: %d ", second);
//Step 6: Calculate the sum of the two integers.
sum = first + second ;
//printf(" TEST the sum: %d ", sum );
//Step 7: Read the Sum.
printf(" The sum of the two integers is %d" , sum );
//Step 8: Calculate Subtraction ( First integer subtracted from second
//integer)
difference = first - second;
// printf(" TEST the Difference: %d ", difference );
//Step 9: Read the subtraction
printf(" The first integer subtracted from the second integer");
printf(" produces %d", difference);
//Step 10: Calculate the product of the two integers
product = first * second;
// printf(" TEST the Product: %d ", product );
//Step 11: Read the Product
printf(" The two integers multiplied together produce %d", product);
//Step 12: Calculate the quotient of the two integers ( Second integer
//divided by the first integer)
quotient =(float) second / first ;
// printf(" TEST the quotient: %f ", quotient );
printf(" The second integer divided by the first integer");
printf(" produces %.2f", quotient);
printf(" Thank you for using the program. Goodbye! ");
return 0;
}
Explanation / Answer
Please find my answer.
#include <iostream>
using namespace std;
int main(void)
{
//Declared Variables
int first = 0, second = 0, sum = 0, difference = 0, product = 0;
float quotient = 0.0;
//Step 1: Greet the user. (Tell the user the purpose of the program)
cout<<" Welcome.";
cout<<" This program will do calculations of two integers.";
//Step 2: Prompt user to enter an integer.
cout<<" Please enter your first integer: ";
//Step 3: Read the user's input.
cin>>first;
// cout<<" TEST first integer: %d ", first );
//Step 4: Prompt the user to enter another integer.
cout<<" Please enter your second integer: ";
//Step 5: Read the user's second input
cin>>second;
//cout<<" TEST second integer: %d ", second);
//Step 6: Calculate the sum of the two integers.
sum = first + second ;
//cout<<" TEST the sum: %d ", sum );
//Step 7: Read the Sum.
cout<<" The sum of the two integers is "<<sum<<endl;;
//Step 8: Calculate Subtraction ( First integer subtracted from second
//integer)
difference = first - second;
// cout<<" TEST the Difference: %d ", difference );
//Step 9: Read the subtraction
cout<<" The first integer subtracted from the second integer";
cout<<" produces "<<difference;
//Step 10: Calculate the product of the two integers
product = first * second;
// cout<<" TEST the Product: %d ", product );
//Step 11: Read the Product
cout<<" The two integers multiplied together produce "<< product;
//Step 12: Calculate the quotient of the two integers ( Second integer
//divided by the first integer)
quotient =(float) second / first ;
// cout<<" TEST the quotient: %f ", quotient );
cout<<" The second integer divided by the first integer";
cout<<" produces "<< quotient;
cout<<" Thank you for using the program. Goodbye! ";
return 0;
}