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 }
  • 相关阅读:
    自定义瀑布流
    传值 属性 block 单例 协议
    sqlite数据库中 保存和读取UIData对象
    SQL
    关于在Xcode控制台打印的注意点
    synthesize的作用
    iPhone屏幕尺寸/launch尺寸/icon尺寸
    关于TableView上有一段留白的解决方法
    mac显示隐藏文件
    多线程之GCD
  • 原文地址:https://www.cnblogs.com/xiaomi5320/p/4172416.html
Copyright © 2011-2022 走看看