Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

prime numbers c#

Tags:

c#

primes

I'm new to C#. And I would like to program something like, displaying the prime numbers in a listbox if user will input any integer in the textbox. (that means, if they write 10, it will display the prime numbers from 0-10, or 20 from 0-20, etc).

What should I consider first, before I do the programming? I know there are many examples in the internet, but first I would like to know what will I need?

Thanks for the tip;-)

=== Thanks guys. So you're suggesting that it's better to do it first in the Console application? I did an example of "For Loop" using Console Application a very simple one, but then when I tried to do it in the Windows Form Application, I'm not sure how to implement it. I'm afraid that if I keep doing examples in the Console, then I'll have difficulty to do it in Windows Form Apps. What do you think?

====== Hello again,

I need some feedback with my code:

        Console.WriteLine("Please enter your integer: ");
        long yourInteger;
        yourInteger = Int32.Parse(Console.ReadLine());

        //displaying the first prime number and comparing it to the given integer
        for (long i = 2; i <= yourInteger; i = i + 1)
        {
            //Controls i if its prime number or not
            if ((i % 2 != 0) || (i == 2))
            {
                Console.Write("{0} ", i);
            }

        }
like image 421
tintincutes Avatar asked May 20 '09 07:05

tintincutes


People also ask

What are prime numbers in C?

Prime number in C: Prime number is a number that is greater than 1 and divided by 1 or itself. In other words, prime numbers can't be divided by other numbers than itself or 1.

How do you check if a number is a prime number in C?

Prime Number Check Program in C i.e.if(5%1==0)then c is incremented, here 5%1=0 thus c is incremented. i.e. if(5%5==0) then c is incremented, 5%5=0 and so c is incremented. here i value is 6 and 6<=5 is false thus the condition fails and control leaves the for loop.

How many prime numbers are there between 1 and 1000 in C?

How many prime numbers are there in between 1 to 1000? There are a total of 168 prime numbers in between 1 to 1000.

What is perfect number in C?

Master C and Embedded C Programming- Learn as you go Perfect number is the number; whose sum of factors is equal to 2*number.


1 Answers

Well, first of all I'd think about how to find prime numbers, and write that in a console app that reads a line, does the math, and writes the results (purely because that is the simplest thing you can do, and covers the same parsing etc logic you'll need later).

When you are happy with the prime number generation, then look at how to do winforms - how to put a listbox, textbox and button on a form; how to handle the click event (of the button), and how to read from the textbox and write values into the listbox. Your prime code should be fairly OK to take "as is"...

If you don't already have an IDE, then note that C# Express is free and will cover all of the above.

like image 161
Marc Gravell Avatar answered Oct 17 '22 22:10

Marc Gravell