WarriorPrincessM
WarriorPrincessM

Reputation: 39

Calculating The Factorial of a Number

When I enter the number 6 to calculate its factorial, it returns 30 (which is wrong).

Why is my program producing incorrect output?

using System;

namespace Scenario1_2
{
    class Program
    { 
        static void Main(string[] args)
        {
            int counter, number, fact;

            Console.WriteLine("Please enter the number you wish to factorize");
            number = int.Parse(Console.ReadLine());
            fact = number;

            for (counter = number - 1; counter >= 1; counter--)
            {
                fact = fact * counter;

                Console.WriteLine("The number you entered was {0} and it's factorial is {1}", number, fact);
                Console.ReadLine();
            }
        } 
    }
}

Upvotes: 3

Views: 12058

Answers (8)

shobbie14
shobbie14

Reputation: 1

import java.util.Scanner;
public class Chapter5ProblemTwelve
{
   public static void main(String [] args)
   {
      Scanner keyboard = new Scanner(System.in);
      int number;
      int factor = 1;
      int counter;
      System.out.print("Enter a positive integer to display the factorial number: ");
      number = keyboard.nextInt();
      //If the number entered is less then zero. The program will tell the user to enter a positive number
      if (number <= 0)
      {
         System.out.println("Please enter a postive number and rerun the program again.");
      }
      else
      {
      // Math work preformed if user enters a postive number. Example if user enters 4.
      // 1*1 = 1, 1*2 = 2,1*3 = 3, 1*4 = 4, The program will multiple all the answers together 1*2*3*4 = 24
       for (counter = 1; counter <= number; counter++)
       {
         factor = factor * counter;
       }
       //display 
       System.out.println("The factorial number of " + number + " is: " + factor);  

      }

   }

}

Upvotes: -1

Stephan Regan
Stephan Regan

Reputation: 41

int n = 4, fact = n;
for (int i = n; i > 1; i--)
{
    fact *= (i - 1);
}
Console.WriteLine(fact);
Console.ReadLine();

Upvotes: 0

gododgers
gododgers

Reputation: 31

using System;

namespace septtwenty
{
    class Program
    {
        static void Main(string[] args)
        {
            int i, number, fact;
            System.Console.WriteLine("Enter the Number");
            number = int.Parse(Console.ReadLine());
            fact = number;
            for (i = number -1; i>=1; i--)
            {
                fact = fact * i;
            }
            System.Console.WriteLine("\nFactorial of Given Number is: "+fact);
            Console.ReadLine();
        }
    }
}

Upvotes: -2

Pramod P
Pramod P

Reputation: 1

using System;
namespace factorial
{
    class Program
    {
        static void Main(string[] args)
        {
            int fact = 1; 
            Console.Write("Enter a number to find factorial:");
            int n = int.Parse(Console.ReadLine());
            for (int i = n; i > 0; i--)
            {
                fact = fact * i;
            }
            Console.Write("Factorial of" + n +"is :"+fact);
            Console.ReadLine();
        }
    }
}

Upvotes: -1

Joel Coehoorn
Joel Coehoorn

Reputation: 415600

You look new to programming, or least C#, so just for fun, this will blow your mind:

using System;

namespace Scenario1_2
{
    class Program
    { 
        static void Main(string[] args)
        {   
            Console.WriteLine("Please enter the number you wish to factorize");
            int number = int.Parse(Console.ReadLine());
            Console.WriteLine("The number you entered was {0} and it's factorial is {1}", number, Factorial(number));
            Console.ReadKey(true);
        }

        static int Factorial(int n)
        {
           if (n >= 2) return n * Factorial(n - 1);
           return 1;
        } 
    }
}  

No loops anywhere, and the function calls itself.

You can also do it like this:

using System;

namespace Scenario1_2
{
    class Program
    { 
        static void Main(string[] args)
        {   
            Console.WriteLine("Please enter the number you wish to factorize");
            int number = int.Parse(Console.ReadLine());
            Console.WriteLine("The number you entered was {0} and it's factorial is {1}", number, Factorial(number));
            Console.ReadKey(true);
        }

        static int Factorial(int n)
        {
           return Enumerable.Range(1, n).Aggregate((i, r) => r * i);
        } 
    }
}

Which is all kinds of messed up :) ...but it does get the significant work down to a single line of code.

Then there's my personal favorite, the infinite enumerable:

using System;

namespace Scenario1_2
{
    class Program
    { 
        static void Main(string[] args)
        {   
            Console.WriteLine("Please enter the number you wish to factorize");
            int number = int.Parse(Console.ReadLine());
            Console.WriteLine("The number you entered was {0} and it's factorial is {1}", number, Factorials().Skip(number-1).First());
            Console.ReadKey(true);
        }

        static IEnumerable<int> Factorials()
        {
            int n = 1, f = 1;
            while (true) yield return f = f * n++;
        } 
    }
}

Upvotes: 7

vivek
vivek

Reputation: 1605

You need to move two lines out from the for loop. The modified code look like this.

using System;

namespace Scenario1_2
{
    class Program
    { 
        static void Main(string[] args)
        {
           int counter, number, fact;

           Console.WriteLine("Please enter the number you wish to factorize");
           number = int.Parse(Console.ReadLine());
           fact = number;

           for (counter = number - 1; counter >= 1; counter--)
           {
               fact = fact * counter; 
           }
           Console.WriteLine("The number you entered was {0} and it's factorial is {1}", number, fact);
           Console.ReadLine();
        } 
    }
}

There are lots of ways to calculate Factorial. You can also do it by creating a recursive function. Google can help you a lot on these basic things. Thanks!

Upvotes: 0

bknights
bknights

Reputation: 15367

The program is paused waiting for some input. You need to move the second Console.ReadLine() out of the loop. And likely the Console.WriteLine() unless you want to see each iteration completing.

Upvotes: 1

Viks
Viks

Reputation: 227

why are You printing the message inside the loop.put it outside the loop

Console.WriteLine("The number you entered was {0} and it's factorial is {1}", number, fact);

Upvotes: -1

Related Questions