Printing table using While Loop in C++
This is a C++ program that prompts the user to enter a limit and a number, and then prints the multiplication table of that number up to the specified limit using a while loop. Here's how the program works:
- It declares four integer variables i, n, m, and a to store the loop counter, the limit, a placeholder, and the number for which the multiplication table is generated, respectively. The initial value of i is set to 1.
- It prompts the user to enter the limit by printing the message "Enter the limit:" to the console using the cout object, and then reads the input from the user into the variable n using the cin object.
- It prompts the user to enter the number for which the multiplication table is generated by printing the message "Enter the table's number:" to the console using the cout object, and then reads the input from the user into the variable a using the cin object.
- It uses a while loop to print the multiplication table of the number a up to the limit n. The loop condition i<=n ensures that the loop continues as long as i is less than or equal to n.
- Inside the loop, the current value of i is printed to the console, followed by the multiplication sign *, the value of a, the equal sign =, and the product of i and a.
- The loop counter i is then incremented by 1 using the postfix increment operator i++.
- The loop continues until the condition i<=n is no longer true.
- The program then exits by returning 0 to the operating system using the return statement.
Source Code
#include<iostream>
using namespace std;
int main()
{
int i=1,n,m,a;
cout<<"\nEnter the limit:";
cin>>n;
cout<<"\nEnter the table's number:";
cin>>a;
while(i<=n)
{
cout<<"\n"<<i<<"*"<<a<<"="<<i*a;
i++;
}
return 0;
}
To download raw file
Click Here
Output
Enter the limit:9
Enter the table's number:9
1*9=9
2*9=18
3*9=27
4*9=36
5*9=45
6*9=54
7*9=63
8*9=72
9*9=81