-1

I have to create a program using a for loop that will count by 3 from the given number from the user 20 times. So far this is what i have.

#include <iostream>
using namespace std;

int main(){
  int num, newNum, add;
  cout << "Enter a number: ";
  cin >> num;

  for(int count = 0; count < 20; count++){
    for(int sum = 0; sum <= count; sum++){
      newNum = num * (count +1);
    }
    cout << num << " ";
  }
}

I have no idea where to go from here. I know that i would have to put the number given into a variable like i do with num and i know i would somehow have to store the new number in another variable like i do with newNum. Any help would be appreciated! Also this is what the output should look like in case i did not explain it good enough.

Enter a number: 3
3, 6, 9, 12, 15, 18, 21, 24, 27, 30, 33, 36, 39, 42, 45, 48, 51, 54, 57, 60.

And bonus points if you can have the last number end with a period instead of a comma :)

ddechino
  • 31
  • 6
  • Are you familiar with how for loops/etc. work? I think if so, this problem should be straightforward. Which part specifically are you confused about? – jj172 Oct 23 '19 at 03:27
  • Print a number, then a comma using `cout << num * (count + 1) << ", ";`. (Inside the loop, of course.) – Mateen Ulhaq Oct 23 '19 at 03:28
  • @jj172 i just learned about for loops. I think the part im having trouble with is adding 3 to the number and then adding 3 to that number and so on. – ddechino Oct 23 '19 at 03:31
  • @MateenUlhaq What would be output for 5 then??? – Aconcagua Oct 23 '19 at 03:35
  • `for(int count = 0; count <= 20; count++)` – this will output in total 21 numbers. Is it really task to output numbers + 20 successors or rather 20 numbers in total? – Aconcagua Oct 23 '19 at 03:39
  • About [using namespace std](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice)... – Aconcagua Oct 23 '19 at 03:46
  • @Aconcagua it would be 20 numbers total. after trying solutions other people have posted i realizes this as well so i went a head and did this ```'for(int count = 1; count < 20; count++)``` – ddechino Oct 23 '19 at 03:46
  • But now these are 19 numbers in total... *Either* `count = 1; count <= 20` *or* `count = 0; count < 20`. If you need indices into an array, prefer the latter, as you get them directly, otherwise – well, for consistency with the former, people usually prefer the latter as well – Aconcagua Oct 23 '19 at 03:56
  • Thanks to everyone who helped with this. I was wildly over complicating it in my head, i mean look at my code i had a nested loop in there for no reason! Im new to c++ but i do enjoy learning it. Again thanks for all the help! – ddechino Oct 23 '19 at 03:57
  • @Aconcagua my mistake i set ```count = 0``` and then did ```count < 20``` i mistyped in my first reply – ddechino Oct 23 '19 at 03:59

4 Answers4

2

This should work:

int main(){
   int num, currentNum, add, newNum;
   std::cout << "Enter a number: ";
   std::cin >> num;
   std::cout << num
   for(int count = 0; count < 20; count++){
         num += 3;
         std::cout << ", " << num;
   }
   std::cout << " ." << std::endl;
}

Also try to not use using namespace std. It clutters the namespace.

Amal K
  • 4,359
  • 2
  • 22
  • 44
  • 1
    You are printing in total 22 numbers, which is (at least, see my comment to question) one too much... – Aconcagua Oct 23 '19 at 03:53
  • Oh, and you woudn't want those spaces *in front of* the punctuation marks (but instead a newline after very last output). – Aconcagua Oct 23 '19 at 04:25
1

You can add 3 to the number either inside the loop's third statement (you can also have multiple conditions etc.)

for(int count = 0; count <= 20; count++, num+=3){
    // ...
}

or inside the loop's code block

for(int count = 0; count <= 20; count++){
    num += 3;
}

As for the comma you just need to add it to the output:

cout << num << ", ";

Since you want a dot at the end you need to handle this in an if-else case

if (count < 20)
    cout << num << ", ";
else
    cout << num << ".";

or a ternary operator:

cout << num << (num < 20) ? ", " : ".";

Bonus points: don't expose the complete namespace (here: std). While in such a small example it's not an issue it may lead to hard to detect bugs later on if you happen to use one or multiple namespaces that offer the same functionality.

Aconcagua
  • 24,880
  • 4
  • 34
  • 59
rbaleksandar
  • 8,713
  • 7
  • 76
  • 161
1

OK, we have so far an answer explaining pretty well and another one optimising the if inside the loop away. We still can optimise away one addition, if you count up the num variable directly:

for(auto end = num + 57; num < end; num += 3)
//                    ^ still one number less, last one follows the loop!
{
    std::cout << num << ", ";
}
std::cout << num << '.' << std::endl;
//                  ^ ^ not much difference, but just outputting a single character
//                      is more efficient, no need to iterate to find the
//                      trailing null character...

std::endl not only adds a newline, but flushes the stream immediately (i. e. the so far possibly buffered output is forced to be printed to console). This is especially useful if there's more output to follow, but possibly quite a while later (without flushing, user might see incomplete last line). Admitted, in given case not necessary, as std::cout's destructor will flush anyway, so you could go, too, with the slightly simpler std::cout << num << ".\n"...

Aconcagua
  • 24,880
  • 4
  • 34
  • 59
0

Without using the extra variables. #include using namespace std;

int main(){
   int num, currentNum, add, newNum;
   cout << "Enter a nummber: ";
   cin >> num;

  for(int count = 0; count <= 20; count++){
       cout << (num + count * 3);
       if(count == 20)cout<<".\n";
        else cout<<", ";
   }
}

More general solution would be:

#include <iostream>
using namespace std;

int main(){
   int num, currentNum, add;
   cout << "Enter a nummber: ";
   cin >> num;
    add = 3;
    currentNum = num;
   for(int count = 0; count <= 20; count++){
       cout << (currentNum); //Print current number
       currentNum = currentNum + add;  // increase the number
       if(count == 20)cout<<".\n";  //if the last one print dot
        else cout<<", ";  //else place comma
   }
}
Aykhan
  • 483
  • 5
  • 12