Guest User

Untitled

a guest
Nov 17th, 2018
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.31 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. namespace ExceptionFatorial
  7. {
  8. class Program
  9. {
  10. public static int Fatorial(int n)
  11. {
  12. if (n < 0)
  13. {
  14. Exception e = new Exception("Nao pode ser negativo! ");
  15. throw e;
  16. }
  17.  
  18. int fat = 1;
  19. for (int i = 2; i <= n; i++)
  20. {
  21. fat = fat * i;
  22. }
  23. return fat;
  24. }
  25. }
  26. }
  27.  
  28.  
  29.  
  30.  
  31.  
  32. using System;
  33. using System.Collections.Generic;
  34. using System.Linq;
  35. using System.Text;
  36.  
  37. namespace ExceptionFatorial
  38. {
  39. class app
  40. {
  41. static void Main(string[] args)
  42. {
  43. Console.Write("Informe o valor de n: ");
  44. int n = Convert.ToInt32(Console.ReadLine());
  45.  
  46. while (n != 0)
  47. {
  48. try
  49. {
  50. Console.WriteLine("O fatorial de {0} é {1}", n, Program.Fatorial(n));
  51. }
  52. catch (Exception ex)
  53. {
  54. Console.Write(ex.Message);
  55.  
  56. }
  57.  
  58.  
  59. Console.Write("Informe o valor de n: ");
  60. n = Convert.ToInt32(Console.ReadLine());
  61. }
  62. }
  63. }
  64. }
Add Comment
Please, Sign In to add comment