Drop Down MenusCSS Drop Down MenuPure CSS Dropdown Menu

Tuesday, 18 July 2017

Simple program


Simple program #8 (Source code to print prime number from x to y)

Enter starting number: 1
Enter last number: 50
O/p:
List of prime number:
2 3 5 7 11 13 17 19 23 29 31 37 41 43 47

The program is written in C# programming language and will accept a starting number and a ending number as input. The logic of the program is to find the prime number between these two numbers.
 
Simple program by imagination hunt
Simple program

Note: If you are new to C# and Console Application. Try to code First C# Program

Note: Read articles on how to use Loops and Conditions.

Let’s find out a simple and easy way to code the program.

Practical Implementation:

using System;

namespace patternProblem.Simple_program
{
    class Simple8   //print prime number from x to y
    {
        static void Main(string[] args)
        {
            Console.Write("Enter starting number: ");
            int startNumber = Convert.ToInt32(Console.ReadLine());
            Console.Write("Enter last number: ");
            int lastNumber = Convert.ToInt32(Console.ReadLine());
            int i = 0;
            int j = 2;
            int count = 0;
            Console.WriteLine();

            Console.WriteLine("-----Output-----");
            Console.WriteLine();

            Console.WriteLine("List of prime number: ");
            for (i = startNumber + 1; i < lastNumber; i++)
            {
                for (j = 2; j < i; j++)
                {
                    if (i % j == 0)
                    {
                        break;
                    }
                }
                if (i == j || i == 1)
                {
                    Console.Write(i + " ");
                    count++;
                }
            }
            Console.WriteLine(); Console.WriteLine();
            Console.WriteLine("Total prime number found: " + count);

            Console.ReadKey();
        }
    }
}

Output:
The input numbers are 1 and 50. The program will find all the prime number between these two numbers. The output is shown below:

Enter starting number: 1
Enter last number: 50
List of prime number:
2 3 5 7 11 13 17 19 23 29 31 37 41 43 47
Press any key to continue . . .


For any query, comment us below.

Skip to Main Table Simple program

Previous - Simple program #7

Next – Simple program #9

Click imagination hunt to read latest blogs.


Keep learning and sharing...

No comments:

Post a comment

Featured post

Think that makes you rich and richer

 Napolean said: “You can think and grow rich, but if you can be brought up like most people with work and you won't starve, this wil...