Copyright © Programs ++
Design by Dzignine
Tuesday 6 March 2012

Factorial program in C++

Calculating factorial is one of the most basic programs in did when i was learning C++ in 12th. :) Its quite a simple program actually, just decrement the number and multiply it with itself. Anyways C++ rookies will find this program on calculating factorial handy, since it is a part of their lab syllabus. The program below calculates the factorial of integers as well as decimal points .

// Program to calculate the factorial of a number
#include<iostream>

int main()
{
     double number;
     double result = 1.0;
     std::cout<<" Enter the number : ";
     std::cin>>number;
     for (double i=number; i>0.0; i--)
     {
          result *= i; // translates to result = result * i;
     }
     std::cout<<"\n The factorial is : "<<result;
     return 0;
} // end of main


------ OUTPUT ------












Please do comment if you don't understand any part or want to know more or just want to say thanks. I love programming and love to teach my friends. Your suggestions and appreciation will make this blog much better.  

0 comments:

Post a Comment

Comment Here....