Answer the question
In order to leave comments, you need to log in
How to implement a cyclic computing process?
I wrote a program, but it is not correct. I don't really understand cycles, I'm just learning. Can you explain what I should do next. The program was written in C++
Assignment.
Given two integers A and B (A < B). Find the sum of all integers from A to B inclusive.
#include "pch.h"
#include < iostream >
using namespace std;
int main()
{
setlocale(0, "");
int A, B;
cout << "Enter A : ";
cin >>A;
cout << "Enter B : ";
cin >> B;
int sum = 0;
for (int i = A; A <= B; i++)
{
sum = sum + i;
cout << "Sum of numbers from a to b = " << sum << endl;
Answer the question
In order to leave comments, you need to log in
As already mentioned, change the loop condition to i <= B
and
cout << "Sum of numbers from a to b = " << sum << endl;
move out of the loop
If you are learning a language, then you need a good book, not this one.
C++ is not something you can learn without a good book.
Nobody has surpassed Stephen Prat yet.
Now on the question:
in the second line you need to add.
And in the loop you find the number of elements, not their sum.sum = sum + i;
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question