Skip to content

Logic Quest

VLSI Interview Questions

  • Wave Diagram Analysis
  • Design Questions
  • Gate Level Questions
  • Some Coding
  • Contact us
  • Toggle search form

Convert to Decimal

Wrtie a function that gets a number (containing only 1’s and 0’s) and returns its decimal representation. You can assume validity of the input.

For example:

INPUT  : 101
OUTPUT : 5

int toDecimal(int num)

Answer

int toDecimal(int num) {
	int digit;
	int toDecimal = 0;
	int base = 1;
	while (num > 0) {
		digit = num % 10;
		toDecimal = toDecimal + base * digit;
		base *= 2;
		num = num / 10;
	}
	return toDecimal;
}
INPUT  : 1001
OUTPUT : 9

INPUT  : 1111
OUTPUT : 5

INPUT  : 1100
OUTPUT : 12

Next Question>>>

Leave a Reply Cancel reply

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

Copyright © 2023 Logic Quest.

Powered by PressBook Blog WordPress theme