zoukankan      html  css  js  c++  java
  • *****Exercise 4.1 Generate a multiplication table

    Recommendation:

    Exercise 4-1. Write a program that will generate a multiplication table of a size entered
    by the user. a table of size 4, for instance, would have four rows and four columns. the
    rows and columns would be labeled from 1 to 4. each cell in the table will contain the
    product of the corresponding row and column numbers, so the value in the position
    corresponding to the third row and the fourth column would contain 12.

     1 //Exercise 4.1 Generate a multiplication table */
     2 #include <stdio.h>
     3 
     4 int main(void)
     5 {
     6   int table_size = 0;
     7   int col = 0;                      // Table size        */
     8   int row = 0;
     9 
    10   printf("Enter the table size (from 2 to 12): ");
    11   scanf("%d", &table_size);
    12   //////////////////////////////////
    13   if(table_size > 12)
    14   {
    15     printf("Table size must not exceed 12 - setting to 12
    ");
    16     table_size = 12;
    17   }
    18   else if(table_size < 2)
    19   {
    20     printf("Table size must be at least 2 - setting to 2
    ");
    21     table_size = 2;
    22   }
    23 /////////////////////////////////////////////////////////////////
    24   for(row = 0 ; row <= table_size ; ++row)
    25   {
    26     printf("
    ");                      // Start new row
    27     for(col = 0 ; col<=table_size ; ++col)
    28     {
    29       if(row == 0)                     // 1st row?
    30       {                                // Yes - output column headings
    31         if(col == 0)                   // 1st column?
    32           printf("    ");              // Yes - no heading
    33         else
    34           printf("|%4d", col);         //No - output heading
    35       }
    36       else
    37       {                                // Not 1st row - output rows
    38         if(col == 0)                   // 1st column?
    39           printf("%4d", row);          // Yes - output row label
    40         else
    41           printf("|%4d", row*col);     // No - output table entry
    42       }
    43     }
    44     if(row == 0 )                      // If we just completed 1st row
    45     {                                  // output separator dashes
    46       printf("
    ");
    47       for(col = 0 ; col <= table_size ; ++col)
    48         printf("_____");
    49     }
    50   }
    51   printf("
    ");
    52     return 0;
    53 
    54 }
  • 相关阅读:
    2019春第一次课程设计实验报告
    第十二周编程总结
    第五周课程总结&试验报告(三)
    第四周课程总结&实验报告(二)
    第三周课程总结&实验报告一
    第二周JAVA学习总结
    2019春总结作业
    对我影响最大的老师
    2019第一周编程总结
    2019春第十二周作业
  • 原文地址:https://www.cnblogs.com/xiaomi5320/p/4172416.html
Copyright © 2011-2022 走看看