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

3917. Count Indices With Opposite Parity (Brute Force) O(n2) + Optimized Solution O(n) + tips LEETCODE WEEKLY 500

  class Solution {     public int [] countOppositeParity ( int [] nums ) {          // approach 1 - checks every pair         int n = n...