C++ LOOP EXAMPLE FOR Beginners. C++ PROGRAM TO FIND TABLE OF A NUMBER USING FOR -LOOP.
THIS IS FOR BEGGINERS, WHO ARE LEARNING C++.
THERE ARE THREE TYPES OF LOOP IN C++ - FOR LOOP, WHILE LOOP, DO-WHILE LOOP.
SO, FOR THIS TABLE FINDER PROGRAM WE ARE GOING TO USE FOR LOOP I N THIS.
WHY FOR LOOP ?
BECAUSE IT IS SIMPLE AND EASY TO UNDERSTAND.
FOR LOOP SYNTAX -
for (int i = 0; i < count; i++)
{
/* code */
}
NOW LETS START OUR PROGRAM.
START WITH-
#include <iostream>
using namespace std;
int main()
{
LETS MAKE SOME VARIABLES AND ASSIGN THIER VALUES-
int ui, mb, aw, gb;
mb = 1;
gb = 0;
NOW TAKE THE NUMBER FROM USER-
cout << "enter your number : ";
cin >> ui;
FINALLY RUNNING THE LOOP-
for (int a = 0; a < 10; a++)
{
aw = ui * mb;
gb++;
cout << ui << " * " << gb << " = " << aw << endl;
mb++;
}
EXPLANATION OF ABOVE FOR LOOP -
* CREATE A LOOP AND MAKE A int a WITH VALUE OF 0.
* A CONDITION TO RUN PROGRAM FOR 10 TIMES.
* a++ TO ADD 1 WHENEVER THE LOOP RUNS.
* aw = ui * mb HERE AW IS HOLD THE VALUE OF UI * MB.
* gb++ TO ADD 1 TO GB EACH TIME THE LOOP RUNS.
* THEN PRINTING THE RESULT.
* mb++ OFCOURSE TO ADD 1 TO MB.
ENDING THE PROGRAM-
return 0;
}
I HOPE YOU HAVE UNDERSTAND IT.
FULL SOURCE CODE-
#include <iostream>
using namespace std;
int main()
{
int ui, mb, aw, gb;
mb = 1;
gb = 0;
cout << "enter your number : ";
cin >> ui;
for (int a = 0; a < 10; a++)
{
aw = ui * mb;
gb++;
cout << ui << " * " << gb << " = " << aw << endl;
mb++;
}
return 0;
}