Hello,
Please help me in this question :
"Write a program that reads a nonnegative integer and computes and prints its factorial."
I don't understand the question and even internet doesn't help me either.
Please anyone help me before 11/4/2011.

답변 (6개)

rana ali
rana ali 2017년 4월 17일
편집: Walter Roberson 2019년 6월 8일

3 개 추천

n=input('the value of n: ');
f=1;
for i=1:n
f=f*i;
end
disp('factorial is')
disp(f)

댓글 수: 2

John D'Errico
John D'Errico 2017년 4월 17일
I deleted your virtual duplicate (OTHER) answer. In the future, if you want to modify an answer, then edit it.
Walter Roberson
Walter Roberson 2017년 4월 17일
For 23, this code produces 25852016738884978212864 instead of the correct 25852016738884976640000

댓글을 달려면 로그인하십시오.

Matt Fig
Matt Fig 2011년 4월 9일

2 개 추천

What do you mean you don't understand the question? I find this very difficult to believe. You know what it is to write a program, right? You know what it is to compute something, then print it? Even if you don't know what a factorial is, the internet surely could help. Here:
Please think it through, do some research, then clarify what you don't understand. Folks here are glad to help when the asker (especially a homework doing asker) puts out some effort and keeps the topic to specific MATLAB questions.

댓글 수: 2

Dungun_Ganu
Dungun_Ganu 2011년 4월 9일
편집: Walter Roberson 2019년 6월 8일
Right,
I found it in internet and programmed it like this :
n=5;
factorial=1;
for i=1:1:n
factorial=factorial*i;
end
After that, I run it and computed ">> factorial",
The output gave ;
factorial =
120
Just like it's theory, 5!=120.
Is it correct the way I programmed it?
By the way, sorry for my poor grammar since I'm not English's native speaker.
Matt Fig
Matt Fig 2011년 4월 9일
Your code looks good, and Walter points out the infeasibility of fulfilling the requirements as written (or at least as relayed).
I would also point out that naming your M-file FACTORIAL will mask the built-in MATLAB function by the same name.

댓글을 달려면 로그인하십시오.

Walter Roberson
Walter Roberson 2011년 4월 9일

1 개 추천

Although your proposed code looks good, you will find that it does not produce correct answers for n=18 or above. Your program is required to handle all non-negative integers, not just 1 to 17. You might perhaps be able to negotiate that your program does not have to handle any integers greater than about 10^308.
You are, though, going to run in to considerable difficulties long before that -- for example, (2^27)! is approximately 8.35*10^1032606161 and there aren't enough elementary particles in the Universe to be able to write that number out exactly.
Mohammed Ali
Mohammed Ali 2017년 12월 25일
편집: Image Analyst 2017년 12월 25일

0 개 추천

Write a program to calculate the factorial N!: a. N!=1, N=0 b. N!=N*(N‐1)*(N‐2)...*3*2*1, N>0
n=input('enter n:')
fact=1
if(n==0)
fact=1
for i=n:1:-1
fact=fact*i
end
end

댓글 수: 1

Image Analyst
Image Analyst 2017년 12월 25일
Nope, that won't do it. Did you actually try it?
It's even wrong in more ways than one.

댓글을 달려면 로그인하십시오.

duoduo chen
duoduo chen 2019년 5월 9일
편집: Walter Roberson 2019년 5월 9일

0 개 추천

n=input('Enter the value of n:');
factorial=1;
if n<0
fprintf('Error!\n');
elseif n==0
factorial=1;
fprintf('factorial=%d\n',factorial);
else
for ii=1:n
factorial=factorial*ii;
end
disp(['factorial=' int2str(factorial)]);
end

댓글 수: 3

Walter Roberson
Walter Roberson 2019년 5월 9일
What does your code have to say about 23 factorial?
Stephen23
Stephen23 2019년 5월 9일
Note that the elseif n==0 section is entirely superfluous.
You can start the loop iterations from 2 rather than 1.
As mentioned by Loginatorist eight years ago, the name factorial should be avoided.
YILMAZ TUNAHAN YILMAZ
YILMAZ TUNAHAN YILMAZ 2022년 8월 11일
dayı adamsın 3 saattir bununla uğraşıyoruz kralsın bitanesin

댓글을 달려면 로그인하십시오.

Lawrence Liu
Lawrence Liu 2019년 6월 8일

0 개 추천

function val=factorial(n)
if (n==0)
val=1;
else
val=n*factorial(n-1);
end
end

댓글 수: 1

Walter Roberson
Walter Roberson 2019년 6월 8일
This will fail when n exceeds 50, giving an error about recursion limit.
It will, as indicated in other responses, start producing incorrect answers long before that.
It will run indefinitely when asked to process a negative input.

댓글을 달려면 로그인하십시오.

질문:

2011년 4월 9일

댓글:

2022년 8월 11일

Community Treasure Hunt

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

Start Hunting!

Translated by