Sum of two number without using arithmetical operator:

Write a program to sum a two number without using arithmetical operator (+).

Solution:

using System;
using System.Collections.Generic;

namespace AdditionSum
{
    class Program
    {
        static void Main()
        {
            Console.WriteLine("Enter a number:");
            var num1 = Convert.ToInt32(Console.ReadLine());

            Console.WriteLine("Enter a number:");
            var num2 = Convert.ToInt32(Console.ReadLine());

            Console.WriteLine($"Sum of two number is: {Sum(num1, num2)}.");
            Console.ReadLine();
        }

        public static int Sum(int a, int b)
        {
            if (b == 0) return a;
            var sum = a ^ b; 
            var carry = (a & b) << 1;  
            return Sum(sum, carry);
        }
    }
}

Leave a Reply

Your email address will not be published. Required fields are marked *