Showing posts with label MathProblems. Show all posts
Showing posts with label MathProblems. Show all posts

Monday, May 1, 2017

Calculate the factorial in C++ || C++ Sample Programs

Leave a Comment
Question::

Write a program that asks the user to enter an integer number. The main() program will pass this number to a function called “ fact()”. This function will return the factorial of the input value. Show the calculated factorial of the given number on the screen.

Code:

/**************************************************|
/*************C++ Programs And Projects************|
***************************************************/
#include<iostream>
using namespace std;

int fact(int); // prototype of a function

void main() {
       int number;
       cout << "Enter a Number to find its factorial == ";
       cin >> number;
       cout<<"Factorial of "<<number<<"is == "<<fact(number);
}


//Body of a function

//Logic of factorial is pretty simple that is if we have a number like 5 then the formula of factorial
//will be like this 5*4*3*2*1
int fact(int myFactorial) {
       int result = 1;
       for (int i = 1; i <=myFactorial; i++)
       {
              if (result == 1) {
                     result = result*i;
              }
              else
              {
                     result = result*i;
              }
       }
       return result;
}




Related Articles:

C++ Primer Plus Sixth Edition Complete Solution Manual 

C++ Books Solution

  

Read More

Tuesday, April 26, 2016

Euler numbers in c++ -- How to

Leave a Comment
Explanation:
Below mention code is compiled in Visual Studio 2015 and Code Blocks 13.12,output snap is attached.. If any problem you feel and you want some explanation feel free to contact us.


e (Euler's Number)

The number e is a famous irrational number, and is one of the most important numbers in mathematics.
The first few digits are:

2.7182818284590452353602874713527 (and more ...)
It is often called Euler's number after Leonhard Euler.
And Euler is spoken like "Oiler".
e is the base of the Natural Logarithms (invented by John Napier).
e is found in many interesting areas, so it is worth learning about.

Calculating

There are many ways of calculating the value of e, but none of them ever give an exact answer, because e is irrational (not the ratio of two integers).
But it is known to over 1 trillion digits of accuracy!
For example, the value of (1 + 1/n)n approaches e as n gets bigger and bigger:
graph of (1+1/n)^n
n(1 + 1/n)n
12.00000
22.25000
52.48832
102.59374
1002.70481
1,0002.71692
10,0002.71815
100,0002.71827

Code:

/**************************************************|
/*************C++ Programs And Projects************|
***************************************************/
#include<iostream>
#include<math.h>
using namespace std;
void main() {
       int Number = 0;
       cout << "Enter Ending Point for Euler Numbers = ";
       cin >> Number;
       for (float i = 1; i <= Number; i++) {
              //(1 + 1 / n)n
              float tempResult = ((1 / i) + 1);
              float result = pow(tempResult, i);
              cout << i << " == " << result << endl;
       }

}

Output:


Read More