User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C# section within the Software Development category of DaniWeb, a massive community of 427,018 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,528 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C# advertiser: Programming Forums
Views: 2882 | Replies: 9
Reply
Join Date: Nov 2006
Posts: 2
Reputation: k8lj is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
k8lj k8lj is offline Offline
Newbie Poster

Help having problem while creating factorial

  #1  
Nov 6th, 2006
i want to write a program that will output all of the factorials up to a maximum number provided....if anyone can guide me, thanks...
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Oct 2006
Posts: 86
Reputation: TylerSBreton is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 3
TylerSBreton's Avatar
TylerSBreton TylerSBreton is offline Offline
Junior Poster in Training

Re: having problem while creating factorial

  #2  
Nov 7th, 2006
youll need an outer loop which loops as high as the amount of factorials you want to compute, and an inner loop which will compute the factorial at the outer loops number.

pseudocode:

input n
set x = 1
loop n times
set num = 1
set y = 0
loop from x to 1
multiply num by y and re-store it into num
end loop
print num
end loop

that should do the job....unless i overlooked something
Reply With Quote  
Join Date: Oct 2006
Location: Idaho
Posts: 44
Reputation: Godfear1 is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 3
Godfear1's Avatar
Godfear1 Godfear1 is offline Offline
Light Poster

Re: having problem while creating factorial

  #3  
Nov 7th, 2006
Well, first of all you need to know what a factorial is. You haven't demonstrated that you know anything on how to solve this problem yet. If you need a description on what a factorial is here's a link.
http://mathworld.wolfram.com/Factorial.html
You would have to have a loop inside of a loop in order to make this work. Here's a little bit of code to get you started on your merry way:

            int factorial = 4;
            int result = 1;
            for (int i = 0; i < factorial; i++)
                result *= i+1;
            System.Console.WriteLine("result:" + result);

the factorial is saved to the int result. If you want multiple answers just put the whole thing in a for loop incrementing factorial till you get to the desired value.
Hope this is helpful. I don't want to solve the whole thing for you though...
Reply With Quote  
Join Date: Nov 2006
Posts: 2
Reputation: k8lj is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
k8lj k8lj is offline Offline
Newbie Poster

Solution Re: having problem while creating factorial

  #4  
Nov 13th, 2006
thanks a lot for the help both of ya guys..the way i did using the code above as following and worked perfectly fine for the results required...

static void Main(string[] args)
{
int num;
int j;

Console.WriteLine("Enter the number: ");
num = int.Parse(Console.ReadLine());

for (int i = 1; i <=num; i++)
{
Console.Write("1");

int total = 1;
for (j=1; j <= i; j++)
{
Console.Write("X" + j);
total = total*j;
}
Console.WriteLine(" = " + total);

}
}
Reply With Quote  
Join Date: Oct 2006
Location: Idaho
Posts: 44
Reputation: Godfear1 is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 3
Godfear1's Avatar
Godfear1 Godfear1 is offline Offline
Light Poster

Re: having problem while creating factorial

  #5  
Nov 13th, 2006
That looks great. I'm glad I could help you. I just took a math assessment and I needed a little encouragment. lol. Don't hesitate to ask if you need help on anything else.
Reply With Quote  
Join Date: Oct 2006
Posts: 1,451
Reputation: sneekula is on a distinguished road 
Rep Power: 4
Solved Threads: 44
sneekula's Avatar
sneekula sneekula is offline Offline
Nearly a Posting Virtuoso

Re: having problem while creating factorial

  #6  
Nov 16th, 2006
Nice program! I ran it and for factorial 13 I got 1932053504 when it should be 6227020800.
No one died when Clinton lied.
Reply With Quote  
Join Date: Nov 2006
Posts: 2
Reputation: Yaseen Hejazi is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
Yaseen Hejazi Yaseen Hejazi is offline Offline
Newbie Poster

Re: having problem while creating factorial

  #7  
Nov 16th, 2006
Originally Posted by sneekula View Post
Nice program! I ran it and for factorial 13 I got 1932053504 when it should be 6227020800.


That because he used int type to total variable which can hold 2,147,483,647 as maximum positeve value.
You can change this line:
int total = 1;

to:
double total = 1;
Reply With Quote  
Join Date: Oct 2006
Location: Idaho
Posts: 44
Reputation: Godfear1 is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 3
Godfear1's Avatar
Godfear1 Godfear1 is offline Offline
Light Poster

Re: having problem while creating factorial

  #8  
Nov 16th, 2006
Thanks for that response. That's what I was going to say. This problem does require a little extra thinking for really large numbers. You need to have an understanding about basic programming.
Reply With Quote  
Join Date: Aug 2005
Posts: 4,782
Reputation: iamthwee is a glorious beacon of light iamthwee is a glorious beacon of light iamthwee is a glorious beacon of light iamthwee is a glorious beacon of light iamthwee is a glorious beacon of light 
Rep Power: 17
Solved Threads: 319
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Industrious Poster

Re: having problem while creating factorial

  #9  
Nov 18th, 2006
For really large number you need to consider using strings.

Perhaps even karbutstra's algo, for efficient multiplication.
I'm not a programmer. My attitude starts with ignorance, holds steady at conversation, and ends with a trip to the hospital. Get used to it.
Reply With Quote  
Join Date: Aug 2005
Posts: 4,782
Reputation: iamthwee is a glorious beacon of light iamthwee is a glorious beacon of light iamthwee is a glorious beacon of light iamthwee is a glorious beacon of light iamthwee is a glorious beacon of light 
Rep Power: 17
Solved Threads: 319
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Industrious Poster

Re: having problem while creating factorial

  #10  
Nov 18th, 2006
And if you can manage it I think there is a possibility to integrate java's big number class with vb.net or so I've read.
I'm not a programmer. My attitude starts with ignorance, holds steady at conversation, and ends with a trip to the hospital. Get used to it.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb C# Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the C# Forum

All times are GMT -4. The time now is 2:45 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC