I am new to the website and also to programming. I have an assignment I am working on which is included below. I am having a difficult time getting started with the code and not sure of what needs to be done. Any advice and help is appreciated. Thanks in advance.


Create a class called "Factorial" that defines a method called "CalculateFactorial" which prompts the user to enter a positive integer and then calculates and prints to the screen the factorial of that number. Create a second class called " UseFactorial" which creates an object of the "Factorial" class and uses its method to calculate and print to the screen the factorial of an integer

What have you tried so far in code? Can you create a project that has 2 (empty) classes in it?

Here is what I was able to creat so far...It works but I know I will have to fit it to 2 seperate classes.

using System;

public class Factorial
{
   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);

      }
   }
}


using System;

public class UseFactorial
{
interger CalculateFactorial = new interger

}

Since you're new to programming, I will tell you some programming tips:

-Try to indent your code (inserting tabs or spaces when you're between brackets).
It will make your loops (for), your conditions (if) and all your code more easy to read and understand.
For example:

int main()
 {
     return 0; //Inserting tab to make the code more readable.
 }

Your code will look like:

FactorialClass factorial = new FactorialClass();
factorial.CalculateFactorial(4);

OR

FactorialClass.CalculateFactorial(4); //Static method invocation (no need of creating an object)

AND

UseFactorial useF = new UseFactorial();
useF.CalculateFactorial(4);

In C#, you create a class like this:

class MyClass
{
    //A method example (this is a comment)
    public void MyMethod()
    {
    }
}

I will leave to you the work of implementing CalculateFactorial methods.

Hope this helps.

Since you're new to programming, I will tell you some programming tips:

-Try to indent your code (inserting tabs or spaces when you're between brackets).
It will make your loops (for), your conditions (if) and all your code more easy to read and understand.

A very good point.


Additionally, putting your code in:

[code]

//code here

[/code]
will preserve the formatting of your code in the forum, otherwise all leading space is stripped from the post.

commented: subtle :) +7

Here is what I was able to creat so far...It works but I know I will have to fit it to 2 seperate classes.

using System;

public class UseFactorial
{
   interger CalculateFactorial = new interger

}

Not a bad start. Take a look at your specification, it says the method should be CalculateFactorial not the class. You could conceivably keep Factorial (a class with member CalculateFactorial) in Program.cs with Main() but I would keep it separate. Make them separate files even (keeping the namespace the same). So right-click on the project name in the solution explorer and click Add...class (if you've done this already all the better).

Take another crack at it and post back with what you have.

Thanks everyone for your help and advice, I will make those changes and continue to read up on everything and work on the code. You have been a big help to me.

Was anyone able to figure this out?

Start your own thread with your code and someone will help you.

using System;
class Factorial
{
    int Number;

    public void CalculateFactorial()
    {
        Console.WriteLine("Enter the Number");
        Number = Convert.ToInt32(Console.ReadLine());
        Console.WriteLine("*************************************");
        if (Number == 1)
        {
            Console.WriteLine("Factorial of given number is:-1");
        }
        else
        {
            int a = Number;
            int x;
            for( x=1; x<a;x++)
            {
                Number = Number * x;
            }
            Console.WriteLine("Factorial of given number is:{0}",Number);
        }
        Console.ReadLine();
     }
}
class UseFactorial
{
    public static void Main(string[] args)
    {
        Factorial obj = new Factorial();
        obj.CalculateFactorial();
    }
}

Old thread, and your code produces the wrong value for one number.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.