locked
A C# program that accepts a range of integer values and outputs the prime numbers in that range. RRS feed

  • Question

  • I've seen this type of question on the net, just that non is exactly what I want

    I need to create a console application that  allows a user to input a range of values e.g '20 to 200' , '100 to 800' , etc.

    The challenge I'm having so far is how to TEST AND RETRIEVE THE PRIME NUMBERS IN THAT RANGE OF VALUES.

    Please I'm new to coding, so please no "High tech explanations" which might get me more confused than I am already.

    My written code so far is shown below. Sorry for the poor documentation/commenting.

    Thank you.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Numeric;
    using System.Net;
    
    namespace NumberManipulation
    {
        class RangeOfValues
        {
            static void Main(string[] args)
            {
                int info, startingValue, stoppingValue;
    
    
                Console.WriteLine("This program will accept a range of INTEGER values from you, e.g \"10 to 50\"");
                Console.WriteLine("and output the prime number(s), even number(s) and odd number(s) in the range");
                Console.WriteLine();
                
                Console.WriteLine("Press \"1\" to CONTINUE or Press \"0\" to QUIT");
                Console.WriteLine();
    
                info = Convert.ToInt32(Console.ReadLine());
    
                if (info == 1)
                {
                    Console.WriteLine("Enter your range of INTEGER values");
    
                    Console.Write("Starting Value: ");
                    startingValue = Convert.ToInt32(Console.ReadLine());
                    Console.WriteLine();
                    Console.Write("Stopping Value: ");
                    stoppingValue = Convert.ToInt32(Console.ReadLine());
                    Console.WriteLine();
    
                    if (startingValue < stoppingValue)
                    {
                        Console.WriteLine("Your output is given below:");
    
                        for (startingValue = startingValue; startingValue <= stoppingValue; startingValue++)
                        {
                            if (startingValue % 2 == 0)
                                Console.WriteLine("Even number:\t{0}", startingValue);
    
                            if (startingValue % 2 != 0)
                                Console.WriteLine("Odd number:\t{0}", startingValue);
    
                            if (((startingValue % startingValue == 0) && (startingValue % 4 != 0)) && ((startingValue % 9 != 0) && (startingValue % 8 != 0) && (startingValue % 7 != 0)&&(startingValue % 6 != 0) && (startingValue % 5 != 0) && (startingValue % 4 != 0)))
                                Console.WriteLine("Prime number:\t{0}", startingValue);
    
                        }
                    }
                    else
                    {
                         Console.WriteLine("Your output is given below:");
    
                         for (startingValue = startingValue; startingValue >= stoppingValue; startingValue--)
                         {
                            if (startingValue % 2 == 0)
                                Console.WriteLine("Even nunber:\t{0}", startingValue);
    
                            if (startingValue % 2 != 0)
                                Console.WriteLine("Odd number:\t{0}", startingValue);
    
                         }
    
                     }
                    
                }
                else
                {
                    Console.WriteLine("You entered \"0\", program is  exiting,\nThanks for participating");
                    Console.WriteLine();
    
                }
    
        
            }
        }
    }
    

    Monday, August 12, 2013 4:01 PM

Answers

  • Hello ArthurEzenwanne,

    Welcome to MSDN Forums.

    I would like to provide some code for you.

    class Program
        {
            static void Main(string[] args)
            {
                int info, startingValue, stoppingValue;
                Console.WriteLine("This program will accept a range of INTEGER values from you, e.g \"10 to 50\"");
                Console.WriteLine("and output the prime number(s), even number(s) and odd number(s) in the range");
                Console.WriteLine();
               Console.WriteLine("Press \"1\" to CONTINUE or Press \"0\" to QUIT");
                Console.WriteLine();
                info = Convert.ToInt32(Console.ReadLine());
                if (info == 1)
                {
                    Console.WriteLine("Enter your range of INTEGER values");
                    Console.Write("Starting Value: ");
                    startingValue = Convert.ToInt32(Console.ReadLine());
                    Console.WriteLine();
                    Console.Write("Stopping Value: ");
                    stoppingValue = Convert.ToInt32(Console.ReadLine());
                    Console.WriteLine();
                    //do some validation to check the number is right
                    for (int i = startingValue ; i <= stoppingValue; i++)
                    {
                        //invoke the method to determine the number whether is prime number or not.
                        string result = string.Format("{0} is {1}", i, AmIPrimeNumber(i) ? "a prime number" : "not a prime number");
                        Console.WriteLine(result);
                    }
                }
                else
                {
                    Console.WriteLine("You entered \"0\", program is  exiting,\nThanks for participating");
                    Console.WriteLine();
                }
                Console.Read();
           }
            /// <summary>
            /// this method test a number you given
            /// </summary>
            /// <param name="number">a number</param>
            /// <returns>if the result is prime number,then return true.else return false</returns>
            private static bool AmIPrimeNumber(int number)
            {
                bool result=true;
                // a prime number. it has exactly two positive divisors, 1 and the number itself.
                // so what we need to do. just let the number divided by the numbers between 1 and itself.
                for (int i = number - 1; i > 1; i--)
                {
                    if (number % i == 0)
                    {
                        //the number can be i divided, it isn't a prime number,so we break the for.
                        result = false;
                        break;
                    }
                }
                //after for statement,there is no number find can divide the number ,so it is prime number.
                return result;
            }
        }

    Tuesday, August 13, 2013 7:25 AM

All replies

  • Hello ArthurEzenwanne,

    Welcome to MSDN Forums.

    I would like to provide some code for you.

    class Program
        {
            static void Main(string[] args)
            {
                int info, startingValue, stoppingValue;
                Console.WriteLine("This program will accept a range of INTEGER values from you, e.g \"10 to 50\"");
                Console.WriteLine("and output the prime number(s), even number(s) and odd number(s) in the range");
                Console.WriteLine();
               Console.WriteLine("Press \"1\" to CONTINUE or Press \"0\" to QUIT");
                Console.WriteLine();
                info = Convert.ToInt32(Console.ReadLine());
                if (info == 1)
                {
                    Console.WriteLine("Enter your range of INTEGER values");
                    Console.Write("Starting Value: ");
                    startingValue = Convert.ToInt32(Console.ReadLine());
                    Console.WriteLine();
                    Console.Write("Stopping Value: ");
                    stoppingValue = Convert.ToInt32(Console.ReadLine());
                    Console.WriteLine();
                    //do some validation to check the number is right
                    for (int i = startingValue ; i <= stoppingValue; i++)
                    {
                        //invoke the method to determine the number whether is prime number or not.
                        string result = string.Format("{0} is {1}", i, AmIPrimeNumber(i) ? "a prime number" : "not a prime number");
                        Console.WriteLine(result);
                    }
                }
                else
                {
                    Console.WriteLine("You entered \"0\", program is  exiting,\nThanks for participating");
                    Console.WriteLine();
                }
                Console.Read();
           }
            /// <summary>
            /// this method test a number you given
            /// </summary>
            /// <param name="number">a number</param>
            /// <returns>if the result is prime number,then return true.else return false</returns>
            private static bool AmIPrimeNumber(int number)
            {
                bool result=true;
                // a prime number. it has exactly two positive divisors, 1 and the number itself.
                // so what we need to do. just let the number divided by the numbers between 1 and itself.
                for (int i = number - 1; i > 1; i--)
                {
                    if (number % i == 0)
                    {
                        //the number can be i divided, it isn't a prime number,so we break the for.
                        result = false;
                        break;
                    }
                }
                //after for statement,there is no number find can divide the number ,so it is prime number.
                return result;
            }
        }

    Tuesday, August 13, 2013 7:25 AM
  • Thanks #Wong. It worked, however, "0" and "1" are also listed as prime numbers in my output, if my range of value starts from "0".

    Thanks.

    Arthur


    Tuesday, August 13, 2013 12:42 PM
  • Hello ArthurEzenwanne,

    Based on the definition of the prime number on Wiki, a prime number is a natural number greater than 1. So you should check the number before you invoke “AmIPrimeNumber” method.

    I’d like you to refer this reference below:

    Prime Number

    Best Regards,

    Wednesday, August 14, 2013 6:15 AM