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
14 Antworten
+ 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
}
+ 15
Its precedence? The way it works?
If so study this tutorial to learn about modulus operator.
[https://www.cprogramming.com/tutorial/modulus.html]
+ 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;
}
+ 14
For setting the output stream precision to your desired one.
#include <iomanip> // std::fixed and std::setprecision is necessary for achieving that effect.
+ 14
num is undefined, my friend!
+ 14
What do you exactly want to know about that?
+ 2
Ah! 😫 Don't ask your homework here. Try to do it yourself. You can ask about doubt but not the whole program
+ 1
thank you sir .hehehs
0
sir can I ask what's the use of <iomanip
0
ok I understand a little :D
0
sir by the way ..can I ask about the operator modulus ?
0
sir how this formula works ?
sum=0;
do {
sum = sum + num % 10 ;
num = num / 10;
}while (num>0);
0
ahm I'll right the code ..I really want to understand .
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;
}