-4

My code finds prime numbers in a given range. I want to make a menu in the end of it that will ask if i want to save output numbers to a file and then save it. I would really appreciate any help.

using System;  
public class Exercise34 
{  
    public static void Main()
{
    int num,i,ctr,stno,enno;

    Console.Write("\n\n");
    Console.Write("Find the prime numbers within a range of numbers:\n");
    Console.Write("---------------------------------------------------");
    Console.Write("\n\n");      

    Console.Write("Input starting number of range: ");
    stno = Convert.ToInt32(Console.ReadLine());     
    Console.Write("Input ending number of range : ");
    enno = Convert.ToInt32(Console.ReadLine());     
    Console.Write("The prime numbers between {0} and {1} are : \n",stno,enno);

    for(num = stno;num<=enno;num++)
       {
         ctr = 0;

         for(i=2;i<=num/2;i++)
            {
             if(num%i==0){
                 ctr++;
                 break;
             }
        }

         if(ctr==0 && num!= 1)
             Console.Write("{0} ",num);
    }
    Console.Write("\n"); 
  } 
}
stuartd
  • 70,509
  • 14
  • 132
  • 163
tynai cha
  • 3
  • 1
  • My code finds prime numbers in a given range. I want to make a menu in the end of it that will ask if i want to save output numbers to a file and then save it. I would really appreciate any help – tynai cha May 16 '18 at 13:49
  • what should we do with this? – T.S. May 16 '18 at 13:51
  • 2
    Possible duplicate of [Easiest way to read from and write to files](https://stackoverflow.com/questions/7569904/easiest-way-to-read-from-and-write-to-files) – mjwills May 16 '18 at 13:51
  • @tynaicha Post your text inside the question body, not as a comment – Slaven Tojić May 16 '18 at 13:54
  • In order to do this you have to actually **record** the prime numbers you find. – stuartd May 16 '18 at 13:59

1 Answers1

1

I suggest extracting method(s): do not cram everything into single Main, decompose the solution into easy to read and debug routines

  private static bool IsPrime(int value) {
    if (value <= 1)
      return false;

    if (value % 2 == 0)
      return value == 2;

    int n = (int) (Math.Sqrt(value) + 0.5);

    for (int i = 3; i <= n; i += 2)
      if (value % i == 0)
        return false;   
  }

  private static IEnumerable<int> Primes(int from, int to) {
    if (to <= 1)
      yield break;

    if (from < 2)
      from = 2;  

    for (int value = from; value <= to; ++value)
      if (IsPrime(value))
        yield return value;
  }

And then you put it as simple as (output to Console)

  public static void Main() {
    ...
    Console.Write("The prime numbers between {0} and {1} are : \n",stno,enno);

    foreach (int value in Primes(stno, enno)) 
      Console.WriteLine(value);
  }

or (output to File)

  using System.IO;
  using System.Linq;

  ...

  public static void Main() {
    ...
    Console.Write("The prime numbers between {0} and {1} are : \n",stno,enno);

    File.WriteAllLines(@"c:\MyFile.txt", Primes(stno, enno)
      .Select(value => value.ToString()));
  }
Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215