Factorial – C++

This code is for C++,

For Factorial you can use one of 3 functions below, they do same thing, but first 2 are recursive (2nd one is simplified version of 1st to make it easier to understand) and 3rd one is iterative.

I used “long long”, you can change it to int, double, and float. Simply just replace all “long long” s in the codes with any data type that you like, but don’t forget that each one has its own limitation. In the list below you can see the largest number that you can use with each data type:

int:
   12! = 479001600

long long:
   20! = 2432902008176640000

float:
   34! = 2.95233e+38

double:
   170! = 7.25742e+306

long double:
   1754! = 1.97926e+4930

For these codes you don’t need to include any header file.

// 1st code, It's Recursive 
long long factorial_1st(long long n) {
    return n >= 1 ? n * factorial_1st( n - 1 ) : 1;
}
 
// 2nd code, It's Recursive 
long long factorial_2nd(long long n) {
    if ( n > 1 ){
		return n * factorial_2nd( n - 1 );
	}
    return 1;
}
 
// 3rd code, It's Iterative 
long long factorial_3rd(long long n) {
    long long answer = 1;
    int i;
    for( i = 1; i <= n; i++ ) {
        answer *= i;
    }
    return answer;
}

Free Advertising

Posted: April 4th, 2009
at 2:17pm by Nodehi Sama

Tagged with , , , ,


Categories: C++

Comments: 1 comment


Cut-off Decimal Places (Float, Double) – C++

This code is for C++,

Sometimes you need to cut-off some decimal places (the number of digits following the point) in float and double numbers. For example you are making a computer-based exam for elementary school and you want to ask them to calculate a division till 2 decimal places. To compare the students’ answers with computer’s answers, you need to keep only 2 digits after the decimal point and omit the rest.

In C++, float data type’s size is +/- 3.4e +/- 38; it means it can be as small as (3.4 times (10 to the power of -38)) and double data type’s size is +/- 1.7e +/- 308. There are some functions in “math.h” header to set the size of decimal places, but they won’t cut the digits, they will round them up (ceil) or round them down (floor).

To use the code to cut the digits you need to include “math.h” like this:

#include <math.h>

The code below will cut the unnecessary digits and keep the number of them that you need:

float cutDecimal( float myNumber, int decimal){
 
    myNumber = myNumber*( pow(10,decimal) );
    myNumber = (int)myNumber;
    myNumber = myNumber/( pow(10,decimal) ) ;
 
    return myNumber;
}

In the function “int decimal” is the number of the digits that you want to keep after the decimal point.

To use the function for double numbers, just replace all “float” s with “double”.

Posted: March 30th, 2009
at 11:36pm by Nodehi Sama

Tagged with , , ,


Categories: C++

Comments: No comments


Check for Prime Numbers – C++

This code is for C++,

To find that a given integer is a prime number or not you can use the function below, but you need to include “math.h” first:

#include <math.h>

This is the function that you need:

bool isPrime(int num){
    bool answer = true;
    for (int i = 2; i <= sqrt ( num ) ; i++ ) {
       if ( num % i == 0 ){
            answer = false ;
            break;
       }
    }
    return answer;
}

This function will return a Boolean value. If the given integer was prime, it will return true and it will return false if it wasn’t prime.


Posted: March 29th, 2009
at 3:48am by Nodehi Sama

Tagged with


Categories: C++

Comments: No comments