Basic Tables

I tried to make a basic table programme. Need improvement?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <iostream>
#include <conio.h>
using namespace std;

int main()
{
    int table, count, ans;
    cout << "Please Enter the Number of Table: ";
    cin >> table;
    count = 1;
    if (table == 1 || table == 2 || table == 3 || table == 4 || table == 5 || table == 6 || table == 7
        || table == 8 || table == 9 || table == 10 || table == 11 || table == 12 || table == 13
        || table == 14 || table == 15 || table == 16|| table == 17
        || table == 18|| table == 19 || table == 20 )
    {
        cout << " \n Table of: " << table << endl << endl;

    while (count <= 10)
    {
       ans = table * count;
       cout <<" " << count << "\t" << "x  " << table << "  = " <<ans << endl << endl;

        ++count;
    }

     }
    else
    {
       cout << "\nInvalid Entry.\nPlease Enter Table Number 1 to 20." << endl;
    }
     getch();

}
Last edited on
if (table == 1 || table == 2 ... )

=> if (table>=1 && table <=20)

while(count ...

=>

1
2
3
4
5
6
    
for (int count=1; count<=10; count++) 
{
    int ans = table*count;
    cout <<" " << count << "\t" << "x  " << table << "  = " <<ans << endl << endl;
}


int table, count, ans;

=> int table;
(it is better to introduce variables before you use them, instead of at the beginning of a function)
Last edited on
Try this...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
#include <conio.h>
using namespace std;
int main()
{
    int table, count, ans;
    cout << "Please Enter the Number of Table: ";
    cin >> table;
    for (count=1; count<=10; count++)
	{
    ans = table*count;
    cout <<" " << count << "\t" << "x  " << table << "  = " <<ans << endl << endl;
	}
     getch();
}
Thanks
Topic archived. No new replies allowed.