Skip to main content

Print prime number program

 Consider the following program with explanation 


  1. using System;  
  2.   
  3. namespace PrimeNumber  
  4. {  
  5.     class Program  
  6.     {  
  7.         static void Main(string[] args)  
  8.         {  
  9.             Console.WriteLine("Enter a number");  
  10.             int number = Convert.ToInt32(Console.ReadLine());  
  11.             int result = Check_Prime(number);  
  12.             if (result == 0)  
  13.             {  
  14.                 Console.WriteLine("{0} is not a prime number", number);  
  15.             }  
  16.             else  
  17.             {  
  18.                  Console.WriteLine("{0} is  a prime number", number);  
  19.             }              
  20.             Console.Read();  
  21.         }   
  22.   
  23.         private static int Check_Prime(int number)  
  24.        {  
  25.             int i;  
  26.             for (i = 2; i <= number - 1; i++)  
  27.             {  
  28.                 if (number % i == 0)  
  29.                 {  
  30.                     return 0;  
  31.                 }  
  32.             }  
  33.             if (i == number)  
  34.             {  
  35.                 return 1;  
  36.             }  
  37.             return 0;  
  38.         }  
  39.     }    
  40. } 


If you apply static keyword with any method, it is known as static method. A static method belongs to the class rather than the OBJECT OF A CLASS. A static method can be invoked without the need for creating an instance of a class. A static method can access static data member and can change the value of it.


Comments

  1. Effortless Commutes: IPIP understands the importance of a hassle-free experience. With door to door transport you can bid farewell to the complexities of coordinating multiple modes of transportation. Whether you're moving homes, transporting goods, or planning a special delivery, IPIP ensures a smooth and efficient process.

    ReplyDelete

Post a Comment

Popular posts from this blog

Windows 11 leak reveals new UI, Start menu, and more

  Windows 11 leak reveals new UI, Start menu, and more 529 The next version of Windows has leaked online     Jun 18, 2021, 12:03pm EDT Share this story Share this on Facebook (opens in new window) Share this on Twitter (opens in new window) SHARE All sharing options The new Windows 11 UI. Microsoft’s upcoming Windows 11 operating system has leaked online today. After screenshots were first published at Chinese site Baidu, the entire Windows 11 OS has appeared online, complete with a new user interface, Start menu, and lots more. The new Windows 11 user interface and Start menu look very similar to what was originally found in Windows 10X. Microsoft had been simplifying Windows for dual-screen devices, before canceling this project in favor of Windows 11. Visually, the biggest changes you’ll notice can be found along the taskbar. Microsoft has centered the app icons here, cleaned up the tray area, and included a new Start button and menu. This updated S...