In: Computer Science
Write a c++ function named multi_table to print out multiplication table for a given number of rows and columns. Your program should print a header row and a header column to represent each row and column number. For example your function call multi_table(4, 4) would print below to the command line:
1 | 1 2 3 4
_________
2 | 2 4 6 8
3 | 3 6 9 12
4 | 4 8 12 16
Test your function in a driver program for 12 by 12 multiplication table.
code:
#include <iostream>
using namespace std;
int multi_table(int n,int m){
//passing parameters to multi_table function
int i,j,k;
for(i=1;i<=n;i++){ //loop for iterating through rows
cout<<i<<"|";
for(j=1;j<=m;j++){ //loop for printing columns
cout<<i*j<<" ";
}
cout<<"\n";
if(i==1){ //printing extra character "-" in column
for(k=1;k<n*3;k++){
cout<<"-";
}
cout<<"\n";
}
}
}
int main()
{
int n,m; //declaring variables n and m
cout<<"Enter no of rows:";
cin>>n; //taking no of rows from user
cout<<"Enter no of columns:";
cin>>m;
multi_table(n,m); //calling multi_table function
}
output:
output2:
code screenshot: