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...
ONLY USE VISUAL STUDIO (NO JAVA CODING) VISUAL STUDIO -> C# -> CONSOLE APPLICATION In this...
ONLY USE VISUAL STUDIO (NO JAVA CODING) VISUAL STUDIO -> C# -> CONSOLE APPLICATION In this part of the assignment, you are required to create a C# Console Application project. The project name should be A3<FirstName><LastName>P2. For example, a student with first name John and Last name Smith would name the project A1JohnSmithP2. Write a C# (console) program to calculate the number of shipping labels that can be printed on a sheet of paper. This program will use a menu...
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....
create a C++ program using Visual Studio that could be used by a college to track...
create a C++ program using Visual Studio that could be used by a college to track its courses. In this program, create a CollegeCourse class includes fields representing department, course number, credit hours, and tuition. Create a child (sub class) class named LabCourse, that inherits all fields from the the CollegeCourse class, includes one more field that holds a lab fee charged in addition to the tuition. Create appropriate functions for these classes, and write a main() function that instantiates...
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT