Clear Filters
Clear Filters

make C code to be a matlab code about LCM, please help me

2 views (last 30 days)
here is the C code
#include <stdio.h>
int main()
{
int m, n, a, b, t, hc, lc; // variable declaration
printf(" Enter two numbers: ");
scanf("%d%d", &a, &b);
m = a;
n = b;
while (n != 0)
{
t = n;
n = m % n;
m = t;
}
lc = (a*b)/hc; // lcm
printf(" The Least Common Multiple of %d and %d = %d\n", a, b, lc);
return 0;
}
then here is my matlab code
clear all; close all; clc;
a = input('first number = ')
na = numel(a);
b = input('second number = ')
nb = numel(b);
m = na;
n = nb;
while n~=0
t =n;
n =mod(m,n);
m =t;
end
lcm = na*nb/m;
fprintf('lcm is %d.', lcm);
please help correct me if I'm wrong

Answers (2)

Guillaume
Guillaume on 18 Jun 2018
Edited: Guillaume on 18 Jun 2018
if I'm wrong
Why can't you find that out yourself by testing your code with a few different values?
Have a look at the documentation of numel. Does it do what you meant it to do? Note that input the way you use it already returns a numeric value, so there's nothing special to do to a or b.

Ankita Bansal
Ankita Bansal on 18 Jun 2018
Edited: Ankita Bansal on 18 Jun 2018
Hi, numel(a) returns number of elements in a vector or matrix a. Remove the lines na=numel(a) and nb=numel(b) and modify your code to
clear all;
close all;
clc;
a = input('first number = ')
b = input('second number = ')
m = a;
n = b;
while n~=0
t =n;
n =mod(m,n);
m =t;
end
lcm = a*b/m;
fprintf('lcm is %d.', lcm);
You can also remove fprintf('lcm is %d.', lcm); and change " lcm = a*b/m; " to " lcm = a*b/m " to print the value of lcm in command window.

Categories

Find more on MATLAB in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!