Algorithm C language - Multiplication

Asked

Viewed 2,685 times

8

Translate to the language C: take a number on the keyboard and repeat the operation of multiplying it by three (printing the new value) until it is greater than 100. Ex.: if the user type 5, we should observe on the screen the following sequence: 5 15 45 135.

I don’t want answer I want a logical explanation about the exercise so I can solve it myself. I made it from here initially:

int main()
{
    int num, i, aux = 1;

    printf("digite um numero\n");
    scanf("%i", &num);

    for(i=3; i<=3; i++)
    {
        if(aux<=100)
        {
            aux=num*i;
            printf("%i\n", aux);
        }
    }
    return 0;
}
  • 3

    Eu não quero resposta eu quero uma explicação lógica sobre o exercício para eu poder resolver I gave you a positive vote on the question, I would love to give you another just for your attitude.

3 answers

4


If you want logic:

Enquanto   numero < 100 :
     numero = numero * 3;
     Imprime( numero) ;

I suggest you check if the entry is less than 0, as you should have a different treatment for it. Or change all variables that interact with num for unsigned int because by mixing both can have problems in casting and end up having unexpected values.

EDITED:

What I wrote is not quite portugol but a pseudo code. The best program that still works in Portuguese is the visualg who owns that documentation

Following the syntax of visualg:

enquanto numero <= 100 faca
   numero <- numero * 3
   escreva (numero:3)
fimenquanto
  • our thanks actually I need to go back aler portugol or do some programs in narrative on paper before you have any cool link for me see this vlw

  • 1

    our guy I went to test now the program and I was half a donkey neh euheuhe was only to keep the value of the variable in the variable itself if not it will only repeat itself and discard the previous value lol

  • 2

    I don’t recommend using unsigned to represent non-negative values in C. It’s very easy to stumble upon a conversion from Signed to unsigned and make a negative number turn into a big number. I’d rather leave everything Signed and make one if(n <= 0) to see if user input is non-positive.

  • @hugomg edited, thanks for the suggestion.

  • It is much easier to exchange all unsigneds for Signed than to carefully exchange signeds for unsigned. To tell you the truth, the only time unsigned is when you are messing with bitmasks or implementing some hash function that makes use of the unsigned’s well-defined overflow.

3

From what I understood in the statement the program will not print 135 because 135 is greater than 100. You need to change this one. In whatever you created it will only give a loop, because at the end of the first do i will already be greater than 3. And if you change the i<=3 it will multiply in one by 3 4 5 etc.

The program should give bonds while aux is less than or equal to 100, in this case I would use while. while you will print the value aux and below aux multiply it by 3 and close the while, then the program will print aux multiplied by 3, and do the same thing again until aux is greater than 100.

Edited:

for(i=0;i<=num;i++) 
{ 
if(num<=100) 
    { 
    num=num*3; 
    printf("%i\n", num); 
}

And in this case the for will still be wrong, because then it will run forever, I mean, how much i increases one per loop(loop) in a will be multiplied by 3. So one will always be greater than i. Besides is a structure "for" and in this case is not necessary, use while that is a structure while.

 while(num<=100){
     printf("%i\n", num);         
     num=num*3; 
 }

Also as I said before the printf has to be in front of num=num*3 in order to print the number typed by the user.

  • vlw mass actually yes for tah well wrong i was seeing i numca will be bigger or q own number so i can be i<=num I believe if I conceguir arrange that program I put here vlw

  • for(i=0;i<=num;i++) { if(num<=100) { num=num*3; printf("%i n", num); } }

  • here I right appears everything as you ask in the example of this exercise of the style that I typed there I just do not know why it stayed in 1 line only

1

Friend if you think programmable will be much easier to perform your task, I’ll give you an example of how to think your execution:

receba the number of the keyboard, enquanto number for menor ou igual to 100(the program calls for it to be bigger than that), multiplique for 3, se maior that 100,imprima e encerre(pause).

Thinking this way, see that it will be extremely easy to do this in C language or any other language, after that with everything ready, if there are errors, just look for the reasons for them.

Good practice.

Browser other questions tagged

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