Please note, this is a STATIC archive of website www.w3resource.com from 19 Jul 2022, cach3.com does not collect or store any user information, there is no "phishing" involved.
w3resource

C# Sharp Exercises: Convert a binary number into a decimal using math function

C# Sharp For Loop: Exercise-46 with Solution

Write a program in C# Sharp to convert a binary number into a decimal number using math function.

Pictorial Presentation:

C# Sharp Exercises: Convert a binary number into a decimal  using math function

Sample Solution:

C# Sharp Code:

using System;  
public class Exercise46
{  
    public static void Main()
{   int n1, n;
	double dec=0,i=0,d;
	
	Console.Write("\n\n");
    Console.Write("Convert a binary number into decimal using math function:\n");
    Console.Write("-----------------------------------------------------------");
    Console.Write("\n\n");	

	Console.Write("Input  the binary number :");
    n = Convert.ToInt32(Console.ReadLine());	
	n1=n;
	while(n!=0)
	{  d = n % 10;
	   dec=dec+d*Math.Pow(2,i);
	   n=n/10;
	   i++;
	}
        Console.Write("\nThe Binary Number : {0}\nThe equivalent Decimal  Number is : {1}\n\n",n1,dec);
  }
}

Sample Output:

Convert a binary number into decimal using math function:                                                   
-----------------------------------------------------------                                                                                         
Input  the binary number :1000001                                                                           
The Binary Number : 1000001                                                                                 
The equivalent Decimal  Number is : 65

Flowchart:

Flowchart: Convert a binary number into decimal using math function

C# Sharp Code Editor:

Contribute your code and comments through Disqus.

Previous: Write a program in C# Sharp to find LCM of any two numbers.
Next: Write a C# Sharp program to check whether a number is Strong Number or not.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.