lundi 2 avril 2018

variable types | type casting in c++

To fix the program, you need to change at least two of the variable types: the answer, and one of the divisors.

Why

the answer, and one of the divisors

why not only answer. I tried setting int to both numerator and denominator then setting float to answer but it produces wrong output.

 /*Goal: fix the variable types problem. 
 **This program outputs the wrong answer
 **even though it compiles and executes without errors. 
 **Fix it so that it outputs the correct value.
 */

 #include <iostream>
 int main(void)
 {
     int numerator = 4;
     float denominator = 5;
     float answer = 0;

     answer = numerator / denominator;
     std::cout<<"answer = "<<answer;
     return 0;
 }

PS: The code is fixed and a quiz by Udacity classroom

Aucun commentaire:

Enregistrer un commentaire