Z
Z
Zhenia Bel2021-10-14 14:32:58
C++ / C#
Zhenia Bel, 2021-10-14 14:32:58

How to add - 1/3 and 1/5 on s?

I have the following code but it returns null

#include <iostream>

int main()
{
    long double a, b, ab;
    a = 1 / 3;
    b = 1 / 5;
    ab = a + b;

    printf("%f", ab);
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vasily Bannikov, 2021-10-14
@vabka

You are dividing whole numbers.
Try this

#include <iostream>

int main()
{
    double a, b, ab;
    a = 1.00 / 3.00;
    b = 1.00 / 5.00;
    ab = a + b;

    printf("%f", ab); // 0.53
}

And instead of long double - just double, because %f is for double

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question