Thursday, January 15, 2015

C++ Program for showing Multiplication table of size N

#include<iostream> //for input/output
#include<iomanip> //for input/output formate
using namespace std;

int main()
{
//declaring variables
int i,j, //i and j are loop control variables
N; //N is for taking input from user
//prompting and taking input from the user
cout << "Enter any Positive Integer number to get the Table: ";
cin >> N;
if( N>=1 )
{
i=1; //initializing loop variable
cout << "The desired output table is..\n"; //prompting the user for showing output
cout << "      "; //for formating output six spaces added
while( i<=N )
{
cout << setw(4) //printing numbers from 1 to the entered number
<< i;
i++;
}
cout << endl
<< setw(6) //+ symbol printed after five spaces
<< "+";
i=1; //loop variable agin initialized to 1
while( i<=N )
{
cout << "----"; //for formating Line ouput...here 4 dots are printed N times to take print in th desired formate
i++;
}
cout << endl;
i=1;
while( i<=N )
{
j=1; //intilizing nested loop control variable
cout << setw(4) //for formating output
<< i
<< " |";
while ( j<=N) //nested loop
{

cout << setw(4) //for formating output
<< i*j; //for printing the product
j++;
}
cout << endl;
i++;
}
}
else
cout << "Error: Wrong Input!" << endl;

return 0;
}

No comments:

Post a Comment