How To Calculate Factorial Then Do It Backwards On Sharpdevelop #6

Words
419
Reading
2 min
Listen
Play
9y

What Will I Learn?

Hello,in this tutorial;

  • You will learn how to use for loop properly.
  • You will learn how to code with c#
  • You will learn Sharpdevelop

Requirements

  • Sharpdevelop
  • Basic knowledge of c# coding.
  • Basic knowledge of using sharpdevelop

Difficulty

  • Basic

Tutorial Contents

Hello,in this tutorial i am going to show you how to use for loop properly to make hard math problems look easy.This program will calculate any factorial easily.Then i will show how to make it backwards with c#.Let's get it started;

As usual we define our library to make codes work.


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Calculate_Factorial
class Program
static void Main(string[] args)

We define our integers,first we define "n" as math term.Then we define "Result" as double.


int n;
double Result = 1;

In this part we print "n".Then convert it to int 32byte.


Console.Write("n=");
n = int.Parse(Console.ReadLine());

1.png

Now we gotta use for loop to increase our variable if needed."Result *=i" this line is how you do the factorial problem.This how you do it on c#


for (int i = 1; i <= n; i++)
Result *= i;

Finally we gotta make console to write the line then make the factorial math in c# type.Then do the addition on result.


Console.WriteLine();
Console.Write(n + "!=" + Result);
Console.ReadKey();

2.png

Output of this code ;

3.png

Now we make it reverse.First define libraries.Name our program


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Reverse_Factorial
class Program
static void Main(string[] args)

Now we define two integers because we need to integers both for "if" loop.


int n;
int k=1;

We ask user to enter the value then convert "n" to integer 32byte.


Console.Write("Enter The Value Please=");
n = int.Parse(Console.ReadLine());

4.png

Now in this line i will use for loop to calculate what user have entered.I did it 90.000 but you can make it bigger if you want.This part is important ;


for (int i = 1; i < 90000; i++)
k = k * i;

Now we will evaluate the possibility and use "if" loop for that.We calculate the factorial in backwards in second line.


if (k==n)
Console.WriteLine(n+"="+i+"!");
break;

5.png

Now if user enters a number that is not a factorial we have to tell it to the user.This part is where we do it;


else if (k>n)
Console.WriteLine("The number you have entered is not factorial!");
break;
Console.ReadKey();

6.png

Outputs of the code;

7.png

8.png

Curriculum

If you want to start from somewhere in c# coding keep up with me.More to come...

From first to last

https://steemit.com/utopian-io/@milliar/how-to-convert-mm-to-any-length-in-sharpdevelop

https://steemit.com/utopian-io/@milliar/how-to-calculate-triangles-area-and-make-operation-do-math-problem-with-sharpdevelop

https://steemit.com/utopian-io/@milliar/how-to-code-with-sharpdevelop-finding-if-random-number-is-odd-or-not-and-more-3

https://steemit.com/utopian-io/@milliar/how-to-use-datetime-method-on-sharpdevelop-4

https://steemit.com/utopian-io/@milliar/how-to-make-basic-lottery-program-and-learn-arrays-on-sharpdevelop-5



Posted on Utopian.io - Rewarding Open Source Contributors

How To Calculate Factorial Then Do It Backwards On Sharpdevelop #6 | Ecency