Borrar filtros
Borrar filtros

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

2 visualizaciones (últimos 30 días)
Fauzi Nuryasin
Fauzi Nuryasin el 18 de Jun. de 2018
Editada: Ankita Bansal el 18 de Jun. de 2018
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

Respuestas (2)

Guillaume
Guillaume el 18 de Jun. de 2018
Editada: Guillaume el 18 de Jun. de 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 el 18 de Jun. de 2018
Editada: Ankita Bansal el 18 de Jun. de 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.

Categorías

Más información sobre MATLAB en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by