Question:21
C++ program to take input for a number and print its table
Sol:
#include <iostream> #include<conio.h> using namespace std; class table { private: int n,t; public: void read(); void show(); }; void table::read() { cout<<"Enter any no "; cin>>n; } void table::show() { int i; for(i=1;i<=10;i++) { t=n*i; cout<<n<<" * "<<i<<" = "<<t<<endl; } } int main() { table t; t.read(); t.show(); return(0); }
/* Output */ Enter any no 5 5 * 1 = 5 5 * 2 = 10 5 * 3 = 15 5 * 4 = 20 5 * 5 = 25 5 * 6 = 30 5 * 7 = 35 5 * 8 = 40 5 * 9 = 45 5 * 10 = 50
Question:22
C++ program to take input for a number and print its factorial
Sol:
#include <iostream> #include<conio.h> using namespace std; class factorial { private: int n,f; public: void read(); void cal(); void show(); }; void factorial::read() { cout<<"Enter any no "; cin>>n; } void factorial::cal() { int i; f=1; for(i=1;i<=n;i++) { f=f*i; } } void factorial::show() { cal(); cout<<"Factorial = "<<f<<endl; } int main() { factorial t; t.read(); t.show(); return(0); }
/* Output */ Enter any no 5 Factorial = 120