Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Code Block
penger = input('Hvor mye penger har du? ');
sjokolade = 30;
smaagodt = input('Hvor mye koster smågodtet ditt? ');
 
surtSmaagodt = False/True;
if penger>(sjokolade + smaagodt)
    if surtSmaagodt
        fprintf('Woho! Dette blir en bra kveld!')
    else
        fprintf('Gå hjem og se på netflix')
    end
else
    fprintf('Du har ikke råd til godteriet :(')
end

 

 

Oppgave 5

a)

Code Block
for x = 1:100
    if mod(x,4)==0
        fprintf('%i ',x)
    end
end

 

b)

 


Code Block
x = input('Skriv inn en nedre grense for intervallet, x: ');
y = input('Skriv inn en øvre grense for intervallet, y: ');
antall = 0; 
for i = x:y
    if mod(i,3)==0
        antall = antall + 1;  
    end
end
fprintf('%i tall i intervallet er delelige på 3.', antall)


 

c)

Code Block
sum = 0;
while sum <= 100
    tall = input('Skriv inn et tall: ');
    sum = sum + tall;
end
fprintf('Summen av alle tallene har oversteget 100')

 

d)

Code Block
sum = 0;
while sum <= 100
    tall = input('Skriv inn et tall: ');
    sum = sum + tall;
    if mod(tall,9)==0
        break
    end
end
fprintf('Summen av alle tallene ble: %i', sum)
  

 

Oppgave 6

a)

Code Block
function prod = fac(n)
prod = 1;
    for x = 2:n
        prod = prod*x;
    end
end

 

b)