- ahmad alhayek

 please answer it in C++

In this question, you are required to write a program that takes as an input any decimal number (with fractional part) and th
Method 1: In this method, you have to split the fractional part from the integer part (if the number has an integer part), an
Method 2 The digits of any integer number are numbered 1,2,3 from right to left, while the digits of any fractional number ar
Sample Test Case o Input: 0.6875 3 Output: 101 Sample Test Case 1 Input: 0.67 6 Output: 101010 Sample Test Case 2 Input: 2.67
Show transcribed image text

Expert Answerinformation icon

  • Anonymous's Avatar

    This is the code : read comments carefully i have mentioned each step in comments:

    I used the method one : First discard the integral part and then doing operation with fractional part :
    Code:

    #include <bits/stdc++.h>
    using namespace std;
    #define ll long long
    #define nline "\n"
    #define mod (ll)(1e9+7)
    // Function to convert decimal to binary upto
    // k-precision after decimal point
    string decimalToBinary(double num, int k_prec)
    {
    string binary = ""; // i used this string to store in a binary format
      
    // Fetch the integral part of decimal number
    int Integral = num;
      
    // Fetch the fractional part decimal number
    double fractional = num - Integral;

    // Conversion of fractional part to
    // binary equivalent
    while (k_prec--)
    {
    // Find next bit in fraction
    fractional *= 2; // this will do multiplication
    int fract_bit = fractional; // if fraction become 1.675 (assume) int actually take 1 here and discarded fraction.
      
    if (fract_bit == 1) // if its equal one just add into a string one into it
    {
    fractional -= fract_bit; //remove those bit here
    binary.push_back(1 + '0'); // update our string
    }
    else
    binary.push_back(0 + '0');
    }
      
    return binary;
    }
    int main() {
    double number;
    cin>>number;
    int k;
    cin>>k;
    number=number-(int)(number); // remove integral part only fraction part be there .
    if(number<0){
    cout<<"invalid";
    }
    else{
    cout<<decimalToBinary(number,k)<<endl;
    }
    }

    Code:With less comments.

    #include <bits/stdc++.h>
    using namespace std;
    #define ll long long
    #define nline "\n"
    #define mod (ll)(1e9+7)
    // Function to convert decimal to binary upto
    // k-precision after decimal point
    string decimalToBinary(double num, int k_prec)
    {
    string binary = "";
      
    // Fetch the integral part of decimal number
    int Integral = num;
      
    // Fetch the fractional part decimal number
    double fractional = num - Integral;

    // Conversion of fractional part to
    // binary equivalent
    while (k_prec--)
    {
    // Find next bit in fraction
    fractional *= 2;
    int fract_bit = fractional;
      
    if (fract_bit == 1)
    {
    fractional -= fract_bit;
    binary.push_back(1 + '0');
    }
    else
    binary.push_back(0 + '0');
    }
      
    return binary;
    }
    int main() {
    double number;
    cin>>number;
    int k;
    cin>>k;
    number=number-(int)(number);
    if(number<0){
    cout<<"invalid";
    }
    else{
    cout<<decimalToBinary(number,k)<<endl;
    }
    }

    Comment 

ليست هناك تعليقات:

إرسال تعليق

ahmad alhayek تصميم ahmad alhayek جميع الحقوق محفوظة 2016

صور المظاهر بواسطة sndr. يتم التشغيل بواسطة Blogger.