Note that there are some explanatory texts on larger screens.

plurals
  1. PONewbie help (in C) details inside
    primarykey
    data
    text
    <p>first of all I am a complete programming newbie, so I am sorry if there is something that does this really easily and I don't know of it, it is in no way my intention to be dumb. [Also, sorry the programs are in spanish but I don't think the printf's really affect the whole logic thing.</p> <p>Anyhow: I made a program that evaluated a number and then got it as near as possible to the closest multiple of three, then I did the same for 5:</p> <pre><code>//cambia un numero al multiplo de 3 mas cercano #include &lt;stdio.h&gt; #include &lt;stdlib.h&gt; int main(){ int a; printf("Escribe el numero que deseas redondear: "); scanf(" %d", &amp;a); while(a%3 != 0){ --a; if (a%3 == 0) break; a++; a++; } printf("El multiplo de 3 mas cercano es: %d\n", a); } //cambia un numero al multiplo de 5 mas cercano #include &lt;stdio.h&gt; #include &lt;stdlib.h&gt; int main(){ int a; printf("Escribe el numero que deseas redondear: "); scanf(" %d", &amp;a); while(a%5 != 0){ --a; if (a%5 == 0) break; --a; if (a%5 == 0) break; a++; a++; a++; if (a%5 == 0) break; a++; if (a%5 == 0) break; if (a%5 == 0) break; a++; } printf("El multiplo de 5 mas cercano es: %d\n", a); } </code></pre> <hr> <p>Now I want to write a program that does the same, but takes the number of the multiple from the user. I wrote this:</p> <pre><code>//cambia un numero al multiplo de n (input) más cercano #include &lt;stdio.h&gt; #include &lt;stdlib.h&gt; int main(){ int a, n; printf("Escribe el numero que deseas redondear: "); scanf(" %d", &amp;a); printf("Escribe el numero base para redondear: "); scanf(" %d", &amp;n); for(a, n; a%n != 0; a++); printf("El multiplo de %d mas cercano (A la derecha) es: %d\n",n, a); } </code></pre> <p>But of course this only makes it as near as possible from the right, and I don't know how (With n cases) to make it go to the closest number. The idea is this:</p> <p>5 6 7 8 9 10 If we want it to go to the closest multiple of 5 and the input is 6-7 then it goes to 5, because that is the closest multiple of 5. If the input is 8-9 then it sends it to 10. The idea is always getting to the "closest" number. But I don't really see how to do it. Any help would be greatly appreciated. </p> <p>If you could alternatively post the help in Java that would be more than sufficient as well. Since I know a bit java as well.</p> <p>Thanks!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload