0

Write a program that changes a temperature reading from Fahrenheit to Celsius using the following formula : )

Celsius = (5/9) * (Fahrenheit - 32) The program should prompt the user to enter a Fahrenheit temperature. output should be like these : Enter the temperature in Fahrenheit : 56 Fahrenheit temperature : 56.000000 Celsius temperature is : 13.333333 Inc++ language

10th Oct 2017, 4:14 PM
Nel John
Nel John - avatar
14 Réponses
+ 17
#include <iostream> using namespace std; int main() { int num,temp,sum; cout<< " Enter a positive integer: "; cin>> num ; // E.g 10 temp = num ; // temp = 10 sum = 0; do { // % give you what's left over after division // // pass 1: sum = 0 + 10 % 10 = 0 + 0 = 0 // pass 2: sum = 0 + 1 % 10 = 0 + 1 = 1 sum = sum + num % 10 ; // pass 1: num = 10 / 10 = 1 // pass 2: num = 1 / 10 = 0 num = num / 10; } while (num > 0 ); cout << " The sum of the digits = " << sum << endl; // 0 // if (1 % 3 == 0) // if (1 == 0) if(sum % 3 == 0) cout<< temp << " is divisible by 3" <<endl; else cout<< temp << " is not divisible by 3 "<<endl; // 10 is not divisible by 3 // if (1 % 9 == 0) // if (1 == 0) if ( sum % 9 == 0 ) cout<< temp << " is divisible by 9"<<endl; else cout<< temp << " is not divisible by 9"<<endl; // 10 is not divisible by 9 }
10th Oct 2017, 5:34 PM
Babak
Babak - avatar
+ 15
Its precedence? The way it works? If so study this tutorial to learn about modulus operator. [https://www.cprogramming.com/tutorial/modulus.html]
10th Oct 2017, 5:15 PM
Babak
Babak - avatar
+ 14
#include <iostream> #include <iomanip> using namespace std; int main () { double fahrenheit = 0; double celsius = 0; cout << "Enter a Fahrenheit temperature: "; cin >> fahrenheit; cout << fixed << setprecision(5); cout << "Fahrenheit temperature : " << fahrenheit << endl; celsius = (5.0/9.0) * (fahrenheit - 32.0); cout << "Celsius temperature: " << celsius; }
10th Oct 2017, 4:25 PM
Babak
Babak - avatar
+ 14
For setting the output stream precision to your desired one. #include <iomanip> // std::fixed and std::setprecision is necessary for achieving that effect.
10th Oct 2017, 4:32 PM
Babak
Babak - avatar
+ 14
num is undefined, my friend!
10th Oct 2017, 5:04 PM
Babak
Babak - avatar
+ 14
What do you exactly want to know about that?
10th Oct 2017, 5:05 PM
Babak
Babak - avatar
+ 2
Ah! 😫 Don't ask your homework here. Try to do it yourself. You can ask about doubt but not the whole program
10th Oct 2017, 5:44 PM
Kartikey Sahu
Kartikey Sahu - avatar
+ 1
thank you sir .hehehs
10th Oct 2017, 4:29 PM
Nel John
Nel John - avatar
0
sir can I ask what's the use of <iomanip
10th Oct 2017, 4:29 PM
Nel John
Nel John - avatar
0
ok I understand a little :D
10th Oct 2017, 4:36 PM
Nel John
Nel John - avatar
0
sir by the way ..can I ask about the operator modulus ?
10th Oct 2017, 4:39 PM
Nel John
Nel John - avatar
0
sir how this formula works ? sum=0; do { sum = sum + num % 10 ; num = num / 10; }while (num>0);
10th Oct 2017, 4:41 PM
Nel John
Nel John - avatar
0
ahm I'll right the code ..I really want to understand .
10th Oct 2017, 5:08 PM
Nel John
Nel John - avatar
0
//program : Divisibility test by 3 and 9 #include <iostream> using namespace std; int main() { int num,temp,sum; cout<< " Enter a positive integer: "; cin>> num ; cout<<endl; temp= num ; sum = 0; do { sum = sum + num % 10 ; num = num/10; }while (num > 0 ) cout<< " The sum of the digits = " <<sum<<endl; if(sum % 3 == 0) cout<< temp << " is divisible by 3" <<endl; else cout<<temp<< " is not divisible by 3 "<<endl; if ( sum % 9 == 0 ) cout<< temp << " is divisible by 9"<<endl; else cout<<temp<," is not divisible by 9"<<endl; }
10th Oct 2017, 5:18 PM
Nel John
Nel John - avatar