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

2 vues (au cours des 30 derniers jours)
Fauzi Nuryasin
Fauzi Nuryasin le 18 Juin 2018
Modifié(e) : Ankita Bansal le 18 Juin 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

Réponses (2)

Guillaume
Guillaume le 18 Juin 2018
Modifié(e) : Guillaume le 18 Juin 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 le 18 Juin 2018
Modifié(e) : Ankita Bansal le 18 Juin 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.

Catégories

En savoir plus sur MATLAB dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by