C++ PROBLEM:
Create a program that calculates the hyperfactorial of any positive integer n, where the hyperfactorial is equivalent to value obtained by the operation: 1^1*2^2*3^3*…..n^n . If user inputs the value that is not a positive value, display a message informing the user that the input is not valid and request a new input. Calculate the hyperfactorial first by creating a program that uses only nested “For loop” structure.
C++ code:
#include<iostream>
using namespace std;
int main(){
// Declarig a varibale to store hyperfactorial
long long int hf = 1;
// Asking the user to enter n
int n;
cout << "Enter n: ";
cin >> n;
// Checking for condition
while(n <= 0){
cout << "n should be > 0" << endl;
cout << "Enter n: ";
cin >> n;
}
// Running nested for loop to calculate hf
for(int i = 1; i <= n; ++i){
for(int j = 1; j <= i; ++j){
hf *= i;
}
}
// Displaying the hyperfactorial
cout << "Hyperfactorial of a number is " << hf << endl;
return 0;
}
OUTPUT:
Get Answers For Free
Most questions answered within 1 hours.