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

1 次查看(过去 30 天)
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

回答(2 个)

Guillaume
Guillaume 2018-6-18
编辑:Guillaume 2018-6-18
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 2018-6-18
编辑:Ankita Bansal 2018-6-18
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.

Community Treasure Hunt

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

Start Hunting!

Translated by