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
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
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;
public:
void input() {
cout << "Enter a number: ";
cin >> n;
}
void calculate() {
fact = 1;
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 is used to get the number from the user, the calculate() method is used to calculate the factorial of the number, and the display() method is used to display the result. The main() function creates an object of the Factorial class and calls these methods in order.
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 Output:Factorial of 5 is: 120
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 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.
write an iterative code for factorial in c
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.