;

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


Tutorialsrack 04/08/2019 C# Programs

In this C# program, we will learn how to write a program to print all the prime numbers between 1 to 100  or any Nth number.

What is the Prime Number?

A prime number (or a prime) is a natural number greater than 1 that cannot be formed by multiplying two smaller natural numbers and divided by 1 or itself. In other words, prime numbers can't be divided by other numbers than itself or 1.

For example, 7 is prime because the only ways of writing it as a product, 1 × 7 or 7 × 1, involve 7 itself.

Here is the code of the program to print all the prime numbers between 1 to 100 or any Nth number.:

Code - C# Program To Print All the Prime Numbers Between 1 to 100
using System;

namespace TutorialsrackPrograms
{
    class Program
    {
        //C# Program to Print All the Prime Numbers Between 1 to 100 or Any Nth Range.
        static void Main(string[] args)
        {
            bool isPrime = true;
            Console.WriteLine("Prime Numbers Between 1 To 100: ");
            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.Read();
        }
    }
}
Output

Prime Numbers Between 1 To 100:
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


Related Posts



Comments

Recent Posts
Tags