Pages

Thursday, April 10, 2025

Finding Factorial in C++

 #include <iostream>

using namespace std;


int main() {

    int n, i, f = 1;

    

    cout << "enter a num : ";

    cin >> n;

    

    

    if( n == 0 ) {  // n should be assigned to 0

        cout << "Factorial; " << f << endl;

    } else {

    

    for(i = 1; i <= n; i++) {

        f = f*i;


        }

        

        cout << "Factorial: " << f << endl; // result come outside the for loop, inside for loop you only see the process BTS

    }

    return 0;

}


No comments:

Post a Comment

Multi-dimensional ArrayList in Java

  // import java.util.ArrayList; import java.util. * ; // import java.util.Collections; public class Classroom {     public static voi...