01温度转换
C = input('Enter a temperature in Celsius:');
F = (C*1.8)+32;
fprintf('Fahrenheit=%f\n',F)
02 加法计算,格式化输出
x = input('Please enter x:');
y = input('Please enter y:');
fprintf('%g + %f = %g',x,y,x+y);
03 简单分支判断正负
num = input('Enter a number:');
if num > 0
fprintf('Positive\n');
elseif num < 0
fprintf('Negative\n');
else
fprintf('num = 0\n');
end
04 简单分支比较大小
a = input('a=');
b = input('b=');
if a>b
fprintf('%g is greater\n',a);
else
fprintf('%g is greater\n',b);
end
05 简单分支判断是否为三角形
a = input('Enter the value of a:');
b = input('Enter the value of b:');
c = input('Enter the value of c:');
if (a+b)>c && (a+c)>b && (b+c)>a
fprintf('Yes\n');
else
fprintf('No\n');
end
06 简单分支判断方程根的情况并求解方程
a = input('Enter the value of a:');
b = input('Enter the value of b:');
c = input('Enter the value of c:');
delta = b^2 - 4*a*c;
if delta > 0
fprintf('2 solutions\n');
x1 = (-b + sqrt(delta) )/(2*a);
x2 = (-b - sqrt(delta) )/(2*a);
fprintf('x1 = %f\n',x1);
fprintf('x2 = %f\n',x2);
elseif delta == 0
fprintf('1 solution\n');
x = (-b)/(2*a);
fprintf('x = %f\n',x);
else
fprintf('no solution\n');
end