C++ programs to print multiplication table using for-loop, recursion and class.
C++ program to print multiplication table using for-loop
#include<iostream>
using namespace std;
int main()
{
int i,n;
cout<<"Enter the no.";
cin>>n ;
for(i=1;i<=10;i++)
cout<<"\n "<<n<<" * "<<i<<" = "<<n*i;
return 0;
}
using namespace std;
int main()
{
int i,n;
cout<<"Enter the no.";
cin>>n ;
for(i=1;i<=10;i++)
cout<<"\n "<<n<<" * "<<i<<" = "<<n*i;
return 0;
}
C++ program to print multiplication table using recursion
#include<iostream>
#include<iomanip>
using namespace std;
void table(int n, int i)
{
if(i==11)
return;
cout<<"\n "<<n<<" * "<<setw(3)<<i<<" = "<<setw(3)<<n*i;
table(n,i+1);
}
int main()
{
int i,n;
cout<<"Enter the no. : ";
cin>>n;
table(n,1);
return 0;
}
#include<iomanip>
using namespace std;
void table(int n, int i)
{
if(i==11)
return;
cout<<"\n "<<n<<" * "<<setw(3)<<i<<" = "<<setw(3)<<n*i;
table(n,i+1);
}
int main()
{
int i,n;
cout<<"Enter the no. : ";
cin>>n;
table(n,1);
return 0;
}
C++ program to print multiplication table using class
#include<iostream>
#include<iomanip>
using namespace std;
class print
{
private:
int n;
public:
void getnum()
{
cout<<"Enter the no.";
cin>>n ;
}
void table();
};
void print::table()
{
for(int i=1;i<=10;i++)
cout<<"\n "<<n<<" * "<<setw(3)<<i<<" = "<<setw(3)<<n*i;
}
int main()
{
print obj;
obj.getnum();
obj.table();
return 0;
}
#include<iomanip>
using namespace std;
class print
{
private:
int n;
public:
void getnum()
{
cout<<"Enter the no.";
cin>>n ;
}
void table();
};
void print::table()
{
for(int i=1;i<=10;i++)
cout<<"\n "<<n<<" * "<<setw(3)<<i<<" = "<<setw(3)<<n*i;
}
int main()
{
print obj;
obj.getnum();
obj.table();
return 0;
}
No comments:
Post a Comment