Write a program to accept a number from the user and display all the prime numbers from one up to the number entered by user.
Sample Solution
C# Sharp Code
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace primeNum { class Program { int num = 0; public int number; public void Prim() { Console.WriteLine("Enter a Number for Prime number"); number = Convert.ToInt32(Console.ReadLine()); Console.WriteLine("Prime number Are "); for (int i = 2; i <= number; i++) { for (int j = 1; j <= i; j++) { if (i % j == 0) { num++; } } if (num == 2) Console.WriteLine(i); num = 0; } } static void Main(string[] args) { Program obj = new Program(); obj.Prim(); Console.ReadLine(); } } }
If you want to display 1 to 20 Prime numbers, then just enter 20 after that you can get the result.
Show the below output.
Sample Output
If you want to display 1 to 100 Prime numbers, then just enter 100 after that you can get the result.
Show the below output.
Sample Output
No comments:
Post a Comment