Re: Calculate and Display Prime Numbers
-
Hi All, I am an Student and learning C#. my work is to write a class for calculating the number which is prime number and w ehave to call that method in a main program to display the output. And i have to get the number from the user end using the Console.ReadLine(). No array declaration only using the for loop or the if else condition loop. please help me in coding for calculating the Prime Number. Awaiting your response ASAP. Regards, Chitra
-
Hi All, I am an Student and learning C#. my work is to write a class for calculating the number which is prime number and w ehave to call that method in a main program to display the output. And i have to get the number from the user end using the Console.ReadLine(). No array declaration only using the for loop or the if else condition loop. please help me in coding for calculating the Prime Number. Awaiting your response ASAP. Regards, Chitra
You want us to do your homework for you? Seriously?
-
Hi All, I am an Student and learning C#. my work is to write a class for calculating the number which is prime number and w ehave to call that method in a main program to display the output. And i have to get the number from the user end using the Console.ReadLine(). No array declaration only using the for loop or the if else condition loop. please help me in coding for calculating the Prime Number. Awaiting your response ASAP. Regards, Chitra
chitra4sat wrote:
And i have to get the number from the user end using the Console.ReadLine().
Does this mean that your program is suppose to determine if a number entered by a user is prime? Design your algorithm for determining whether a number is prime. Definition: An integer p is called a prime number if the only positive integers that divide p are 1 and p itself. Integers that are not prime are called composite. [^] Aside from the number 2, we can eliminate all even numbers because all even numbers are evenly divisible by 2. For more details on optimizing a prime number algorithm, be sure to google. You'll find lots of stuff. This should get you started. If you have any questions, feel free to ask.
-
chitra4sat wrote:
And i have to get the number from the user end using the Console.ReadLine().
Does this mean that your program is suppose to determine if a number entered by a user is prime? Design your algorithm for determining whether a number is prime. Definition: An integer p is called a prime number if the only positive integers that divide p are 1 and p itself. Integers that are not prime are called composite. [^] Aside from the number 2, we can eliminate all even numbers because all even numbers are evenly divisible by 2. For more details on optimizing a prime number algorithm, be sure to google. You'll find lots of stuff. This should get you started. If you have any questions, feel free to ask.
Thanks Guyz, I have tried it as this but the problem is i have to put all the methods in a another class progam such as PrimeNumClass.cs and i have to call that in the main program to display the output. here is my code:
namespace PrimeNumber { class Program { static void Main(string[] args) { // declare variables int n = 2; int totalPrimeNumbers = 0; int x; double sumOfPrimes = 0; // while loop when n <= 100 while (n <= 100) { bool isPrime = true; // test if n is prime for (x = 2; x < n; x++) { if ((n % x) == 0) { isPrime = false; break; } } if (isPrime == true) { Console.WriteLine(n + " is a prime Number."); sumOfPrimes = sumOfPrimes + n; totalPrimeNumbers++; } n++; } } } }
Here everything is in Main Program itself. But i want it all in a seperate class Nmaed PrimeNumClass.cs and i have to call that class in the main Class to display the output. please help me. itz very urgnet. regards, Chitra -
Thanks Guyz, I have tried it as this but the problem is i have to put all the methods in a another class progam such as PrimeNumClass.cs and i have to call that in the main program to display the output. here is my code:
namespace PrimeNumber { class Program { static void Main(string[] args) { // declare variables int n = 2; int totalPrimeNumbers = 0; int x; double sumOfPrimes = 0; // while loop when n <= 100 while (n <= 100) { bool isPrime = true; // test if n is prime for (x = 2; x < n; x++) { if ((n % x) == 0) { isPrime = false; break; } } if (isPrime == true) { Console.WriteLine(n + " is a prime Number."); sumOfPrimes = sumOfPrimes + n; totalPrimeNumbers++; } n++; } } } }
Here everything is in Main Program itself. But i want it all in a seperate class Nmaed PrimeNumClass.cs and i have to call that class in the main Class to display the output. please help me. itz very urgnet. regards, Chitrachitra4sat wrote:
Here everything is in Main Program itself. But i want it all in a seperate class Nmaed PrimeNumClass.cs and i have to call that class in the main Class to display the output.
Creating a new class is easy. Just right click on your project and click on Add then Class from the pop menu. Fill in the text box with the name of your of your class, in this case, PrimeNumClass. The IDE will create a source file for you that looks like this:
using System;
using System.Collections.Generic;
using System.Text;namespace PrimeNumber
{
class PrimeNumClass
{
}
}Now, once you get to this point, you need to add a method to the class that contains your prime number algorithm. In this case, it would probably be best to make the method static. Also, make sure that it has public access so that it can be called from your Main function. I'm going to leave it here and let you take over. If you have any problems, feel free to ask.
-
chitra4sat wrote:
Here everything is in Main Program itself. But i want it all in a seperate class Nmaed PrimeNumClass.cs and i have to call that class in the main Class to display the output.
Creating a new class is easy. Just right click on your project and click on Add then Class from the pop menu. Fill in the text box with the name of your of your class, in this case, PrimeNumClass. The IDE will create a source file for you that looks like this:
using System;
using System.Collections.Generic;
using System.Text;namespace PrimeNumber
{
class PrimeNumClass
{
}
}Now, once you get to this point, you need to add a method to the class that contains your prime number algorithm. In this case, it would probably be best to make the method static. Also, make sure that it has public access so that it can be called from your Main function. I'm going to leave it here and let you take over. If you have any problems, feel free to ask.
Hi Thanks, I have created it as you said the PrimeNumClass as you have mentioned . but what are all the things have to be inside that class and let me know it by code. becoz i have the output in the main program and all the funcctions are carried out there. but i have been asked that in the main program there should not be anything but we have to create the object and w ehave to call that method to display the output of the prime number. so please help me what to do ?
-
Hi Thanks, I have created it as you said the PrimeNumClass as you have mentioned . but what are all the things have to be inside that class and let me know it by code. becoz i have the output in the main program and all the funcctions are carried out there. but i have been asked that in the main program there should not be anything but we have to create the object and w ehave to call that method to display the output of the prime number. so please help me what to do ?
chitra4sat wrote:
but what are all the things have to be inside that class and let me know it by code. becoz i have the output in the main program and all the funcctions are carried out there. but i have been asked that in the main program there should not be anything but we have to create the object and w ehave to call that method to display the output of the prime number.
You need to add a method to your PrimeNumClass. For example, in your Program class, you have a method named Main. When you run your console application, the application invokes your Main method. So add a method to your PrimeNumClass. You need to give your method a name, return type, and specify its access. In addition you need to decide if your method takes any arguments. For example, below is a class with no methods:
namespace ClassDemo
{
public class MyClass
{
}
}Now, let's add a method to it that doesn't return anything and takes no arguments:
namespace ClassDemo
{
public class MyClass
{
public void SayHello()
{
Console.WriteLine("Hello!");
}
}
}The class now has a method called SayHello. If I want to use this class, I can create an instance of it and invoke the SayHello method:
MyClass mc = new MyClass();
mc.SayHello();
Based on what I've shown you here, add a method to your PrimeNumClass. Then put the code you have in your Main method into this method.