Write a code in the language of your choice that will list out all the prime numbers between 2 given numbers, and display the sum of list at the end. We will be looking at the quality of your code. You attention to details like validation. And your understanding to the requirement.
Vb 6 need help
Collapse
X
-
Tags: None
-
-
This one in ASP.NET but its not including sum up the prime numbers.
ArrayList primeNumbers = new ArrayList();
for(int i = 2; primeNumbers.Co unt < 10000; i++) {
bool divisible = false;
foreach(int number in primeNumbers) {
if(i % number == 0) {
divisible = true;
}
}
if(divisible == false) {
primeNumbers.Ad d(i);
Console.Write(i + " ");
}
}
This one in VB.net
Dim lowerLimit, upperLimit, primeNumber As Integer
Dim isPrime As Boolean = False
lowerLimit = CType(TextBox1. Text, Integer)
upperLimit = CType(TextBox2. Text, Integer)
While lowerLimit < upperLimit
primeNumber = lowerLimit
If primeNumber = 2 Or primeNumber = 3 Then
isPrime = True
ElseIf primeNumber = 1 Then
isPrime = False
ElseIf primeNumber = upperLimit Then
Exit While
Else
For i As Integer = 2 To (primeNumber - 1)
If primeNumber Mod i = 0 Then
isPrime = False
Exit For
Else
isPrime = True
End If
Next
End If
If isPrime = True Then
TextBox3.Text += primeNumber & vbCrLf
End If
lowerLimit += 1
End While
Note : As you can see, I used two textboxes for the lower and upper limit values and then when I found a prime number, I output it to a third multiline textbox. I skipped over 1-3 as one is of course not a prime number and then two and three do not fit well into the for loop with values. Which is why I started the for loop at 2 and looped up to the upperlimit minus 1. This is, of course, because a number is always divisible by 1 and itself (the definition of a prime number). If the loop ever catches a number that is divisble by another number with a 0 remainder it flags the number as non-prime and exits the loop. Otherwise it will continue looping through until it reaches one less than the upper limit.
GUYS ....HELP ME CHECK ON CODINGComment
Comment