-3

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 ");
                        }
ocgprogramers
  • 35
  • 1
  • 8
  • 2
    Possible duplicate of [Check if number is prime number](http://stackoverflow.com/questions/15743192/check-if-number-is-prime-number) – nicovank Oct 21 '16 at 03:27
  • no, it is not. just because i have written word prime in it does not mean I am looking for same thing. I need to find the function or an expression which checks the result if it is whole number or not. – ocgprogramers Oct 21 '16 at 03:30
  • An int is always a whole number. If you declared primeNum as an int, it will always be a whole number, and of type int. – Max Hampton Oct 21 '16 at 03:38

5 Answers5

1

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
}
soohoonigan
  • 2,342
  • 2
  • 10
  • 18
1
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.

Isaac Hili
  • 129
  • 1
  • 3
  • 16
0

See this: How can I get the data type of a variable in C#?

and try:

if(primeNum.GetType() == typeof(int))
Community
  • 1
  • 1
nicovank
  • 3,157
  • 1
  • 21
  • 42
0

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");
        }
    }
 }
}
James N
  • 84
  • 2
  • 11
  • thank you for showing your code. but what i am trying to do is to code this program without using "break;"command. but the program still loops through and does not include "Not Prime numbers". in the final result – ocgprogramers Oct 22 '16 at 23:58
0

You can check whether the number is equal to the value of its conversion to int, like this:

if (primeNum == (int)primeNum)
Lajos Arpad
  • 64,414
  • 37
  • 100
  • 175