Click here to Skip to main content
15,905,867 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i'am using this code code
C#
protected void Button1_Click(object sender, EventArgs e)
        {

            int z = Convert.ToInt32(TextBox1.Text);
            for (int x = 2; x <= z; x++)
            {
                int isprime = 0;
                for (int y = 1; y < x; y++)
                {
                    if (x % y == 0)
                        isprime++;
                    if (isprime == 2) break;

                }
                if (isprime != 2)
                    Label2.Text =Convert.ToString(x);
                isprime = 0;
               
            }
           
        }


What I have tried:

In this case biggest prime number is coming from the output,but i need all prime numbers up to maximum number printing in the output?.
what changes i'am taking to get my required output?

regards,
nagarjuna
Posted
Updated 15-May-16 23:25pm
Comments
Karthik_Mahalingam 16-May-16 4:55am    
How do you want to show all those numbers in the from?
Nagarjuna99 16-May-16 4:59am    
in that case only the biggest number was print in the output, i want to print all the numbers up to enter number
Karthik_Mahalingam 16-May-16 5:01am    
yes but how do you want to display in the screen ?
Richard MacCutchan 16-May-16 5:00am    
You need to create a list in your form, and add each number to the list as you validate it. You could use a ListBox, or a TextBox, in the latter case building a string of all the numbers before you display them.
Nagarjuna99 16-May-16 5:03am    
is its possible to print in the pyramid shap

When you write:
C#
Label2.Text =Convert.ToString(x);

Inside a loop, you will only ever get teh last value to used, just teh same as if you said:
C#
int x = 0;
for (int i = 0; i < 10; i++)
   {
   x = i;
   }
X will always equal the final value in the loop: 9.
You can do what you want with strings:
C#
Label2.Text += Convert.ToString(x);
But that's not a good idea, as string are immutable - every time you add to a string you create a new string, and copy the old and new data into it. That's pretty inefficient, particularly when you generate a lot of numbers!
A better solution is to use a StringBuilder:
C#
StringBuilder sb = new StringBuilder();
string sep = "";
for (int i = 0; i < 10; i++)
    {
    sb.AppendFormat("{0}{1}", sep, i);
    sep = ", ";
    }
And then use the output of the that after the loop:
C#
Label2.Text = sb.ToString();


But a couple of details to add:
1) Don't use Convert for input or output - it has a place, but this isn't it! When you use Convert.ToInt32 on user input, you app will crash if the user mistypes.
A much better solution here is to use TryParse:
C#
int z;
if (!int.TryParse(TextBox1.Text, out z))
   {
   // User mistyped
   report problem to user
   return;
   }
And there is no need to use Convert for output:
C#
... = x.ToString;
Is a lot clearer!
2) Do yourself a favour, and stop using Visual Studio default names for everything - you may remember that "TextBox8" is the mobile number today, but when you have to modify it in three weeks time, will you then? Use descriptive names - "tbMobileNo" for example - and your code becomes easier to read, more self documenting, easier to maintain - and surprisingly quicker to code because Intellisense can get to to "tbMobile" in three keystrokes, where "TextBox8" takes thinking about and 8 keystrokes...
3) Stop using single character variable names! Using descriptive names which explain what the variable is used for makes your code a lot easier to read and understand (and generally is as quick as single character ones thanks to Intellisense).
 
Share this answer
 
Comments
Nagarjuna99 16-May-16 5:24am    
using this
Label2.Text += Convert.ToString(x);,
i get the output ,but i need to add , in two numbers who it is possible?
OriginalGriff 16-May-16 5:35am    
Do you want to try explaining that again, this time in more words?

Remember that we can't see your screen, access your HDD, or read your mind! :laugh:
Nagarjuna99 16-May-16 5:37am    
i need to add camma(,) between to primenumbers is it's possible?
Nagarjuna99 16-May-16 5:39am    
i got the solution ,thanks for explain this
try this, refer Solution 1 for the explanation..

C#
 if (isprime != 2)
    ListBox1.Items.Add(x.ToString());
isprime = 0;
 
Share this answer
 
v2
Comments
Nagarjuna99 16-May-16 5:34am    
thanks, its added all prime numbers up to max value.
In other case i'am using this code: Label2.Text += Convert.ToString(x);
its also working ,but numbers printed row, i need to add camma(,) between to numbers?
Karthik_Mahalingam 16-May-16 5:37am    
for that explanation you can refer Solution1

try, Label2.Text += Convert.ToString(x) + ",";

however you will be having a comma after the last number.
you can remove that by adding the below line at the last line of the button click event

Label2.Text = Label2.Text.TrimEnd(',');
Nagarjuna99 16-May-16 5:39am    
once again thanks
Karthik_Mahalingam 16-May-16 5:40am    
welcome

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900