Interview Question


Country: India
Interview Type: Written Test




Comment hidden because of low score. Click to expand.
0
of 0 vote

Writing jus the algorithm

float num;
int before decimal = (int) num;
float after_decimal = num - before-decimal;

//find the binary val of before_decimal ... very straight forward. 
// For floating values multiply by two and write the number which comes before decimal point in output .. If the result is greater than 1 substract 1 and continue  then pevious step till you get result 0 

For example 0.125 

0.125 * 2 = 0.250 ....        o/p = 0  
0.250 * 2 = 0.50               o/p  = 0 
0.50 * 2 =1.00                  op = 1 
1.00 -1 =0.00  Leave here .. 

so binary is 0.001

- Shivaprasad May 13, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
#include<math.h>
using namespace std;

int main()
{
    char s[5],d[5];
    int pos=0,k=0,flag=0,n,m;
    cin>>s;//enter number as string
    n=atoi(s);//converting string to integer leaving the decimal part
    for(int i=0;i<strlen(s);i++)//loop to extract the decimal part
    {
            if(s[i]=='.' && flag!=1)
            {pos=i;flag=1;}
            else if(flag==1)
            {
                   if(s[i]!='.')
                  d[k++]=s[i];     
                  else
                  {cout<<"\nwrong input"<<endl;return(0);}
            }
            
            
    }
    //n is the integer part of string s and m is the integer part of d(the decimal nos)
    if(flag==1)
    {m=atoi(d);}
    else
    m=0;
    
    int x=n,rem,ans[100],u=0;
    while(x!=0)//converting int part to binary
    {rem=x%2;
    x=x/2;
    ans[u++]=rem;
    }
    for(int i=u-1;i>=0;i--)
    cout<<ans[i];    
    if(m!=0)
    {
            cout<<".";
            
            float p,q,t;
            q=m/pow(10,strlen(d));//converting decimal part as float
            p=q;
            while(p!=0)//converting decimal part to binary
            {
                       p=p*2;
                       t=int(p);
                       p=p-t;
                       
                       cout<<t;
            }
            
            
            
    }
    
    
    getch();
    return(0);
}

- nachu May 27, 2012 | Flag Reply


Add a Comment
Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

Books

is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

Learn More

Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.

Learn More

Resume Review

Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

Learn More

Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.

Learn More