1. Not finding help here? Sign up for a free 30min tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

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

  1. Jun 24, 2013 #1
    1. The problem statement, all variables and given/known data

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

    2. Relevant equations

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

    3. 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.
    1. The problem statement, all variables and given/known data



    2. Relevant equations



    3. The attempt at a solution
     
  2. jcsd
  3. Jun 24, 2013 #2

    D H

    User Avatar
    Staff Emeritus
    Science Advisor

    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!.
     
  4. Jun 24, 2013 #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.
     
  5. Jun 24, 2013 #4

    D H

    User Avatar
    Staff Emeritus
    Science Advisor

    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.
     
  6. Jun 24, 2013 #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..
     
  7. Jun 24, 2013 #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
    }
     
  8. Jun 24, 2013 #7

    Borek

    User Avatar

    Staff: Mentor

    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.
     
  9. Jun 24, 2013 #8

    Curious3141

    User Avatar
    Homework Helper

    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.
     
  10. Jun 24, 2013 #9

    rcgldr

    User Avatar
    Homework Helper

    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.
     
  11. Jun 24, 2013 #10

    D H

    User Avatar
    Staff Emeritus
    Science Advisor

    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.
     
  12. Jun 24, 2013 #11

    rcgldr

    User Avatar
    Homework Helper

    There was no mention of putting "using namespace xxx" into a header file, perhaps you had a different point to make here?
     
  13. Jun 24, 2013 #12

    D H

    User Avatar
    Staff Emeritus
    Science Advisor

    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."
     
  14. Jul 12, 2013 #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();
    }
     
  15. Jul 12, 2013 #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: Jul 12, 2013
  16. Jul 12, 2013 #15

    Mark44

    Staff: Mentor

    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.
     
  17. Jul 12, 2013 #16

    Mark44

    Staff: Mentor

    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.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?
Draft saved Draft deleted



Similar Discussions: To write a C++ program to find the factorial of a number
Loading...