C# Program to Print All the Prime Numbers between 1 to 100


This is a C# Program to display all the prime numbers between 1 to 100.

Problem Description

This C# Program Displays All the Prime Numbers Between 1 to 100.

Problem Solution

Here prime number is a natural number greater than 1 that has no positive divisors other than 1 and itself.

Program/Source Code

Here is source code of the C# Program to Display All the Prime Numbers Between 1 to 100. The C# program is successfully compiled and executed with Microsoft Visual Studio. The program output is also shown below.

 * C# Program to Display All the Prime Numbers Between 1 to 100
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace PrimeNumber
    class Program
        static void Main(string[] args)
            bool isPrime = true;
            Console.WriteLine("Prime Numbers : ");
            for (int i = 2; i <= 100; i++)
                for (int j = 2; j <= 100; j++)
                    if (i != j && i % j == 0)
                        isPrime = false;
                if (isPrime)
                    Console.Write("\t" +i);
                isPrime = true;
Program Explanation

In this C# program, using for loop we are finding the prime numbers from 1 to 100. Inside the loop, if condition statement is used to check that range value is less than 2, if the condition is true.

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!

Then execute if condition statement and print number of prime numbers. Using if condition statement checks the value of ‘i’ variable is not equal to the value of ‘j’ variable and the modulus of the value of ‘i’ variable by the value of ‘j’ variable is equal to 0 using logical AND operators.

If the condition is true, then execute if condition statement. Assign the value of isprime variable as false. For loop is used to check the number of prime numbers occurring up to the range. Using if condition statement, print all prime numbers between1 to 100.

Runtime Test Cases
Prime Numbers : 
      2 3 5 7 11 13 17 19 23 29
31 37 41 43 47 53 59 61 67 71
73 79 83 89 97

Sanfoundry Global Education & Learning Series – 1000 C# Programs.

If you wish to look at all C# Programming examples, go to 1000 C# Programs.

Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.