D
D
Danil Fliginskiy2020-05-02 12:27:02
C++ / C#
Danil Fliginskiy, 2020-05-02 12:27:02

Why doesn't addition work?

#include <iostream>
#include "locale.h"
using namespace std;

int main()
{
    setlocale(LC_ALL, "Russian");
    system("chcp 1251");

    float n, total = 0;

    cout << "Программа для вычисления числового ряда" << endl;
    cout << "Введите число слагаемых в числовом ряде: ";
    cin >> n;

    for (int i = 1; i <= n; i++) {
        total += (1 / 4);
        cout << endl << total << endl;
    }
    cout << "Сумма числового ряда из " << n << " слагаемых = " << total;

    return 0;
}


I don't understand why the addition doesn't work. If you add one, then it works. If you add 0.25, then everything works, but if you add 1/4, then it does not work. Not to mention the formula 1 / (i * 3)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander Ananiev, 2020-05-02
@danilfliginskiy

Because 1/4 is an integer division. Need 1.0/4

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question