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

1 回表示 (過去 30 日間)
Fauzi Nuryasin
Fauzi Nuryasin 2018 年 6 月 18 日
編集済み: Ankita Bansal 2018 年 6 月 18 日
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.

カテゴリ

Help Center および File ExchangeGenerated Code Interfacing についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by