Quantcast

Got Homework?

Connect with other students for help. It's a free community.

  • across
    MIT Grad Student
    Online now
  • laura*
    Helped 1,000 students
    Online now
  • Hero
    College Math Guru
    Online now

Here's the question you clicked on:

55 members online
  • 0 replying
  • 0 viewing

ajprincess Group Title

Plzz help. write a C++ program to find the factorial of a number using keyboard input.

  • 2 years ago
  • 2 years ago

  • This Question is Closed
  1. ajprincess Group Title
    Best Response
    You've already chosen the best response.
    Medals 1

    Suppose if the entered number is 5 then your output should be as follows. 5*4*3*2*1=120. How do u do this.

    • 2 years ago
  2. adhokshaj Group Title
    Best Response
    You've already chosen the best response.
    Medals 0

    int n; // this is user input long fact = 1; for (int i = n; i>0; --i) { fact *= i; } cout<<fact; NOTE: Validate input (n>=0) before for loop.

    • 2 years ago
  3. ajprincess Group Title
    Best Response
    You've already chosen the best response.
    Medals 1

    output will b only 120 right?

    • 2 years ago
  4. ajprincess Group Title
    Best Response
    You've already chosen the best response.
    Medals 1

    My coding is this #include<iostream> using namespace std; void disfactorial(); int main() { disfactorial(); system("PAUSE"); return 0; } void disfactorial() { int num, fact=1; cout<<"Enter the number:"; cin>>num; for(int i=num;i>1;i--) { fact=fact*i; } cout<<fact<<endl; }

    • 2 years ago
  5. ajprincess Group Title
    Best Response
    You've already chosen the best response.
    Medals 1

    When I enter 5 I get the output as 120. I want it to display 5*4*3*2*1=120. Hw can I edit my code to display this?

    • 2 years ago
  6. eSpeX Group Title
    Best Response
    You've already chosen the best response.
    Medals 1

    A suggestion would be to implement your user interaction within the main: int main() { int fact; cout<<"Enter the number:"; cin>>num; fact = disfactorial(num); cout<<"The factorial of " + 5 + " is " + fact<<endl; system("PAUSE"); return 0; } Then define your disfactorial() method like this: int disfactorial(num) { int fact = 1, i; for (i = 1; i<num; i++) { fact =fact * (i+1); } return fact; } The output of the for loop, if the user enters 5, should be: i=1: 1*2 i=2: 2*3 i=3: 6*4 i=4: 24*5

    • 2 years ago
  7. eSpeX Group Title
    Best Response
    You've already chosen the best response.
    Medals 1

    To fix your code to produce 120 as the output you would just need to change your loop: from this: for(int i=num;i>1;i--) { fact=fact*i; } to this: for(int i=1;i<num;i++) { fact=fact*i+1; }

    • 2 years ago
  8. ajprincess Group Title
    Best Response
    You've already chosen the best response.
    Medals 1

    I dnt get the correct output.

    • 2 years ago
  9. eSpeX Group Title
    Best Response
    You've already chosen the best response.
    Medals 1

    I had some compilation errors, mixed java and c++ syntax. Try this: #include<iostream> using namespace std; int disfactorial(int x); int main() { int fact, num; cout<<"Enter the number:"; cin>>num; fact = disfactorial(num); cout<<"The factorial of " << 5 << " is " << fact<<endl; return 0; } int disfactorial(int num) { int fact = 1, i; for (i = 1; i<num; i++) { fact =fact * (i+1); } return fact; }

    • 2 years ago
  10. eSpeX Group Title
    Best Response
    You've already chosen the best response.
    Medals 1

    input: 5 output: Enter the number:The factorial of 5 is 120

    • 2 years ago
  11. ajprincess Group Title
    Best Response
    You've already chosen the best response.
    Medals 1

    I got the same output for my coding above.

    • 2 years ago
  12. ajprincess Group Title
    Best Response
    You've already chosen the best response.
    Medals 1

    I changed ur code and only compiled bt didnt get the output.

    • 2 years ago
  13. eSpeX Group Title
    Best Response
    You've already chosen the best response.
    Medals 1

    If you are getting 120 as the factorial of 5, which it is, then I must have misunderstood the question, I apologize. What were you having difficulty with?

    • 2 years ago
  14. ajprincess Group Title
    Best Response
    You've already chosen the best response.
    Medals 1

    I want the output like this 5*4*3*2*1=120 Nt jst 120

    • 2 years ago
  15. eSpeX Group Title
    Best Response
    You've already chosen the best response.
    Medals 1

    OH! You are looking at formatting..

    • 2 years ago
  16. eSpeX Group Title
    Best Response
    You've already chosen the best response.
    Medals 1

    With the smaller numbers it will be simple to just hard code them, but you could always create an array the size of your "num" and set each index as you step through your for loop. However you should consider that if this code were to be used on a large number, say 50, you would have text scrolling across the screen.

    • 2 years ago
  17. ajprincess Group Title
    Best Response
    You've already chosen the best response.
    Medals 1

    it's true. Thanxxxxxxxxxxxx a lot

    • 2 years ago
  18. agdgdgdgwngo Group Title
    Best Response
    You've already chosen the best response.
    Medals 0

    the factorial grows really fast, so you get integer overflow once you go after about 20! or something

    • 2 years ago
  19. agdgdgdgwngo Group Title
    Best Response
    You've already chosen the best response.
    Medals 0

    I wonder if C++ has bignums included in the library.

    • 2 years ago
  20. eSpeX Group Title
    Best Response
    You've already chosen the best response.
    Medals 1

    I'm fairly certain they have long unsigned double, that's fairly big. :)

    • 2 years ago
    • Attachments:

See more questions >>>

Your question is ready. Sign up for free to start getting answers.

spraguer (Moderator)
5 → View Detailed Profile

is replying to Can someone tell me what button the professor is hitting...

23

  • Teamwork 19 Teammate
  • Problem Solving 19 Hero
  • You have blocked this person.
  • ✔ You're a fan Checking fan status...

Thanks for being so helpful in mathematics. If you are getting quality help, make sure you spread the word about OpenStudy.

This is the testimonial you wrote.
You haven't written a testimonial for Owlfred.