
- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to find the product of two binary numbers using C#?
To find the product of two binary numbers, firstly set them.
val1 = 11100; val2 = 10001; Console.WriteLine("Binary one: "+val1); Console.WriteLine("Binary two: "+val2);
Now loop through to get the product.
while (val2 != 0) { digit = val2 % 10; if (digit == 1) { val1 = val1 * factor; prod = displayMul(val1, prod); } else val1 = val1 * factor; val2 = val2 / 10; factor = 10; } Console.WriteLine("Product = {0}", prod);
Above a method displayMul() is called with the first binary number.
static long displayMul (long val1, long val2) { long i = 0, rem = 0, mul = 0; long[] sum = new long[30]; while (val1 != 0 || val2 != 0) { sum[i++] =(val1 % 10 + val2 % 10 + rem) % 2; rem =(val1 % 10 + val2 % 10 + rem) / 2; val1 = val1 / 10; val2 = val2 / 10; } if (rem != 0) sum[i++] = rem; i = i-1;; while (i >= 0) mul = mul * 10 + sum[i--]; return mul; }
Here is the complete code −
Example
using System; class Demo { public static void Main(string[] args) { long val1, val2, prod = 0; long digit, factor = 1; val1 = 11100; val2 = 10001; Console.WriteLine("Binary one: "+val1); Console.WriteLine("Binary two: "+val2); while (val2 != 0) { digit = val2 % 10; if (digit == 1) { val1 = val1 * factor; prod = displayMul(val1, prod); } else val1 = val1 * factor; val2 = val2 / 10; factor = 10; } Console.WriteLine("Product = {0}", prod); } static long displayMul (long val1, long val2) { long i = 0, rem = 0, mul = 0; long[] sum = new long[30]; while (val1 != 0 || val2 != 0) { sum[i++] =(val1 % 10 + val2 % 10 + rem) % 2; rem =(val1 % 10 + val2 % 10 + rem) / 2; val1 = val1 / 10; val2 = val2 / 10; } if (rem != 0) sum[i++] = rem; i = i-1;; while (i >= 0) mul = mul * 10 + sum[i--]; return mul; } }
Output
Binary one: 11100 Binary two: 10001 Product = 111011100
- Related Questions & Answers
- How to find the Sum of two Binary Numbers using C#?
- Python Program to Find the Product of two Numbers Using Recursion
- Java Program to Find the Product of Two Numbers Using Recursion
- How to find the product of 2 numbers using recursion in C#?
- Find the Sum of two Binary Numbers without using a method in C#?
- Find two distinct prime numbers with given product in C++
- Java program to calculate the product of two numbers
- Find the Product of first N Prime Numbers in C++
- Find two distinct prime numbers with given product in C++ Program
- How to find the dot product of two matrices in R?
- How to find the dot product of two pandas series objects?
- C++ Program to find the median of two sorted arrays using binary search approach
- Python program to find product of rational numbers using reduce function
- How to get the product of two integers without using * JavaScript
- C++ program to find two numbers with sum and product both same as N
Advertisements