vendredi 18 novembre 2022

how do I make a program with a break in it [closed]

Write a program that accepts three integer inputs. The first input will be the starting number, the second input will be the ending number, and the third input will serve as the max number.

Using a loop, add the numbers from the starting number until the ending number. If the running sum is greater than or equal to the max number, break out of the loop. Print the running sum every iteration until it stops.

The type of loop is of your preference. Figure out what suits best for this problem. the output should be like this: Enter·starting:·1 Enter·ending:·10 Enter·limit:·15 1 3 6 10 15

and this is my code

#include <iostream>
using namespace std;
int main(){
    int s,e,m,sum;
    cout << "Enter starting: ";
    cin >> s;
    cout << "Enter ending: ";
    cin >> e;
    cout << "Enter limit: ";
    cin >> m;
       cout << s<<endl;
       int b=s;
for (int a=1;a<e;a++){
      int sum=b+s;
      sum+=a;
      if(sum>=e){
            break;
      }
      cout << sum << endl;
      b=sum;
}}

and the output i get is: Enter starting: 1 Enter ending: 10 Enter limit: 15 1 3 6

Aucun commentaire:

Enregistrer un commentaire