Double, int and float. See the differences.

I don't know where I made the mistake

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <iostream>
using namespace std;

int main()

{

	int x,y;
	
	float a;
	double b;
	int c;
	
	cout<<"Birinci sayiyi giriniz:";
	cin>>x;
    cout<<"İkinci sayiyi giriniz:";
	cin>>y;

	a=x+y;
	b=x+y;
	c=x+y;

	cout<< "Float sum:" << a << endl;
	cout<< "Double sum:" << b << endl;
	cout<< "int sum:" << c << endl;
	
	return 0;
	
}



Ask :

x = 3;

y= 2.7;

write a code that get sum of these two numbers. write sum in three types of numbers double, int and float. See the differences.
Last edited on
Both x and y are integers. Their sum will be a whole number regardless of whether the variable you store it in is an integer or not.

I understand, but do not fix
The only thing I see that is wrong is you input as int instead of float or double like assignment wants? When it is correct you will probably get 5 for int, then 5.6999999->5.7000001 with float and 5.6999999->5.700000000000001 with double.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
#include <iomanip>

int main()
{
    //all inputs are with float type as if you had x and y as float. You could make them doubles also and cast that way
    int const a = 5.7f; //will implicitly cast to int which floors to 5
    float const b = 5.7f; //no need to cast float is float
    double const c = 5.7f;  //casts from float to double

    std::cout.setprecision(16);
    std::cout << "Int: " << a << std::endl;
    std::cout << "Float: " << b << std::endl;
    std::cout << "Double: " << c << std::endl;

    return 0;
}

Int: 5
Float: 5.699999809265137
Double: 5.699999809265137
Last edited on
Thank you :))
Topic archived. No new replies allowed.