#include <stdio.h>
int main() {
      int num, i = 1;
      printf("\n Enter any Number:");
      scanf("%d", &num);
      printf("Multiplication table of %d: \n", num);
      while (i <= 10) {
            printf("\n %d x %d = %d", num, i, num * i);
            i++;
      }
      return 0;
}

Hi.. Can someone please patiently explain me this program, my doubt is why it is assigned i=1 and why it has given i<=10 and why i++ is given after printf statement..

Please explain me the full program, it would be really helpful for me..
Many thanks..

Edited 3 Years Ago by Dani: Formatting fixed

>>why it is assigned i=1

That's called initializing a variable to some known value. The value 1 was arbitrary, would have been any number. The important point is that the value is something that the author knows. Programmers would normally initialize variables to 0.

>>why it has given i<=10

So that the loop can count from 1 to (and including) 10. Had the value of i been initialized to 0 instead of 1, then the i<=10 would have been i<10. In either case the loop will execute 10 times.

>>why i++ is given after printf statement.
That increments the value of i by 1.

Another way that loop could have been written is like this

for(i = 1; i <= 10; i++)
{
    printf("\n %d x %d = %d", num, i, num * i);
}

When learning to program a good way to help you understand the code is to compile and test it yourself. don't be afraid to change the code a little to find out what happens, such as change i<=10 to i<10 and to change i=1 to i=15 (if you changed i=15 the loop would not run at all because the value of i is already greater than 10 )

Edited 4 Years Ago by Ancient Dragon: n/a

int main() // your program will be executed line by line after int main()

printf("\n Enter any Number:"); // printf means show me in screen what i wrote between ""

symbol \n means -> change line

scanf("%d", &num); // scanf used to set something in a variable
%d means system is waitting an integer,
&num is where the thing that you will insert from keyboar will be put in variable named num (example if you press 5 and enter the variable num will become 5)

printf("Multiplication table of %d: \n", num); // As we said it will show us what is in "" and it will show us in screen:

--> Multiplication table of %d

BUT %d WILL BE REPLACEd BY THE thing THAT YOU ENTERED AT SCANF (If you added 5 and then clicked enter the message will be: )

--> Multiplication table of 5

Then

while (i <= 10) { // while variable i < or = 10 do what is between brackets
printf("\n %d x %d = %d", num, i, num * i);
i++; // this means i+1
}


so it check if i(which is 1 as is set at the begin) is <= 10
printf -> show at screen -> 5(num of var num) x 1(num of var i) = 5 (num * i)
i++ background sets i+1 so i becomes 2

then it checks if i(which is now 2) is <= 10
printf -> show at screen ---> 5 x 2 = 10
i++ background sets i+1 so i becomes 2

the shame again and again

when i becomes 9 it is <10 and when i is 10 it is = 10 so it will keep doing

then when i become 11 it will end

so the one that wrote the code wanted to show the first 10 multiplications of the number that will be given by the user

in our example it will show us in screen:


1 x 5 = 5
2 x 5 = 10
3 x 5 = 15
4 x 5 = 20
5 x 5 = 25
6 x 5 = 30
7 x 5 = 35
8 x 5 = 40
9 x 5 = 45
10 x 5 = 50

I hope my post is useful and well written.

Edited 4 Years Ago by JilMakias: n/a

This article has been dead for over six months. Start a new discussion instead.