Link to home
Create AccountLog in
Avatar of kensy11
kensy11Flag for Belgium

asked on

For loop dont work

Hello,

I'm stuck i dont know why my FOR loop dont work???

i want to get the faculty of a number using for , but the FOR dont work ?

here is my code


using System;

namespace ConsAppl1TI11 {
  class Program {
    static void Main(string[] args) {
      ConsCode Cc = new ConsCode();
            int n = 0;
      int p = 0;
      int Faculteit = 0;
      Console.WriteLine("Combinatieleer");
      Console.WriteLine("Voor ingegeven waarde n en p wordt berekend: P<n> V<n, p> C<n, p>");
      
      Console.WriteLine("Geef de waarde van n a.u.b. ... ");
      n = int.Parse(Console.ReadLine());

      Console.WriteLine("Geef de waarde van n a.u.b. ... ");
      p = int.Parse(Console.ReadLine());

      while (n <= p) {

        Console.WriteLine("Getal N moet groter zijn dan getal P .");
        Console.WriteLine("Geef de waarde van n a.u.b. ... ");
        p = int.Parse(Console.ReadLine());
      
      }

      for(int teller =n-1 ; teller < 1 ; teller--) {

        Faculteit += n *teller;

        Console.WriteLine("P<{0}> = {1}", n, Faculteit);
      }

      Console.WriteLine("P<{0}> = {1}", n, Faculteit);
    }/*Main*/
  }/*Program*/
}/*ConsAppl1TI11*/

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of pivar
pivar
Flag of Sweden image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
SOLUTION
Avatar of sarabande
sarabande
Flag of Luxembourg image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
pivar has pointed out the correct reason for the FOR loop not working.

And sara has shown another potential problem.

But, I have a more fundamental question.
In your question, you have written that:
> i want to get the faculty of a number using for

But, your for loop is doing:
> Faculteit += n *teller;

So, what is Facultit to do with factorial?
Is it just a spelling error or are you trying something else?

If you want to do factorial of large numbers, here are few suggestions.
1. You should use a library such as GMP (http://gmplib.org/)
2. You can also look at:
      http://libtom.org/?page=features&newsitems=5&whatfile=ltm
    for such libraries.
3. Here is another way to get the result without using any special library:
     http://cquestionbank.blogspot.com/2010/08/factorial-of-big-numbers-in-c.html

But, the above links are for C programming.
I don't know, if you can directly use them in your code also.