Jump to content

Need help writing a Code In Matlab

Neil96

Hello, I have to make a program in Matlab.

 

Write 2 programs that calculate the factorial of a user-entered number between 0 and 20.

 

One should use a for loop and the other should use a while loop as the main logical structure.

 

1. Prompt the user to enter a number to be factorialized

2. Return an error message if it is less than 0 or greater than 20

3. Compute and display the result

 

If anybody can help me out, that would be great. Not really good at programming/ coding.

 

Thank you.

Link to comment
Share on other sites

Link to post
Share on other sites

Programming is very much like a list of directions you give a program. If you can write a list of directions, you can make a program.

 

Since I'm assuming this is homework, I'm not going to provide any source code (plus I'm not too familiar with Matlab anyway), I'm just going to tell you what to look for in your reference materials (use Google)

 

45 minutes ago, Neil96 said:

1. Prompt the user to enter a number to be factorialized

https://www.mathworks.com/help/matlab/ref/input.html

Quote

2. Return an error message if it is less than 0 or greater than 20

Use an if statement (I know they're in there) to check the value.

Quote

3. Compute and display the result

I'm going to assume you know what a factorial is, but I'm going to assume you're not familiar with loops. Displaying can be found at https://www.mathworks.com/matlabcentral/newsreader/view_thread/241136

 

A loop in general is a series of instructions that the program will repeat until told otherwise, called the exit condition. There are a few ways to do this but the most common is the for loop and while loop.

 

A for-loop is more for a well defined number of iterations. It usually specifies a variable that holds the iteration and a range of iterations of some sort. For example, let's take this for-loop declaration.

FOR X IN RANGE 1:20

This means that this loop will iterate 20 times (1 through 20), with X holding the current iteration number. Of course, it's starting points can be arbitrary. You could substitute the start and end positions with a variable.

 

That's a more simpler for-loop declaration. Some languages, like C, define a starting state, an exit condition, and how the iteration should work:

for(x = 1; x < 20; x++)

This is basically the same kind of for loop as the one I described earlier: it will iterate 20 times, 1 through 20, with x holding the current iteration number. Or in another way of saying, with X starting at 1; as long as X is less than 20, keep looping; increment X by 1.

 

A while-loop on the other hand, only defines an exit condition. This is more useful for loops that have an unknown number of iterations. So for the same 20 iteration loop, we would have something like this:

WHILE X < 20
  'Do something.
  X = X + 1

There are just two issues with while-loops

  • The variable you are using for your exit condition must be defined/declared and have a valid value beforehand
  • The variable you are using for your exit condition must be modified, otherwise the program will loop forever because the exit condition variable doesn't change.

 

As you can see, you achieve the same kind of loop with while or for loops, but you should be using one or the other depending on your situation.

  • When to use for-loops: Your algorithm has a well-defined number of iterations. This could be things like repeating the same steps or going through a non-changing collection of something.
  • When to use while-loops: Your algorithm has does not have a well-defined number of iterations. A common example of this is if you have a menu that you want the user to input an entry. You don't know when the user is going to exit the application, just that some input means to quit. Just remember the two issues described previously though.
Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×