Write a C++ Program to find Factorial of a number using classinputEnter a number: 5outputBelow is the input and output of the provided C++ program to find the factorial of a number using a class:Input:Enter a number: 5 Output:Factorial of 5 is: 120
Question
Write a C++ Program to find Factorial of a number using classinputEnter a number: 5outputBelow is the input and output of the provided C++ program to find the factorial of a number using a class:Input:Enter a number: 5 Output:Factorial of 5 is: 120
Solution
Sure, here is a simple C++ program that calculates the factorial of a number using a class:
#include<iostream>
using namespace std;
class Factorial {
int n, fact = 1;
public:
void input() {
cout << "Enter a number: ";
cin >> n;
}
void calculate() {
for(int i = 1; i <= n; i++) {
fact = fact * i;
}
}
void display() {
cout << "Factorial of " << n << " is: " << fact << endl;
}
};
int main() {
Factorial obj;
obj.input();
obj.calculate();
obj.display();
return 0;
}
In this program, we have a class named Factorial with three methods: input(), calculate(), and display(). The input() method takes the number as input from the user, the calculate() method calculates the factorial of the number, and the display() method displays the factorial of the number.
Similar Questions
Write a C++ Program to find Factorial of a number using classinputEnter a number: 5outputBelow is the input and output of the provided C++ program to find the factorial of a number using a class:Input:Enter a number: 5
Write a C++ program to find the factorial of a given integer using recursion.sample input and outputEnter a positive integer: 5 Factorial of 5 is: 120
c++ program on factorial of a number in short
Write a program to calculate the factorial of N.
Write a C program, using functions where the function calculates the factorial of a given integer, and the main() program does I/O and calling of factorial function.
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.