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.
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
Next – Simple program #9
Keep learning and sharing...
No comments:
Post a Comment