Write a code without using ceil or any other rounding commands (round, etc...) calculate ceil(x), where x is a positive number chosen by the user. USING MATLAB!
x = input('Enter a positive number: ');
What we can do is multiply the number by 10 store in a variable, keep adding 1 to it until mod(variable,10) becomes 0. Then print the variable/10.
Example :
Input = 11.8
temp = 11.8*10 = 118
mod(118,10) = 8
118+1 = 119
mod(119,10) = 9
119+1 = 120
mod(120,10) = 0 => So, print 120/10 = 12
CODE :
clear all; close all; clc;
x = input('Enter a positive number: ');
temp = x*10;
r = mod(temp,10);
while i<10
if r==0
disp(temp/10);
break;
else
temp = temp+1;
r = mod(temp,10);
i=i+1;
end
end
Get Answers For Free
Most questions answered within 1 hours.