Tricky problem

Asked

Viewed 89 times

-7

How to make in C++ a program that prints the user’s number as follows:

5 4 3 2 1
5 4 3 2
5 4 3
5 4
5

This is too complicated for me.

  • 4

    put your code, and what number is this?

  • This number is just for example, when the user type any number, it has to go decreasing in the same way as this example

  • 2

    You think this problem of yours is complicated? Start to take a look at the questions and answers that we have here on the site that you will see much more complicated thing than yours.

1 answer

2


Try this code:

#include <iostream>

using namespace std;

void imprimeRecursivo(int numero, int final) {
  if(final <= numero) {
    for (int i = numero; i >= final; i--) {
        cout << i;
    }
    cout << "\n";
    imprimeRecursivo(numero, final + 1);
  }
}

int main()
{ 
  imprimeRecursivo(5, 1);
}

And if not with recursion:

void imprimeComLoop(int numero, int final) {
    for ( int p = final; p <= numero; p++) {
        for (int i = numero; i >= p; i--) {
            cout << i;
        }
        cout << "\n";
    }
}
  • I presented this answer without knowing the context or the real problem you want to solve..

  • But I can’t use recursion. Just using for, while or do while, that’s the problem ;/

  • But that’s exactly what has to be done

  • This sounds like algorithmic exercises in college, try to clarify the why of your questions, see how many people she has negatively. And those negative votes are because you didn’t present your code, you didn’t explain what you’re doing and for what, so it complicates things for those who are trying to figure out how to help you

  • All right, I’m sorry. It’s my first time here...

  • Come on! Don’t feel overwhelmed by anyone, the idea is to do the best for the community to grow!!

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.