Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- namespace ExceptionFatorial
- {
- class Program
- {
- public static int Fatorial(int n)
- {
- if (n < 0)
- {
- Exception e = new Exception("Nao pode ser negativo! ");
- throw e;
- }
- int fat = 1;
- for (int i = 2; i <= n; i++)
- {
- fat = fat * i;
- }
- return fat;
- }
- }
- }
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- namespace ExceptionFatorial
- {
- class app
- {
- static void Main(string[] args)
- {
- Console.Write("Informe o valor de n: ");
- int n = Convert.ToInt32(Console.ReadLine());
- while (n != 0)
- {
- try
- {
- Console.WriteLine("O fatorial de {0} é {1}", n, Program.Fatorial(n));
- }
- catch (Exception ex)
- {
- Console.Write(ex.Message);
- }
- Console.Write("Informe o valor de n: ");
- n = Convert.ToInt32(Console.ReadLine());
- }
- }
- }
- }
Add Comment
Please, Sign In to add comment