Balanced number in MATLAB
Given a positive integer find whether it is a balanced number. For a balanced number the 
sum of first half of digits is equal to the second half.
Examples:
Input n = 13722
Output tf is true
because 1 + 3 = 2 + 2.
Input n = 23567414
Output tf = true
You can check the question in the below link also:
https://www.mathworks.com/matlabcentral/cody/problems/74
Sample TEST CASE Output:
You can try with other input also :-)
sum of first half of digits is equal to the second half.
Examples:
Input n = 13722
Output tf is true
because 1 + 3 = 2 + 2.
Input n = 23567414
Output tf = true
You can check the question in the below link also:
https://www.mathworks.com/matlabcentral/cody/problems/74
MATLAB CODE:
clc
clear all
close all
x=input('Enter the number');
Y=[];
c=0;
m=x;
while (m>0)
    b=rem(m,10);
    Y=[b Y];
    c=c+1;
    m=(m-b)/10;
end
r=c/2;
qw=floor(r);
if(qw==r)
t=0;
l=0;
for i=1:qw
    t=t+Y(i);
end
for i=(qw+1):c
    l=l+Y(i);
end
if(t==l)
    disp('Yes');
else
    disp('No');
end
else
   t=0;
l=0;
for i=1:qw
    t=t+Y(i);
end
for i=(qw+2):c
    l=l+Y(i);
end
if(t==l)
    disp('Yes');
else
    disp('No');
end 
end
EXPLANATION:
IMPLEMENTATION OF THE CODE AS FUNCTION:
function tf = isBalanced(n)
x=n;
Y=[];
c=0;
m=x;
while (m>0)
    b=rem(m,10);
    Y=[b Y];
    c=c+1;
    m=(m-b)/10;
end
r=c/2;
qw=floor(r);
if(qw==r)
t=0;
l=0;
for i=1:qw
    t=t+Y(i);
end
for i=(qw+1):c
    l=l+Y(i);
end
if(t==l)
    tf=true;
else
    tf=false;
end
    else
   t=0;
l=0;
for i=1:qw
    t=t+Y(i);
end
for i=(qw+2):c
    l=l+Y(i);
end
if(t==l)
    tf=true;
else
    tf=false;
end 
end
You can try with other input also :-)
 
 
 
 
 
 
 
%206th%20Edition,%20Kindle%20Edition.jpg) 
 
 
 Posts
Posts
 
 
 
 
 
 
 
 
No comments