this is my program I hope you understand what I am trying to do.
if (primeNum = int)
{
Console.WriteLine(" the number entered is not prime ");
}
this is my program I hope you understand what I am trying to do.
if (primeNum = int)
{
Console.WriteLine(" the number entered is not prime ");
}
If you want to know if it's a whole number, then round it and compare the numbers. If the number is a whole number, it will not change when it's rounded...so if the rounded version of the number is equal to the number itself, then it's a whole number. If they're different, then it's not a whole number:
double i = 1.2;
if (Math.Round(i, 0, MidpointRounding.AwayFromZero) == i) {
//whole number
} else {
//not a whole number
}
private static bool IsWhole(double number)
{
return (number % 1 == 0);
}
If Program.isWhole(5.67)
returns true
call System.Environment.Exit(0)
to close application.
See this: How can I get the data type of a variable in C#?
and try:
if(primeNum.GetType() == typeof(int))
Try this program:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace primeCsharp
{
class Program
{
static void Main(string[] args)
{
int n = 0; // Number to be test for prime-ness
int i = 2; // Loop counter
bool is_prime = true; // Boolean flag...
// Assume true for now
Console.WriteLine("Enter a number and press ENTER: ");
n = Console.Read();
// Test for a prime number by checking for divisiblity
// by all whole numbers from 2 to sqrt(n).
while (i <= Math.Sqrt(n))
{
if (n % i == 0)
{ // If i divides n,
is_prime = false; // n ix not prime
break; // BREAK OUT OF THE LOOP NOW!
}
++i;
}
// Print results
if (is_prime)
{
Console.WriteLine("Number is prime.\n");
}
else
{
Console.WriteLine("Number is not prime.\n");
}
}
}
}
You can check whether the number is equal to the value of its conversion to int
, like this:
if (primeNum == (int)primeNum)