TCS NQT Set 3 | Day 1 2020 | Decimal conversion

TCS NQT Set 3 | Day 1 2020 | Decimal conversion

TCS NQT 2020 | Day 1 | Set 3

Given a maximum of four-digit to the base 17 (10 – A, 11 – B, 12 – C, 13 – D … 16 – G} as
input, output its decimal value.

Logic:

  • Get the input as a string because it contains both integer and character.
  • Then assign A=10,B=11………. G=16.
  • Convert the character to an integer using ASCII
  • Apply the rule for any number to decimal conversion.
  • Here base value 17, then move from right to left and the power value starts from 0 then increments
  • Find the power of 17 from 0 to length of string and correspondingly add the value in a variable
  • Then print the result which is the decimal value.

Other numbers to Decimal conversion

  • Step 1 − Determine the column (positional) value of each digit (this depends on the position of the digit and the base of the number system).
  • Step 2 − Multiply the obtained column values (in Step 1) by the digits in the corresponding columns.
  • Step 3 − Sum the products calculated in Step 2. The total is the equivalent value in decimal.

TCS NQT Set 3

Program:

[code lang=”c”]
#include<stdio.h>
int pow(int a,int n)
{
int b=a;
if(n==0) //return 1 when n value is 0
{
return 1;
}
while(n>1) //calculate the power value
{
a*=b;
n–;
}
return a;
}
void main()
{
char a[50];
int n,i,j=0,decimal=0;
printf("Enter the base 17 number:");
scanf("%s",&a); //get the input as string
n=strlen(a);
while(n>0)
{
if(a[n-1]>64&&a[n-1]<72) //check the char between A to G
{
i=a[n-1]-55; //convert A=10,b=11 ….. G=16
decimal+=i*pow(17,j); //calculate the decimal value
n–,j++;
}
else //check for number 0 to 9
{
i=a[n-1]-48; //convert char value to int
decimal+=i*pow(17,j);
n–,j++;
}
}
printf("%d\n",decimal); // print the converted decimal value
}
[/code]

You might also  like:

TCS NQT 2020 | Day 1 | Set 1

TCS NQT 2020 | Day 1 | Set 2

TCS NQT 2020 | Day 1 | Set 4

Vignesh

A Computer Science graduate who likes to make things simpler. When he’s not working, you can find him surfing the web, learning facts, tricks and life hacks. He also enjoys movies in his leisure time.

2 thoughts on “TCS NQT Set 3 | Day 1 2020 | Decimal conversion

  1. //code by Mithan (mail me @:mithanmr5@gmail.com
    b=input()
    s=b[::-1]
    d={‘A’:10,’B’:11,’C’:12,’D’:13,’E’:14,’F’:15,’G’:16}
    summ=0
    for i in range(len(s)):
    if s[i].isdigit():
    summ+=int(s[i])*(17**i)
    else:
    summ+=d[s[i]]*(17**i)
    print(summ)

Leave a Reply

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