Results 1 to 8 of 8

Thread: Question about a factorial program

  1. #1

    Thread Starter
    Fanatic Member Wynd's Avatar
    Join Date
    Dec 2000
    Location
    In a bar frequented by colossal death robots
    Posts
    772
    I was wondering if anyone can help, because i cn get it to work in java but not in C++.

    #include <iostream.h>

    void main()
    {
    int input;
    cout<<"Type in a number\n";
    cin>>input;
    for (int i = 1; i <= input; i++)
    {
    input = i * input;
    }
    cout<<input
    }

    like i said, i got it to work injava, but whenever i type something in here i get something totally different (like -1899959296 for 5 when it should be 120).

  2. #2
    Frenzied Member HarryW's Avatar
    Join Date
    Jan 2000
    Location
    Heiho no michi
    Posts
    1,827
    try it with this line instead:

    cout << "Type in a number" << endl;
    Harry.

    "From one thing, know ten thousand things."

  3. #3

    Thread Starter
    Fanatic Member Wynd's Avatar
    Join Date
    Dec 2000
    Location
    In a bar frequented by colossal death robots
    Posts
    772

    Cool

    Heh, I should have guessed. Lots of times whe ni have something like this i can use endl; and fix it. It's like the solution to all the world's problems or something....

  4. #4
    New Member
    Join Date
    Feb 2001
    Posts
    5
    No cheating Pete, figure it out for yourself. No using Math.PI. You have to learn the functions

  5. #5

    Thread Starter
    Fanatic Member Wynd's Avatar
    Join Date
    Dec 2000
    Location
    In a bar frequented by colossal death robots
    Posts
    772

    Angry

    Hmm...it's still not working.

  6. #6
    PowerPoster sail3005's Avatar
    Join Date
    Oct 2000
    Location
    Chicago, IL, USA
    Posts
    2,340
    try putting endl after all the lines

  7. #7
    Frenzied Member HarryW's Avatar
    Join Date
    Jan 2000
    Location
    Heiho no michi
    Posts
    1,827
    It's because of the way you're using the input variable as a control variable for the loop. You can do it the way you're doing it with an extra variable, like this:

    Code:
    #include <iostream.h> 
    
    void main() 
    { 
    	int input; 
    	int output = 1;
    	cout<<"Type in a number" << endl;
    	cin>>input; 
    	for (int i = 1; i <= input; i++) 	
    		output *= i;
    	cout<< output<< endl; 
    }
    Harry.

    "From one thing, know ten thousand things."

  8. #8

    Thread Starter
    Fanatic Member Wynd's Avatar
    Join Date
    Dec 2000
    Location
    In a bar frequented by colossal death robots
    Posts
    772
    Cool, it works now. Thanks a lot for your help
    Alcohol & calculus don't mix.
    Never drink & derive.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width