To write a C++ program to find the factorial of a number

In summary, a C++ program can be written to find the factorial of a number. This involves using a loop to multiply the number by all of its preceding numbers until 1 is reached. The program can take in the number as user input and output the factorial result. It is important to handle edge cases, such as negative numbers or non-integer inputs, in order to ensure accurate results.
  • #1
Urmi Roy
753
1

Homework Statement



To write a C++ program to find the factorial of the number input 'a'.

Homework Equations



the factorial evaluated is denoted as 'f'
i is the counter

The Attempt at a Solution



Pls check if my program is correct...(here f is the value of factorial and a is the number whose factorial is to be found...)

#include<iostream>
int f
int a
int i
void main()
{cout<<"Enter a number:<<a;
cin>>a;
for(i=0,i<a;i++)
{f=a;
f=f*i;
}
cout<<f;
end1
}

Please tell me if this is okay.
 
Physics news on Phys.org
  • #2
No, it's not OK. The code you posted doesn't even compile. Even after you fix the syntactical errors, it won't print a!.
 
  • #3
Considering this question is under 'homework help', some constructive feedback from ppl who are more adept with C++ would be more helpful than just a discouraging statement.
 
  • #4
Have you tried to compile your program? What kind of error messages did you get?

Also, is what you typed in the opening post an exact replica of the code you are trying to compile and execute, or did you rewrite it for this thread? There are a number of syntactical errors in what you wrote. If your code does compile, please post that code (e.g., copy and paste) rather than retyping it.
 
  • #5
I haven't even got C++ on my computer...this was homework from where I'm taking C++ coaching (only just begun, as you can well imagine)...my teacher asked me to write a code and show him..
 
  • #6
How about this...

#include<iostream>
void main()
int f;
int a;
int i;
f=1;
i=2;
cout<<"Enter a number:'<<a;
cin>>a;
for(i=2,i<a;i++)
{
f=f*i;
}
cout<<f;
return0
}
 
  • #7
At first sight - it still won't compile. Curly parentheses are not matched. " and ' don't match. No such thing as return0. Please TRY to compile your code before posting it here, otherwise this thread is a waste of time.
 
  • #8
BTW, there are many free C++ compilers out there for download for any platform/OS. If you're using Linux or some Unix (including OS X), you probably have it already installed, and accessible through the terminal. So there is absolutely no excuse for not attempting compilation before posting here.

Syntax errors should, at a minimum, be completely fixed before posting to ask for help. Posting to clarify the program flow because the output is not as expected - now that's far more reasonable.
 
  • #9
Urmi Roy said:
How about this.
You need to add this line:

using namespace std;

just after the #include <iosream> would be ok. This line is needed or else you have to use std::cout and std::cin instead of cout or cin.

you need to remove the << a from this line, since cout is used to output data, and at this point in your program, 'a' is not initialized (no specific value has been stored in 'a yet).

cout<<"Enter a number:'<<a;

This part of your program is getting close, but it needs a minor fix.

for(i=2,i<a;i++)
{
f=f*i;
}

If you have windows, you can download visual c / c++ 2010 express for free. There's also visual studio express 2012, but that needs windows 7.
 
  • #10
rcgldr said:
You need to add this line:

using namespace std;
My advice: Don't ever do that. Not ever. It's a bad habit and it will get you in trouble. Put that in a header file and it will get not only you but everyone who includes that header into trouble.

It's not just my opinion. Programming standard after programming standard prohibits the use of "using namespace xxx" no matter what that xxx is.
 
  • #11
D H said:
My advice: Don't ever do that. Not ever. It's a bad habit and it will get you in trouble. Put that in a header file and it will get not only you but everyone who includes that header into trouble.
There was no mention of putting "using namespace xxx" into a header file, perhaps you had a different point to make here?
 
  • #12
Yeah. Use std::cout, std::sqrt, std::this, std::that. It's five extra characters that will save your life. Remember the adage "Always program as if the person who will maintain or use your code is a homicidal maniac who knows where you live."
 
  • #13
OK, I got visual C++ (actually visual studio 2008) , and here's a code that does compile, and gives no errors. However, the problem is that after I enter my name, it displays 'Enter your age:' but does not take any input.
Pls provide some feedback.

#include<iostream>
#include<conio.h>
using namespace std;
class customer
{ char name[30];
int age;
public:
void accept()
{cout<<"Enter name : ";
cin>>name[30];
cout<<"Enter your age : ";
cin>>age;
}
};

void main()
{customer obj;
obj.accept();
getch();
}
 
  • #14
cin>>name[30] is a bad statement. You are supposed to use only cin>>name.
The reason for that is name[29] is the last allocated byte for the variable. name[30] might point to garbage or nothing at all.
 
Last edited:
  • #15
Urmi Roy said:
OK, I got visual C++ (actually visual studio 2008) , and here's a code that does compile, and gives no errors. However, the problem is that after I enter my name, it displays 'Enter your age:' but does not take any input.
Pls provide some feedback.
Sure, here's some. When you post code here at Physics Forums, put a [code] tag at the top and a [/code] tag at the bottom. Doing this preserves whatever indentation you started with, and makes your code easier to read.

I have done this below.
Urmi Roy said:
Code:
#include<iostream>
#include<conio.h>
using namespace std;
class customer
{	
   char name[30];
   int age;

   public:

   void accept()
   {
      cout<<"Enter name : ";
      cin>>name[30];
      cout<<"Enter your age : ";
      cin>>age;
   }
};

void main()
{
   customer obj;
   obj.accept();
   getch();
}
 
  • #16
Urmi Roy said:
OK, I got visual C++ (actually visual studio 2008) , and here's a code that does compile, and gives no errors. However, the problem is that after I enter my name, it displays 'Enter your age:' but does not take any input.
Pls provide some feedback.

Here's what's happening.
name is an array with indices running from 0 through 29.

Program: Enter name :
You: Urmi Roy

The type of name[30] is char, so the statement, cin << name[30]; stores the character 'U' at a location just outside the name array. This is why Millennial said this was a bad statement.

The input buffer still contains the characters rmi Roy, so when the prompt to enter your age appears, none of the characters in the input buffer can be construed to be a number, so your program ignores them, and ends.
 

1. What is a factorial?

A factorial is the product of all the positive integers from 1 to a given number. For example, the factorial of 5 would be 1 x 2 x 3 x 4 x 5 = 120.

2. How do you find the factorial of a number in C++?

To find the factorial of a number in C++, you can use a for loop to multiply the numbers from 1 to the given number. Alternatively, you can use recursion to call a function that calculates the factorial of the given number.

3. What is the syntax for calculating the factorial of a number in C++?

The syntax for calculating the factorial of a number in C++ may vary depending on the approach used. However, a basic syntax for a for loop would be:

int factorial = 1;

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

factorial = factorial * i;

}

Where n is the given number.

4. Can you provide an example of a C++ program to find the factorial of a number?

Yes. Here is an example of a C++ program that uses a for loop to calculate the factorial of a number:

#include <iostream>

using namespace std;

int main() {

int n, factorial = 1;

cout << "Enter a number: ";

cin >> n;

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

factorial = factorial * i;

}

cout << "Factorial of " << n << " is " << factorial << endl;

return 0;

}

5. Are there any special cases to consider when finding the factorial of a number?

Yes, there are a few special cases to consider when finding the factorial of a number. For instance, the factorial of 0 is 1. Also, since factorials can grow very large, it is important to use a data type that can handle big numbers. You may also need to consider negative numbers and handle them appropriately in your code.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
2
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
754
  • Engineering and Comp Sci Homework Help
Replies
24
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
10
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
15
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
8
Views
1K
  • Programming and Computer Science
Replies
12
Views
1K
Back
Top