Question

In: Computer Science

answer the following using C# Design and program a Visual Studio Console project in C# that...

answer the following using C#

Design and program a Visual Studio Console project in C# that allows your user to enter a number. The program will examine the number to see if it is prime. If it is prime, it will print the next higher prime and the next lower primes to the console. If the number entered by the user is not prime, display a message to that effect.

All code should be written by you. Do not copy/paste any code from any other source.

Assume the input will be within the range 2 to (2^31-1)

You should not validate the user input.

Solutions

Expert Solution


using System;
using System.Dynamic;

namespace prime
{
class Program
{
static void Main(string[] args)
{
//Declaring variables flag is for finding prime in first loop
//flag is for finding lower prime
//flag is for finding higher prime
int value, flag = 0, flag1 = 0, flag2 = 0;
Console.WriteLine("Enter value: ");
//Reading value from user
value = int.Parse(Console.ReadLine());
//Asking the user to enter value greater than zero
if (value <= 0)
Console.WriteLine("Enter value greater than zero");
else
{
//To check whether given value is prime or not
for (int i = 2; i < value; i++)
{
//If any of the value which is less than user value modulous equal to zero then break
if (value % i == 0)
{
flag = 1;
break;
}
}
if (flag == 0)
{
//Finding lower prime value
for(int j = value-1; j >= 1; j--)
{
int prime = 0;
for(int k=2; k < j; k++)
{
if (j % k == 0)
{
prime = 1;
break;
}
}
//This condition to check no lower value is presenty
if (prime == 0)
{
flag1 = 1;
Console.WriteLine($"The next lower prime is {j}");
break;
}
}
if (flag1 == 0)
{
Console.WriteLine("There is no lower prime for the given value");
}
//Finding higher prime value
for(int j=value+1;Math.Abs(j)<= 2147483647; j++)
{
int prime = 0;
for (int k = 2; k < j; k++)
{
if (j % k == 0)
{
prime = 1;
break;
}
}
if (prime == 0)
{
flag2 = 1;
Console.WriteLine($"The next higher prime is {j}");
break;
}
}
if (flag2 == 0)
{
Console.WriteLine("There is no higher prime for the given value");
}
}
else
Console.WriteLine("The given vaue is not a prime");

}
}
}
}
Sample Input and output:

Enter value:
2147483647
The next lower prime is 2147483629
The next higher prime is -2147483648

//If you observe the above condition for the highest value which is 2^31 - 1 has lower prime and higher prime is negative value. This is because range of int from -2147483648 to 2147483647. To avoid these situation I added Math.abs value to above code which is in bold.

Enter value:
2147483647
The next lower prime is 2147483629

//If you apply Math.Abs then below exception will be raised.

//System.OverflowException: 'Negating the minimum value of a twos complement number is invalid.'

Enter value:
37
The next lower prime is 31
The next higher prime is 41

Enter value:
7
The next lower prime is 5
The next higher prime is 11


Related Solutions

Create a Visual Studio console project (c++) containing a main() program that declares a const int...
Create a Visual Studio console project (c++) containing a main() program that declares a const int NUM_VALUES denoting the array size. Then declare an int array with NUM_VALUES entries. Using a for loop, prompt for the values that are stored in the array as follows: "Enter NUM_VALUES integers separated by blanks:" , where NUM_VALUES is replaced with the array size. Then use another for loop to print the array entries in reverse order separated by blanks on a single line...
Write a C program The Visual Studio project itself must make its output to the Console...
Write a C program The Visual Studio project itself must make its output to the Console (i.e. the Command Prompt using printf) and it must exhibit the following features as a minimum: 3%: Looping Menu with 3 main actions: View Cars, Sell Car, View Sales Note: A Car is defined by its price and model 3%: Must contain at least three arrays to record sales figures (maximum of 10 Car models) Two for recording the price and model of one...
Create a C# .NET Core Console project in Visual Studio. (This is the same kind of...
Create a C# .NET Core Console project in Visual Studio. (This is the same kind of project we have been doing all semester.) Do all of the following in the Program class. You do not need to add any other classes to this project. 2. If it exists, remove the Console.WriteLine(“Hello World!”); line that Visual Studio created in the Program class. 3. At the very top of the Program.cs page you should see using System; On the empty line below...
C++ PROGRAM Using the attached C++ code (Visual Studio project), 1) implement a CoffeeMakerFactory class that...
C++ PROGRAM Using the attached C++ code (Visual Studio project), 1) implement a CoffeeMakerFactory class that prompts the user to select a type of coffee she likes and 2) returns the object of what she selected to the console. #include "stdafx.h" #include <iostream> using namespace std; // Product from which the concrete products will inherit from class Coffee { protected:    char _type[15]; public:    Coffee()    {    }    char *getType()    {        return _type;   ...
Create a Visual Studio console project using c++ void lowerToUpper(std::string & sentence) that iterates over all...
Create a Visual Studio console project using c++ void lowerToUpper(std::string & sentence) that iterates over all characters in the sentence argument. Any lowercase letter should be converted to uppercase. This can be done by including <cctype> and testing each character in sentence with the islower() function. If islower(sentence[i]) returns true then sentence[i] should be replaced with toupper(sentence[i]). The main() function should assign "Hello how are you doing?" to sentence, call lowerToUpper(sentence), and use an if statement to check the new...
Make a Program in Visual Studio / Console App (.NET Framework) # language Visual Basic You...
Make a Program in Visual Studio / Console App (.NET Framework) # language Visual Basic You will be simulating an ATM machine as much as possible Pretend you have an initial deposit of 1000.00. You will Prompt the user with a Main menu: Enter 1 to deposit Enter 2 to Withdraw Enter 3 to Print Balance Enter 4 to quit When the user enters 1 in the main menu, your program will prompt the user to enter the deposit amount....
USING VISUAL STUDIO 2017, LANGUAGE VISUAL C# I have struggled on this program for quite some...
USING VISUAL STUDIO 2017, LANGUAGE VISUAL C# I have struggled on this program for quite some time and still can't quite figure it out. I'm creating an app that has 2 textboxes, 1 for inputting customer name, and the second for entering the number of tickets the customer wants to purchase. There are 3 listboxes, the first with the days of the week, the second with 4 different theaters, and the third listbox is to display the customer name, number...
write a c++ program using micro soft visual studio 2010 to write a program and store...
write a c++ program using micro soft visual studio 2010 to write a program and store 36 in variable x and 8 in variable y. add them and store the result in the variable sum. then display the sum on screen with descriptive text. calculate the square root of integer 36 in x. store the result in a variable. calculate the cube root of integer 8 in y. store result in a variable. display the results of square root and...
Create a new Visual Studio console project named assignment042, and translate the algorithm you developed in...
Create a new Visual Studio console project named assignment042, and translate the algorithm you developed in Assignment 04.1 to C++ code inside the main() function. Your program should prompt for a single 9-digit routing number without spaces between digits as follows: Enter a 9-digit routing number without any spaces: The program should output one of: Routing number is valid Routing number is invalid A C++ loop and integer array could be used to extract the routing number's 9 digits. However...
Create a Visual Studio console project named exercise101. The main() function should prompt for the name...
Create a Visual Studio console project named exercise101. The main() function should prompt for the name of a text file located in the same directory as exercise101.cpp, and search for a word in the text file as follows: Enter text file name: Enter search word: The program should print the number of occurrences of the word in the file: occurrences of were found in If the file could not be opened then display: File not found Use Stream file I/O...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT