Matalb 基础运用分支

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

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值