Question

In: Computer Science

Everything else works except the range, I only want the numbers entered to be in between...

Everything else works except the range, I only want the numbers entered to be in between 2-70 but I can enter 700 for some reason. How can I make the limit work? (This is in C#)

namespace Prime_Numbers
{
class Program
{
static void Main(string[] args)
{
int limit; // Declare limit as an int as we do not need double.

Console.WriteLine("*****************************\nPRIME NUMBERS UP TO 25\n*****************************");

Console.Write("Enter a limit to the prime numbers you want displayed: ");

while (true) {

string input = Console.ReadLine(); // Convert number to a string.
Int32.TryParse(input, out limit);
if (!Int32.TryParse(input, out limit)) // See if we can parse the 'text' string. If False:
{

Console.WriteLine("Input must be a whole number. Please try again.");
}

else if (limit <= 70 || limit >= 2) // If True run the program:
{
Console.WriteLine("Calculating...");
for (int i = 1; i <= limit; i++) // Main loop for the prime number rows.
{
if (checkPrime(i))
{
for (int j = 0; j < i; j++) //For space after "X" has printed.
{
Console.Write("*");
}

Console.WriteLine(" " + i); // Adds prime number onto string output.
}
}

break;
}
  
else // If the number is out of range:
{
Console.WriteLine("The value entered must be between 2 and 70.");
}
}

Console.ReadKey();
}

static bool checkPrime(int n) // Create function to check for prime values.
{
// Check if n is 0 or 1.
if (n == 0 || n == 1)
{
return false;
}

else
{
// Loop from 2 to n / 2,
for (int i = 2; i <= n / 2; i++)
{
// If any number is divisible by i then it is not a prime number
// Assign the result of a boolean expression to n.
if (n % i == 0)
{
return false;
}

}

return true;

} //end of else
}

}

}


Solutions

Expert Solution

Code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Prime_Numbers
{
class Program
{
static void Main(string[] args)
{
int limit; // Declare limit as an int as we do not need double.
Console.WriteLine("*****************************\nPRIME NUMBERS UP TO 25\n*****************************");
Console.Write("Enter a limit to the prime numbers you want displayed: ");
while (true)
{
string input = Console.ReadLine(); // Convert number to a string.
Int32.TryParse(input, out limit);
if (!Int32.TryParse(input, out limit)) // See if we can parse the 'text' string. If False:
{
Console.WriteLine("Input must be a whole number. Please try again.");
}
else if (limit <= 70 && limit >= 2) // If True run the program:
{
Console.WriteLine("Calculating...");
for (int i = 1; i <= limit; i++) // Main loop for the prime number rows.
{
if (checkPrime(i))
{
for (int j = 0; j < i; j++) //For space after "X" has printed.
{
Console.Write("*");
}
Console.WriteLine(" " + i); // Adds prime number onto string output.
}
}
break;
}

else // If the number is out of range:
{
Console.WriteLine("The value entered must be between 2 and 70.");
}
}
Console.ReadKey();
}
static bool checkPrime(int n) // Create function to check for prime values.
{
// Check if n is 0 or 1.
if (n == 0 || n == 1)
{
return false;
}
else
{
// Loop from 2 to n / 2,
for (int i = 2; i <= n / 2; i++)
{
// If any number is divisible by i then it is not a prime number
// Assign the result of a boolean expression to n.
if (n % i == 0)
{
return false;
}
}
return true;
} //end of else
}
}
}

output

If you have any query regarding the code please ask me in the comment i am here for help you. Please do not direct thumbs down just ask if you have any query. And if you like my work then please appreciates with up vote. Thank You.


Related Solutions

Hello! I am stuck on only B-1 Recession EPS - I got everything else and I...
Hello! I am stuck on only B-1 Recession EPS - I got everything else and I cannot figure out what I'm doing wrong - I keep getting $4.81, but it's incorrect and I don't know why. Can you help? I pasted my numbers below the question. Sunrise, Inc., has no debt outstanding and a total market value of $240,900. Earnings before interest and taxes, EBIT, are projected to be $40,000 if economic conditions are normal. If there is strong expansion...
Please assist with getting my code to correctly check to validate input. Everything else works fine,...
Please assist with getting my code to correctly check to validate input. Everything else works fine, but when I enter a character, it should say Invalid entry. Everything else should also work. It should not allow more or less than 9 digits, and it should ask again for input if it doesn't like the input. Please help my code and explain how it is working. I also need to keep the recursive function. #include <stdio.h> #include <ctype.h > int SecNumSum(long...
Having trouble with the Luhn algorithm for java. Everything works great except for when cthe number...
Having trouble with the Luhn algorithm for java. Everything works great except for when cthe number 5105105105105109 is entered. I'm sure there are other numbers that do this, but this returns check digit as 10 when it should come back as 0. Suggestions? here is the algorithm and then my current code without the prints because my error is in the math. I am not allowed to use break. Another key part of the credit card number standard is the...
This is the exact post of the assignment i double checked the numbers and everything is...
This is the exact post of the assignment i double checked the numbers and everything is correct Tough Steel, Inc. is a processor of stainless steel products. The firm is considering replacing an old stainless steel tube-making machine for a more cost-effective machine that can meet the firm’s quality standards  The old machine was acquired 2 years ago at an installed cost of $500,000. It has been depreciated under the MACRS’s 5-year recovery period, and has a remaining economic...
You are considering between two loans. Assume everything between these two loans is the same except...
You are considering between two loans. Assume everything between these two loans is the same except for the interest rate. Loan A offers 5.5% compounded weekly. Loan B offers 5.64% compounded semiannually. Which loan is better and why? Loan A because the actual rate is 5.65% is lower than Loan B’s actual rate. Loan A because 5.5% is lower than Loan B’s 5.64%. Loan B because the actual rate is 5.72% is higher than Loan A’s actual rate. Loan B...
I am writing a lab report about (Simple Pendulum) and I did everything except( the Review...
I am writing a lab report about (Simple Pendulum) and I did everything except( the Review of Literature and the methods) Please type it if not make sure on a clear hand writing I got a answer but the methods was not there I need it as soon as possible thanks
Write everything you know about EMG not WRITTEN i want it TYPED using Microsoft word between...
Write everything you know about EMG not WRITTEN i want it TYPED using Microsoft word between 2 to 3 pages
I want to talk about the relevant range is the range :Relevant range::Cost behaviorrelevant range:of the...
I want to talk about the relevant range is the range :Relevant range::Cost behaviorrelevant range:of the volume of activity where total fixed costs remain constant and the variable cost per unit remains constant. Can someone provide an example?
In the end, all the characters in Everything I Never Told You survive-- except Lydia. James...
In the end, all the characters in Everything I Never Told You survive-- except Lydia. James and Marilyn reconcile, emotionally and physically. Hannah begins to receive love and appreciation from her mother especially. Nath gets to punch out Jack, go to Harvard, and eventually (as one line of the novel reveals) becomes an astronaut. But Lydia dies. Why was she the family's fulcrum for suffering in the years leading up to, and why did she drown in the lake that...
I want to know everything there is to know about wireless networking, in particular, how to...
I want to know everything there is to know about wireless networking, in particular, how to connect a device to 5G cellular networks, also looking at NFC, GPS, all that would be helpful too
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT