Question

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.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

I have uploaded the Images of the code, Typed code and Output of the Code. I have provided explanation using comments (read them for better understanding).

Images of the Code:1 // using Systems 2 using System; 3 class Primes 4-{ 5 // A method named is_prime to determine Prime or Not 6 // with inputint number=0; // Prompting for input Console.WriteLine(Enter a Number:); // reading input from user number = Convert.ToInt3is + Higher); { // if Higher value is a Prime number // printing Higher value Console.WriteLine(The Higher Prime of +number
Note: If the below code is missing indentation please refer code Images

Typed Code:

// using Systems
using System;
class Primes
{
// A method named is_prime to determine Prime or Not
// with input parameters n
static int is_prime(int n)
{
// A variable to store factors count
int count=0;
// Finding the factors of n
for(int i=1;i<=n;i++)
{
// if n is divisible by i
if(n%i == 0)
{
// incrementing factors count
count++;
}
}
// if factors count is 2
if(count == 2)
{
// returning 1
return 1;
}
else // otherwise
{
// returning 0
return 0;
}
}
  
// Main method
static void Main()
{
// A variable to store user input
int number=0;
// Prompting for input
Console.WriteLine("Enter a Number:");
// reading input from user
number = Convert.ToInt32(Console.ReadLine());
// Calling the method is_prime with user input number
if(is_prime(number)==1)
{
// if the user input is Prime number
Console.WriteLine(number+" is a Prime number");
// A variable to Store Lower Prime number
// initializing it with number - 1
int Lower = number - 1;
// A variable to Store Higher Prime number
// initializing it with number - 1
int Higher = number + 1;
  
// Finding the possible Lower Prime number
while(Lower>2)
{
// Calling method is_prime
if(is_prime(Lower)==1)
{
// if Lower value is a Prime number
// printing Lower value
Console.WriteLine("The Lower Prime of "+number+ " is " +Lower);
// break the loop
break;
}
// decrementing Lower value
Lower--;
}
  
// Finding the possible Higher Prime number
while(Higher > 2)
{
// Calling method is_prime
if(is_prime(Higher) == 1)
{
// if Higher value is a Prime number
// printing Higher value
Console.WriteLine("The Higher Prime of "+number+ " is " + Higher);
// break the loop
break;
}
// incrementing Higher value
Higher ++ ;
}
}
else
{
// if user input is not a prime number
// printing a Message not a a prime
Console.WriteLine(number + " is Not a Prime Number");
}
  
}
}
//code ended here

Output:

Enter a Number: 5 5 is a Prime number The Lower Prime of 5 is 3 The Higher Prime of 5 is 7
If You Have Any Doubts. Please Ask Using Comments.

Have A Great Day!

Add a comment
Know the answer?
Add Answer to:
answer the following using C# Design and program a Visual Studio Console project in C# that...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Recursion program C++. Visual Studio Win 32 console application that has user loop. The Design a...

    Recursion program C++. Visual Studio Win 32 console application that has user loop. The Design a computer program a executes the program, should do the following. computer, as it 1) The computer should politely ask the user for two input integers m and n. There should be code to make sure that m <-n 2) Then there should be a recursive function, called LargestToSmallestO that is invoked to output all the values from n downto m. 3) Then there should...

  • PLEASE DO IN C# AND MAKE SURE I CAN COPY CODE INTO VISUAL STUDIO Program 4:...

    PLEASE DO IN C# AND MAKE SURE I CAN COPY CODE INTO VISUAL STUDIO Program 4: A palindromic prime number is a number that is both prime number and a palindrome number. For example, 131, 313, and 757 are palindromic prime numbers. Design (pseudocode) and implement (source code) a program (name it PalindromicPrime) to display the first 50 palindromic prime numbers, 10 per line separated by one space. The program defines the following methods: Method isPalindome() to check if a...

  • This is with microsoft studio visual basic Sorted Names Design a program that allows the user...

    This is with microsoft studio visual basic Sorted Names Design a program that allows the user to enter 20 names into a String array. Sort the array in ascending (alphabetical) order and display its contents.

  • No. 7 7th question i want this code to be done on c++ visual studio without...

    No. 7 7th question i want this code to be done on c++ visual studio without using bool operation. and please try to make as simple as possible. Also i need flowchart for the code thanks. Q7 I need a code programmed on c++ visual studios. Also flowchart of the code. Try to make it simple college level understandable code. thanks. b. wg. Prompt the user to input the value of a double variabler, which stores the radius of a...

  • Has to be written in C++. Visual studio. Write a C++ program to realize the game...

    Has to be written in C++. Visual studio. Write a C++ program to realize the game of guessing the number. Generally, the game will generate a random number and the player has to find out the number. In each guess, the program will give you a feedback as your guess is correct, too large, or too small. Then the player play repetitively until find out the number. Specifically, the game plays as the following. a). When the game is started,...

  • C++ and Using Microsoft Visual Studio. Write 2 programs: One program will use a structure to...

    C++ and Using Microsoft Visual Studio. Write 2 programs: One program will use a structure to store the following data on a company division: Division Name (East, West, North, and South) Quarter (1, 2, 3, or 4) Quarterly Sales The user should be asked for the four quarters' sales figures for the East, West, North, and South divisions. The data for each quarter for each division should be written to a file. The second program will read the data written...

  • PLEASE DO IN C# AND MAKE SURE I CAN COPY CODE IN VISUAL STUDIO Program 2:...

    PLEASE DO IN C# AND MAKE SURE I CAN COPY CODE IN VISUAL STUDIO Program 2: Design (pseudocode) and implement (source code) a class called Counter. It should have one private instance variable representing the value of the counter. It should have two instance methods: increment() which adds on to the counter value and getValue() which returns the current value. After creating the Counter class, create a program that simulates tossing a coin 100 times using two Counter objects (Head...

  • USING VISUAL BASIC STUDIO PLEASE PROVIDE THE CODE IN C# LANGUAGE SELECT CONSOLE APPLICATION You are...

    USING VISUAL BASIC STUDIO PLEASE PROVIDE THE CODE IN C# LANGUAGE SELECT CONSOLE APPLICATION You are to create a House with 2 rooms in the house. The following is an example of C++ code for the basic classes: **in C#, it may be written differently** class Room { private: double L; double W; public: //functions go here. } class House { private: Room room1; Room room2; public: //functions go here } The code above is C++ version. In C#, you...

  • In this project you will create a console C++ program that will have the user to...

    In this project you will create a console C++ program that will have the user to enter Celsius temperature readings for anywhere between 1 and 365 days and store them in a dynamically allocated array, then display a report showing both the Celsius and Fahrenheit temperatures for each day entered. This program will require the use of pointers and dynamic memory allocation. Getting and Storing User Input: For this you will ask the user how many days’ worth of temperature...

  • **This program is to be created using Visual Studio C#**Create as a Windows Form application** 1....

    **This program is to be created using Visual Studio C#**Create as a Windows Form application** 1. Output a header in the console: “This is a replacement program” 1. Output a header that states: “This is Program 5” 2. Output a thank you message: “Thank you for running the program.”

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT