Writing C# programs-easy-

Elie
by Elie · 11 posts
15 years ago in .Net (C#, VB, etc)
Posted 15 years ago · Author
well im not posting this to get the solution, I already have it .
im posting this assignment to make interacting between the members of this site and also to give benefits to those whom wants to learn C# but they dont know much about it .
( sure I hope that Don approve to post things like these, if not its ok :) )

so lets start with a very simple program :

Using C# Write a program that calculate the factorial of a number N,
then improve it to estimates the value of the mathematical constant e by using the formula :

e = 1 + 1/1! + 1/2! + 1/3! ..........................

1! = factorial of 1, 2! = factorial of 2 ..........................................
any ideas ?? :)
Posted 15 years ago · Author
OOPS lol , I think that no one is interested into c#
anyway I'll post the solution .
Code
using System;
using System.Collections.Generic;
using System.Text;
namespace Factorial
{
    class Program
    {
        static void Main(string[] args)
        {
            int N;
            long fact = 1;

            // input the number
            Console.WriteLine("Enter a Number");
            N = Convert.ToInt32(Console.ReadLine());

            // calculate the factorial
            for (int i = 2; i <= N; i++)
            {
                fact *= i;
            }

            // output the result
            Console.WriteLine("The Result is {0}", fact);
        }
    }
}
Posted 15 years ago · Author
then we could easily improve it to calculate the ( e ) by doing this
Code

using System;
using System.Collections.Generic;
using System.Text;
namespace Factorial
{
    class Program
    {
        static void Main(string[] args)
        {
           Console.WriteLine("This program is to calculate the natural logarithm ( e )");
           double fractional=1;
           double e=2;
           /* in the next step ( 100 ) is optional , but you should make the 
reapitition over 17 times to get an accurate value of (e) */

           for (int i = 2; i <=100; i++)
           {
              fractional = fractional * i;
              e = e + (1 / fractional);
           }
           Console.WriteLine(" e is {0}", e);
   
       
        }
    }
}

PST. ( e ) should look something like this 2.71828182845905
Posted 14 years ago
I dunno c# but I can give u c
Posted 14 years ago
Don Von Free Credits wrote:
JonXRulz wrote:
I dunno c# but I can give u c


C# is microsoft's version of C++, which is just C without pointers and String support added.


yeah but the statements are kinda different
Posted 14 years ago
Thanks this really helped me out too :)
Posted 14 years ago
hello :D
Posted 14 years ago
Helpful:)
Posted 14 years ago
Useful

Create an account or sign in to comment

You need to be a member in order to leave a comment

Sign in

Already have an account? Sign in here

SIGN IN NOW

Create an account

Sign up for a new account in our community. It's easy!

REGISTER A NEW ACCOUNT