L
Luka Bond
Guest
So I just started learning c# but it wasn't that hard because i already had my basics in C (arduino). I wanted to learn game design but i needed to try learning c# first. I made my second project a Prime Number calculator where there are two main modes. The first mode checks if your input number is prime or not, and the second makes list primes in between your input starting value and ending value. The problem is that sometimes when I input my value and press enter, nothing shows up. I'm not really sure why but I think it might be because I set the bar kind of high with unsigned long for every input. Please take this code and if it works, keep playing with it. It doesn't take long for the bug to show up, for me atleast.
But anyways here's my code:
using System;
namespace Prime_Numbers_calculator
{
class Program
{
public static decimal x;
public static ulong pd;
public static int mc;
public static bool gtfo;
static void Main(string[] args)
{
for (int y = 0; y < 1; y++)
{
Console.WriteLine("Welcome to prime number calculator, please press any key to proceed.");
Console.ReadKey();
}
Console.Clear();
while (true)
{
MM();
if (mc == 0)
{
Console.Clear();
PC();
Console.WriteLine("Press any key to proceed.");
Console.ReadKey();
}
else if (mc == 1)
{
Console.Clear();
PVINP();
Console.WriteLine("Press any key to proceed.");
Console.ReadKey();
}
EM();
Console.Clear();
if (gtfo == true)
{
Console.WriteLine("Thank you for using Prime Number Calculator");
break;
}
}
}
static void PC()//Program that checks if num(input) = prime
{
ulong num;
Console.Write("Please enter a number");
Console.WriteLine(", The limit is about 18.4 quintrillion");
num = Convert.ToUInt64(Console.ReadLine());
Console.WriteLine("Your answer is:");
x = num;
pd = 2;
while (true)
{
if (num == 2)
{
Console.WriteLine("{0} is a prime number because it can't be divided by anything under half of {1}({2})", num, num, x / 2);
Console.WriteLine("to get a result of a whole number meaning that it only has 2 factors which is 1 and {0}.", num);
break;
}
else if (num / 2 == x / 2)//check if num = even
{
Console.WriteLine("{0} isn't a prime number because it can be divided by 2", num);
break;
}
else if (num / 5 == x / 5)//check if num = even
{
Console.WriteLine("{0} isn't a prime number because it can be divided by 5", num);
break;
}
else if (num / pd == x / pd)//checks if num can be divided by other primes
{
Console.WriteLine("{0} isn't a prime number because it can be divided by {1}", num, pd);
break;
}
else if (num / pd != x / pd && pd < x / 2)//if can't be divided, prime divider +2
{
pd = pd + 2;
}
else if (pd > num / 2)//The program comes to conclusion that num = prime
{
Console.WriteLine("{0} is a prime number because it can't be divided by anything under half of {1}({2})", num, num, x/2);
Console.WriteLine("to get a result of a whole number meaning that it only has 2 factors which is 1 and {0}.", num);
break;
}
}
}
static void PVINP()//Program that makes a list of primes from your starting-ending value input
{
ulong cc;
Console.WriteLine("Please enter your starting value, The limit is about 18.4 quintrillion");
ulong min = Convert.ToUInt64(Console.ReadLine());
Console.WriteLine("Please enter your ending value, The limit is about 18.4 quintrillion");
ulong max = Convert.ToUInt64(Console.ReadLine());
Console.WriteLine("Here are the prime values between {0} and {1}", min, max);
pd = 2;
cc = min;
while(cc < max+1)//repeated until counting value > max value
{
x = cc;
if (cc == 2)
{
Console.WriteLine(cc);
cc++;
}
else if (cc / 2 == x / 2)//checks if num = even
{
cc++;
}
else if (cc / 5 == x / 5)//check if num can be divided by 5
{
cc = cc + 2;
break;
}
else if (cc / pd == x / pd)//checks if num can be divided by other primes
{
cc = cc + 2;
}
else if (cc / pd != x / pd && pd < x / 2)//if can't be divided, prime divider +2
{
pd = pd + 2;
}
else if (pd > x / 2)//The program comes to conclusion that num = prime
{
Console.WriteLine(cc);
cc = cc + 2;
pd = 3;
}
}
}
static void MM()//Main menu
{
mc = 2;
Console.WriteLine("Please select a mode");
Console.WriteLine("Press A to Check if the number is prime or not");
Console.WriteLine("Press S to Show all prime values in a period of numbers");
for (int i = 0; i < 1
{
var ki = Console.ReadKey();
switch (ki.Key)
{
case ConsoleKey.A:
Console.WriteLine("\nYou chose the first option");
i++;
mc = 0;
break;
case ConsoleKey.S:
i++;
mc = 1;
Console.WriteLine("\nYou chose the second option");
break;
default:
break;
}
}
}
static void EM()//Ending menu
{
gtfo = false;
Console.Clear();
Console.WriteLine("Use Prime Number Calculator again?");
Console.WriteLine("Press SPACEBAR to reuse.");
Console.WriteLine("Press ENTER to leave");
for (int i = 0; i < 1
{
var ki = Console.ReadKey();
switch (ki.Key)
{
case ConsoleKey.Spacebar:
Console.WriteLine("\nYou chose to reuse the calculator");
i++;
break;
case ConsoleKey.Enter:
i++;
gtfo = true;
Console.WriteLine("\nYou chose to leave");
break;
default:
break;
}
}
}
}
}
Continue reading...
But anyways here's my code:
using System;
namespace Prime_Numbers_calculator
{
class Program
{
public static decimal x;
public static ulong pd;
public static int mc;
public static bool gtfo;
static void Main(string[] args)
{
for (int y = 0; y < 1; y++)
{
Console.WriteLine("Welcome to prime number calculator, please press any key to proceed.");
Console.ReadKey();
}
Console.Clear();
while (true)
{
MM();
if (mc == 0)
{
Console.Clear();
PC();
Console.WriteLine("Press any key to proceed.");
Console.ReadKey();
}
else if (mc == 1)
{
Console.Clear();
PVINP();
Console.WriteLine("Press any key to proceed.");
Console.ReadKey();
}
EM();
Console.Clear();
if (gtfo == true)
{
Console.WriteLine("Thank you for using Prime Number Calculator");
break;
}
}
}
static void PC()//Program that checks if num(input) = prime
{
ulong num;
Console.Write("Please enter a number");
Console.WriteLine(", The limit is about 18.4 quintrillion");
num = Convert.ToUInt64(Console.ReadLine());
Console.WriteLine("Your answer is:");
x = num;
pd = 2;
while (true)
{
if (num == 2)
{
Console.WriteLine("{0} is a prime number because it can't be divided by anything under half of {1}({2})", num, num, x / 2);
Console.WriteLine("to get a result of a whole number meaning that it only has 2 factors which is 1 and {0}.", num);
break;
}
else if (num / 2 == x / 2)//check if num = even
{
Console.WriteLine("{0} isn't a prime number because it can be divided by 2", num);
break;
}
else if (num / 5 == x / 5)//check if num = even
{
Console.WriteLine("{0} isn't a prime number because it can be divided by 5", num);
break;
}
else if (num / pd == x / pd)//checks if num can be divided by other primes
{
Console.WriteLine("{0} isn't a prime number because it can be divided by {1}", num, pd);
break;
}
else if (num / pd != x / pd && pd < x / 2)//if can't be divided, prime divider +2
{
pd = pd + 2;
}
else if (pd > num / 2)//The program comes to conclusion that num = prime
{
Console.WriteLine("{0} is a prime number because it can't be divided by anything under half of {1}({2})", num, num, x/2);
Console.WriteLine("to get a result of a whole number meaning that it only has 2 factors which is 1 and {0}.", num);
break;
}
}
}
static void PVINP()//Program that makes a list of primes from your starting-ending value input
{
ulong cc;
Console.WriteLine("Please enter your starting value, The limit is about 18.4 quintrillion");
ulong min = Convert.ToUInt64(Console.ReadLine());
Console.WriteLine("Please enter your ending value, The limit is about 18.4 quintrillion");
ulong max = Convert.ToUInt64(Console.ReadLine());
Console.WriteLine("Here are the prime values between {0} and {1}", min, max);
pd = 2;
cc = min;
while(cc < max+1)//repeated until counting value > max value
{
x = cc;
if (cc == 2)
{
Console.WriteLine(cc);
cc++;
}
else if (cc / 2 == x / 2)//checks if num = even
{
cc++;
}
else if (cc / 5 == x / 5)//check if num can be divided by 5
{
cc = cc + 2;
break;
}
else if (cc / pd == x / pd)//checks if num can be divided by other primes
{
cc = cc + 2;
}
else if (cc / pd != x / pd && pd < x / 2)//if can't be divided, prime divider +2
{
pd = pd + 2;
}
else if (pd > x / 2)//The program comes to conclusion that num = prime
{
Console.WriteLine(cc);
cc = cc + 2;
pd = 3;
}
}
}
static void MM()//Main menu
{
mc = 2;
Console.WriteLine("Please select a mode");
Console.WriteLine("Press A to Check if the number is prime or not");
Console.WriteLine("Press S to Show all prime values in a period of numbers");
for (int i = 0; i < 1
{
var ki = Console.ReadKey();
switch (ki.Key)
{
case ConsoleKey.A:
Console.WriteLine("\nYou chose the first option");
i++;
mc = 0;
break;
case ConsoleKey.S:
i++;
mc = 1;
Console.WriteLine("\nYou chose the second option");
break;
default:
break;
}
}
}
static void EM()//Ending menu
{
gtfo = false;
Console.Clear();
Console.WriteLine("Use Prime Number Calculator again?");
Console.WriteLine("Press SPACEBAR to reuse.");
Console.WriteLine("Press ENTER to leave");
for (int i = 0; i < 1
{
var ki = Console.ReadKey();
switch (ki.Key)
{
case ConsoleKey.Spacebar:
Console.WriteLine("\nYou chose to reuse the calculator");
i++;
break;
case ConsoleKey.Enter:
i++;
gtfo = true;
Console.WriteLine("\nYou chose to leave");
break;
default:
break;
}
}
}
}
}
Continue reading...