-
C Multiplication Table
Im having trouble writing a multiplication table for C.
I have actually wrote the programme, without any user interface, however i want user interface, i want the user to be able to choose what multiplication they see.
e.g. if the put in 5 & 6 in then they will see the following:
1 2 3 4 5 6
2 4 6 8 10 12
3 6 9 12 15 18
4 8 12 16 20 24
5 10 15 20 25 30
here is the code that i have so far :
Code:
{
int number1;
int number2;
int result;
for (number1 = 1 ; number1 <= 12 ; number1++)
{
for (number2 = 1 ; number2 <= 12 ; number2++)
{
result = number1 * number2;
printf ("%4d ",result);
}
printf("\n");
}
}
and the output i get with that is a standard 12x12 table without any user interface.
-
firstly, you need to get the numbers from the user with something like
Code:
scanf("%d", &number);
secondly, your for loop doesnt do quite what you expect it to do
Code:
for (number1 = 1 ; number1 <= 12 ; number1++)
this makes number1 equal to one (overwriting any information that was stored in this variable) and goes all the way up to twelve. what you want is for it to start at one and go up to a certain number which the user inputs. so you will have to change this a little bit.
you probably need a few extra variables.
-
Ive managed to get it working
below is what i have done:
Code:
int number1;
int number2;
int result;
int first;
int second;
printf("Please enter a positive value for number 1 ");
scanf("%d", &number1);
printf("Please enter a positive value for number 2 ");
scanf("%d", &number2);
for (first = 1 ; first <= number1 ; first++)
{
for (second = 1 ; second <= number2 ; second++)
{
result = first * second;
printf ("%4d ",result);
}
printf("\n");
}
}
Thanks for your reply :)