problem with adding two numbers
Afficher commentaires plus anciens
I am trying to convert fractional decimal number into binary. having done conversion with integer and fractional part I want to finally add them and show the result. I am getting following result. Please guide me what is wrong with the code?
clear all
clc
N = input('Enter the decimal number - ');
%B = input('Enter the base - ');
int = floor(N); %integer part
V = N - int; %decimal part
i = 1;
while(int > 0)
R(i) = rem(int,2);
int = floor(int/2);
i = i+1;
end
b_n = fliplr(R);
disp('Binary equivalent of number is ');
disp(b_n);
x = 0;
k = 0.1;
while(V ~= 0)
V = (V-floor(V))*2;
x = x + floor(x + V)*k;
k = k/10;
end
f_n = V + x;
disp(f_n);
C = b_n + f_n;
disp(C)
output
Enter the decimal number - 100.25
Binary equivalent of number is
1 1 0 0 1 0 0
0.0100
Columns 1 through 6
1.0100 1.0100 0.0100 0.0100 1.0100 0.0100
Column 7
0.0100
1 commentaire
Wycliff Dembe
le 28 Août 2018
Réponses (0)
Catégories
En savoir plus sur Logical dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!