Q:

Write a C# Sharp program to determine whether a given number is prime or not

0

Write a C# Sharp program to determine whether a given number is prime or not

Sample Output:

Check whether a given number is prime or not:                                                                                         
-----------------------------------------------                                                                                                         
Input  a number: 53                                                                                                         
53 is a prime number.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

using System;  
public class Exercise32  
{  
    public static void Main()
{

    int num,i,ctr=0;
	
	Console.Write("\n\n");
    Console.Write("Check whether a given number is prime or not:\n");
    Console.Write("-----------------------------------------------");
    Console.Write("\n\n");  	
	
    Console.Write("Input  a number: ");
    num= Convert.ToInt32(Console.ReadLine());	
    for(i=2;i<=num/2;i++){
        if(num % i==0){
         ctr++;
            break;
        }
    }
   if(ctr==0 && num!= 1)
        Console.Write("{0} is a prime number.\n",num);
   else
      Console.Write("{0} is not a prime number\n",num);
	} 
}

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now