Algorithm language C - Multiplication

8
  

Translate into the C language: Take a number from the keyboard and repeat the operation by multiplying it by three (by printing the new value) until it is greater than 100. Eg if the user types 5, screen the following sequence: 5 15 45 135.

I do not want to answer I want a logical explanation about the exercise so I can solve it myself. I did this 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;
}
    
asked by anonymous 09.06.2014 / 22:33

3 answers

4

If you want logic:

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

I suggest you check if the input is less than 0, so you should have a different treatment for that. Or you can change all variables that interact with num to unsigned int because mixing them may have casting problems and end up having unexpected values.

EDITED:

What I wrote is not really Portuguese but a pseudo code. The best program that still works in portugol is the visualg that has this documentation

Following the visual syntax:

enquanto numero <= 100 faca
   numero <- numero * 3
   escreva (numero:3)
fimenquanto
    
09.06.2014 / 22:45
3

From what I understand in the statement the program will not print 135 because 135 is greater than 100. You need to change this for. In the case that you created it will only give a loop, because at the end of the first i will already be greater than 3. And if you change the i

Edited:

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

And in this case the is still going to be wrong, because it will run forever, that is, as i increases one per loop, one will be multiplied by 3. So in one always will always be greater than i. In addition it is a "to" structure and in this case it is not necessary, while it is a structure whereas.

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

In addition, as I said before, printf has to be in front of num = num * 3 in order to print the number entered by the user.

    
09.06.2014 / 23:06
1

Friend if you think programmatically it will be much easier to perform your task, I'll give you an example of how to think about its execution:

receba the number of the keyboard, enquanto number is menor ou igual to 100 (the program asks it to be greater than this), multiplique by 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 training.

    
10.06.2014 / 14:45