Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Dec 2011
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Unhappy MATLAB code to display prime numbers?

    Can you guys please help me with a MATLAB code, I need a simple program to display the prime numbers between 1 and 100. Please help as I am new to programming and I have to do it as an assignment.

  • #2
    Senior Coder alykins's Avatar
    Join Date
    Apr 2011
    Posts
    1,760
    Thanks
    41
    Thanked 191 Times in 190 Posts
    it has been a while since I have done MATLAB (and I did not do a whole lot in it but I will help you with the logic flow so you can figure it out) You cannot post questions on "how do I do this" for assignments, but you can ask for help on why things are not working... this pseudo code may help you
    Code:
    int[] myNums = new int[100]
    i=1
    totalprime =0
    while (i<=100){
      bool eval = true
      cnt = 0
      while(eval = true){
         for(int y=1; y<=i; y++)
         if(i%y != 0) eval = false
         else cnt++
         if (y==i) eval false
      }
      if(cnt == 2) myNums[totalprime] = i
      i++
    }
    i think that logic flow is correct... take a look at it and tell me if you can figure out what it is doing... a key to doing this is going to be modulus

    I code C hash-tag .Net
    Reference: W3C W3CWiki .Net Lib
    Validate: html CSS
    Debug: Chrome FireFox IE


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •