Ruby Program to Subtract a Number Without Using the minus (-) Operator

You are currently viewing Ruby Program to Subtract a Number Without Using the minus (-) Operator

Ruby program to subtract a number without using the minus (-) operator

# Ruby program to subtract the numbers 
# without using the '-' operator

subt=0
num1=0
num2=0

print "Enter number1: ";
num1 = gets.chomp.to_i;  

print "Enter number2: ";
num2 = gets.chomp.to_i;  

subt = num1 + (~num2) + 1;

print "Subtraction is: ",subt;
Enter number1: 8
Enter number2: 3
Subtraction is: 5

Ranjith Kumar

Hi, I'm Ranjith a full-time Blogger, YouTuber, Affiliate Marketer, & founder of Coding Diksha. Here, I post about programming to help developers.

Leave a Reply