Create a program that reads a number between 2 and 20 and generate a screen with the configuration shown below:

Asked

Viewed 32 times

-4

the program must display the following result:

Type a random number between 2 and 20:

7

Exit from the program:

1234567

x123456

xx12345

xxx1235

xxxx123

xxxxx12

xxxxxx1

(NOTE: the value typed by the user has to be between 2 and 20, 7 was typed as an example of what is requested)

#include <stdio.h>
#include <stdlib.h>

void main()
{
    int n, c;

    printf("digite um número entre 2 e 20: ");
    scanf("%d", &n);

    for (size_t i = 1; i <= n; i++)
    {
        //c++;
        for (c = 0; c <=n; c++)
            printf("x");
        printf("%d \n", i);    
    }
    printf("\n");
}
  • Please clarify your problem or provide additional details in order to highlight exactly what you need. The way it’s written these days it’s hard to tell exactly what you’re asking.

  • If the entrance can be 1234567, why do you have a message that the number should be between 2 and 20?

  • Sorry for the lack of information, this program should receive 1 value of no more than 2 to 20. The value that the user enter a number between 2 and 20. has to be in that range. And then the program must display the predecessor numbers of the number typed up to the number typed. and these values shall be printed as the above example

  • the numbers 1 - 7 are an example of what should happen if the user type 7. The same thing has to happen if the user type 8, or 12 for example

  • Simplifying the program should display these output: ----- Type a number: 7--------- Program output: 1234567 x123456 xx12345 xxx1235 xxxx123 xxxxx12 xxxxxx1

  • this helps @hkotsubo ?

  • and @Gean Santos ?

  • I suggest [Edit] the question and put that information there. It is better because in the body of the question you can format, besides having more space for you to elaborate better

  • https://ideone.com/vMrnYy

  • Vlw @hkotsubo vc is a genius

  • I thank everyone who helped, people you are of wonderful

Show 6 more comments

1 answer

0

Running the program below and typing the number 1234567, the program will print on the screen what is asked in the question title.

#include <stdio.h>  // printf
#include <math.h>   // log10

#define len(n) log10(n)+1

int main() {
    int n;
    printf("digite um número: ");
    scanf("%d", &n);
    int expected_size = len(n);
    int current_size;

    for (; n; n/=10) {
        current_size = len(n);
        for (int i = 0; i < expected_size-current_size; ++i)
            printf("x");
        printf("%d ", n);
    }
    printf("\n");

    return 0;
}

Browser other questions tagged

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