In: Computer Science
1. Write a C++ code segment to read 50 temperature values in Fahrenheit and to convert them to Celsius. You convert a Fahrenheit temperature to Celsius by using the following formula: Celsius = 5.0 / 9 * (Fahrenheit - 32).
2.A client has purchased 20 products in a store. Write a C++ code segment to read the unit price and the number of items of each product and to compute and print the total price of all these products.
#include <iostream>
using namespace std;
int main()
{
int n = 5;//reading 5 temparatures, if you want to read 50 then
change this 5 as 50.
double f[n];
for(int i=0; i<n; i++){
cout<<"Enter the temperature value in Fahrenheit: ";
cin >> f[i];
}
cout<<"Fahrenheit\tCelsius"<<endl;
for(int i=0; i<n; i++){
double c = 5.0 /( 9 * (f[i] - 32));
cout<<f[i]<<"\t"<<c<<endl;
}
return 0;
}
Output:
sh-4.2$ g++ -o main *.cpp
sh-4.2$ main
Enter the temperature value in Fahrenheit: 50
Enter the temperature value in Fahrenheit: 60
Enter the temperature value in Fahrenheit: 70
Enter the temperature value in Fahrenheit: 80
Enter the temperature value in Fahrenheit: 90
Fahrenheit Celsius
50 0.0308642
60 0.0198413
70 0.0146199
80 0.0115741
90 0.00957854
Question 2:
#include <iostream>
using namespace std;
int main()
{
int n = 5;//reading 5 products, if you want to read 20 then change
this 5 as 20.
double price[n];
int numOfItems[n];
for(int i=0; i<n; i++){
cout<<"Enter theproduct "<<(i+1)<<" unit price:
";
cin >> price[i];
cout<<"Enter the number of items: ";
cin>> numOfItems[i];
}
cout<<"Product\tUnit Price\tItems\tTotal
Price"<<endl;
for(int i=0; i<n; i++){
cout<<(i+1)<<"\t"<<price[i]<<"\t"<<numOfItems[i]<<"\t"<<numOfItems[i]*price[i]<<endl;
}
return 0;
}
Output:
sh-4.2$ g++ -o main *.cpp
sh-4.2$ main
Enter theproduct 1 unit price: 2
Enter the number of items: 2
Enter theproduct 2 unit price: 3.3
Enter the number of items: 3
Enter theproduct 3 unit price: 4.4
Enter the number of items: 4
Enter theproduct 4 unit price: 5.5
Enter the number of items: 5
Enter theproduct 5 unit price: 6.6
Enter the number of items: 6
Product Unit Price Items Total Price
1 2 2 4
2 3.3 3 9.9
3 4.4 4 17.6
4 5.5 5 27.5
5 6.6 6 39.6