开发者

Calculating factorials in C#

开发者 https://www.devze.com 2023-04-01 23:08 出处:网络
I am trying to write a program that will take the users input value and asks whether they want to calculate the value of the numbers 1 to n or the factorial of n!

I am trying to write a program that will take the users input value and asks whether they want to calculate the value of the numbers 1 to n or the factorial of n! This is what I have so far

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Project_2_Part_B
{
    class Program
    {
        static void Main(string[] args)
        {
            var Fkeylow = "f";
            var FkeyCap = "F";
            var Skeylow="s";
            var SkeyCap="S";
            int n = 0;
            long factorial = n;

            Console.WriteLine("Input a value n");
            n = Convert.ToInt32(Console.ReadLine());

            Console.WriteLine("Do you want to calculate factorial or sum");
            Console.WriteLine("Enter F or S");

            var A = Console.ReadLine();

            if (A == Fkeylow)
                Console.WriteLine();

            if (A == FkeyCap)
                Console.WriteLine();

            var B=Console.ReadLine();

            if (B == Skeylow)
                Console.WriteLine();

  开发者_JAVA百科          if (B == SkeyCap)
                Console.WriteLine();

            Console.WriteLine("Press any key to close...");
            Console.ReadLine();
        }
    }
}

My issue is with the syntax of the calculation to make the code execute the n*(n-1) while n>1.


static void Main(string[] args)
{
    Console.WriteLine("Input a value n");
    string number = Console.ReadLine(); // Read number
    int n = Convert.ToInt32(number); // Converting to int

    Console.WriteLine("Do you want to calculate factorial or sum? ");
    Console.WriteLine("Enter F or S. ");
    string choose = Console.ReadLine(); // F or S

    int result = -1; // To view later

    if (choose == "f" || choose == "F")
    {
        result = 1;
        for (int i = n; i >= 1; i--) // Loop for calculating factorial
            result *= i;
    }
    else if (choose == "s" || choose == "S")
    {
        result = 0;
        for (int i = n; i >= 1; i--) // Loop for calculating sum
            result += i;
    }

    Console.WriteLine(result); // Printing answer

    Console.WriteLine("Press any key to close...");
    Console.ReadLine();
}


Using recursion is another way of doing it:

 static void Main(string[] args)
 {
        int n = 5;
        Console.WriteLine(factorial(n));
        Console.WriteLine(sum(n));
        Console.Read();
 }

 public static int sum(int n)
 {
     if(n==0)
        return 0;
     return n+sum(n-1);
 }
 public static int factorial(int n)
 {
    if (n == 1)
       return 1;
    return n * factorial(n - 1);
 }

Prints:

120 -->5!
15  -->1+2+3+4+5


Basic solution is here, tested,

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication50
{
    class Program
    {
        static void Main(string[] args)
        {
            NumberManipulator manipulator = new NumberManipulator();
            Console.WriteLine("Factorial of six is :" + manipulator.factorial(16));
            Console.ReadLine();
        }
    }
class NumberManipulator
{
    public int factorial(int num)
    {
        int result=1;
        int b = 1;
        do
        {
            result = result * b;//fact has the value 1  as constant and fact into b will be save in fact to multiply again. 
            Console.WriteLine(result);
            b++;
        } while (num >= b);
        return result;
    }
  }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号