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
  • Using Visual Studio 2017 Create This Program Using C# implement source code Program 5: The concept...

    Using Visual Studio 2017 Create This Program Using C# implement source code Program 5: The concept of a 5-digit palindrome number is a 5-digit number that reads the same from left to right and from right to left. For example, 12121, 45454, and 14741 are valid 5-digit palindrome numbers. Design (pseudocode) and implement (source code) a program (name it FiveDigitPalindrom) that reads a 5-digit number from the user (as integer value, not string) and then mathematically (using division and remainder...

  • 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...

  • **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: "Program 1" 2. Next you will ask the user to enter three floats and read the values into variables. 3. Perform addition, multiplication, and division examples with the numbers entered. 4. Implicitly cast the floats into doubles. 5. Explicitly cast the floats into ints. 6. Use conversion methods to convert the floats into strings. 7. Ask the user...

  • Using visual Studio C# 1. In one text box, your program must except a letter (A-Za-z),...

    Using visual Studio C# 1. In one text box, your program must except a letter (A-Za-z), a number (0-9), or a special symbol (#@!$%^&*). 2. If the user enters a letter, call a module, displayLetter. Pass the letter they entered into the module. The module will then display a figure (stick man?) using the letter the person entered. 3. If the user enters a number, call a module displayNumber. Pass the number they entered into the module. The module will...

  • 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...

  • Use Microsoft Visual Studio C# only, don't need a GUI. Use Console interface Danielle, Edward, and...

    Use Microsoft Visual Studio C# only, don't need a GUI. Use Console interface Danielle, Edward, and Francis are three salespeople at Holiday Homes. Write an application named HomeSales that prompts the user for a salesperson initial (D, E, or F). Either uppercase or lowercase initials are valid. While the user does not type Z, continue by prompting for the amount of a sale. Issue an error message for any invalid initials entered. Keep a running total of the amounts sold...

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