C# Program to Print All the Prime Numbers between 1 to 100
Posted by Superadmin on August 11 2022 07:30:32

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;
                        break;
                    }
 
                }
                if (isPrime)
                {
                    Console.Write("\t" +i);
                }
                isPrime = true;
            }
            Console.ReadKey();
        }
    }
}
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.

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