Class: Pascal

Inherits:
Object
  • Object
show all
Includes:
Modules
Defined in:
lib/math_func.rb

Instance Method Summary collapse

Instance Method Details

#funcObject



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/math_func.rb', line 14

def func
  fact = Inherit.new
  prompt = "Enter a number between the given range"
  loop do
    print "Menu:\n1 : Armstrong Number\n2 : Factorial\n3 : Prime Number\n4 : Pascal Triangle\n0 : Exit\nPlease enter your choice : "
    selection = gets.to_i
    if selection == 1
    def Armstrong(n)
      num = n
      sum = 0
      temp = num
      while num>0
        sum = sum + (num%10) * (num%10) * (num%10)
        num = num/10  
      end
      if temp == sum
         "#{temp} is an Armstrong Number"
        
      else
         "#{temp} is not an Armstrong Number"
      end
    end
      print "Enter a number: "
      n = gets.to_i
      # catch (n > 0) do
      #  while n <= 0
      #    puts "Enter number only"
      #    throw id <= 0 unless id = gets.to_i 
      #  end 
      # end
    puts Armstrong(n)
    elsif selection == 2
      print "Enter your choice:\n1 : Factorial\n2 : ncr\n3 : npr\n"
      print "Enter a number between 1 to  3 : "
      
      select = gets.to_i
      case select
        when 1
          puts"Factorial:"
          print "Enter a number: "
          n = gets.to_i
          print "Factorial of #{n} of is ",  fact.factorial(n), "\n\n"
          
        when 2
          puts "ncr:"
          print "Enter the value of n "
          n = gets.to_i
          print "Enter the value of r "
          r = gets.to_i
          res = fact.factorial(n) / (fact.factorial(r))
          
          puts "ncr of given variables is #{res}"
        when 3
          puts "npr:"
          print "Enter the value of n: "
          n = gets.to_i
          print "Enter the value of r: "
          r = gets.to_i
          res = fact.factorial(n) / (fact.factorial(r) * fact.factorial(n-r))
          puts "ncr of given variables is #{res}"
      end
    elsif selection == 3
      print "Enter a number \t"
      n = gets.to_i
      def prime(n)
      puts "That's not an integer." unless n.is_a? Integer
      is_prime = true
      for i in 2..n-1
          if n % i == 0
            is_prime = false
          end
        end
      if is_prime
          puts "#{n} is prime!"
        else
          puts "#{n} is not prime."
        end
        end
        prime(n)
    elsif selection == 4
      print "Enter how many rows you want: "
      n = gets.to_i
      def triangle(n)
        fact = Inherit.new
        for i in (0..n)     
          for j in (0..n-i-1)
          print " " 
          end
          for j in (0..i)
            res = fact.factorial(i)/(fact.factorial(j) * fact.factorial(i-j))
            print res
            print " "
          end
          puts "\n"
        end
      end
      triangle(n)
    elsif selection == 0
      puts "Thank you"
      break
    else
      print "Enter a number from the given range only"
      redo
    end 
end 
end