Foro de elhacker.net

Programación => Programación C/C++ => Mensaje iniciado por: _OLAYA_ en 4 Octubre 2015, 09:10 am



Título: Tabla de multiplicar
Publicado por: _OLAYA_ en 4 Octubre 2015, 09:10 am
(for anidados).

Escribir  un programa, que genere la tabla de multiplicar con el siguiente formato:

      1    2    3    4    5    6    7    8    9   10
 1    1    2    3    4    5    6    7    8    9   10
 2         4    6    8   10   12   14   16   18   20
 3              9   12   15   18   21   24   27   30
 4                  16   20   24   28   32   36   40
 5                       25   30   35   40   45   50
 6                            36   42   48   54   60
 7                                 49   56   63   70
 8                                      64   72   80
 9                                           81   90
10                                               100

Necesito una orientación para empezar el programa en C. No código, si no como empezarlo. Me imagino que por el enunciado seran for dentro de otros for ¿o me equivoco? Podeis ayudarme?

Gracias


Título: Re: Tabla de multiplicar
Publicado por: Seyro97 en 4 Octubre 2015, 12:45 pm
Te voy a presentar el código porque no sabría explicarlo bien. Ahora bien, te pido ENCARECIDAMENTE que entiendas el código, para así tener mi conciencia tranquila :P. Si no se entiende algo, pregunta!!

Código
  1. #include <stdio.h>
  2.  
  3. unsigned int CountDigits(unsigned int uNumber);
  4.  
  5. int main() {
  6. printf("      1    2    3    4    5    6    7    8    9   10\n\n");
  7.  
  8. for(unsigned int uRow = 1u; uRow <= 10u; uRow++) {
  9. if(CountDigits(uRow) == 1)
  10. printf("\n %u", uRow);
  11. else
  12. printf("\n%u", uRow);
  13.  
  14. for(unsigned int uCol = 1u; uCol <= 10u; uCol++) {
  15. if(uCol >= uRow) {
  16. for(unsigned int i = 0; i < 5u - CountDigits(uCol*uRow); i++)
  17. fputc(' ', stdout);
  18.  
  19. printf("%u", uRow*uCol);
  20. } else {
  21. printf("     ");
  22. }
  23. }
  24. }
  25.  
  26. fgetc(stdin);
  27. return 0;
  28. }
  29.  
  30. unsigned int CountDigits(unsigned int uNumber) {
  31. if(uNumber <= 9)
  32. return 1;
  33.  
  34. if(uNumber <= 99)
  35. return 2;
  36.  
  37. if(uNumber <= 999)
  38. return 3;
  39.  
  40. printf("No se ha podido contar el numero de cifras");
  41. return 0;
  42. }