Can someone help me.
I need to solve these problems with c#. Verry fast!!

1. Display the first Prime "n" numbers (n=given from the keyboard)
Ex: n= 6 -> 2, 3,5,7,11,13
2. Enter a string of numbers on the keyboard until we enter "0".
After that we need to print out the Arithmetic mean of the par numbers (ex: 2) and the impair numbers (ex: 3) that we entered earlier.

Tankyou so much!!!

Recommended Answers

All 16 Replies

sounds like homework to me

why don't you give it a shot and we'll help you out

1.

namespace ConsoleApplication2
{
    class Program
    {
        static void Main(string[] args)
        {
            double n, i;
            Console.WriteLine("Enter a number>");
            n = Convert.ToDouble(Console.ReadLine());
            for (i = n; i <= n; i--)

                if (i >= 1)
                {
                    if (i % 2 == 0)
                        Console.Write("{0}, ", i);
                }
                else
                    break;
            Console.ReadKey();

        }
    }

}

It's not working!

ex: In don't know it!

first n, needs to be an integer

second, you need to see if its a prime number, dividing by two doesn't work (ex 9)

first n, needs to be an integer

second, you need to see if its a prime number, dividing by two doesn't work (ex 9)

can you help me with the code pls!

i will help you, but you must try first

maybe try google, c# is prime

using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            bool current = false;
            int j;

            Console.WriteLine("Enter any integer");
            int num = Int32.Parse(Console.ReadLine());

            for (int i = 2; i <= num; i++)//2 is the first prime number.
                                          //I set i to 2 beacuse it has to print 2 firstly...  
            {
                for (j = 2; j < i; j++)
                {
                    if (i % j == 0)//Controls i is prime number or not...
                    {
                        current = true;
                        break;//breaks for not controlling anymore...
                    }
                }
                if (current == false)
                    Console.Write("{0} ", j);//if i is prime number, print it...
                else
                   current = false;   
            }

            Console.ReadLine();



        }
    }
}

i have to modifi it! so that
1.Display the first Prime "n" numbers (n=given from the keyboard)

would love to help, but please post the code in code tags [ not <

let me give you some tips, you are making progress though

you can't use a for loop like that because the number entered is not a number you go up to, its the total number or prime numbers, redo it with a while loop and a counter for primes found

here's enough to get you started

int num = Int32.Parse(Console.ReadLine());
int primesFound = 0;
int curNum =2;
while(primesFound < num)
{
   bool isNotPrime = false;
   
   //do your check for prime against curNum

   if(isNotPrime == false)
  {
 //if curNum is prime number, print it...
      Console.Write("{0} ", curNum);       
// increment our prime counter
primesFound++; 
  }


  curNum++;

}

CAN YOU HELP ME WITH THIS ONE ALSO!
2.Enter a string of numbers on the keyboard until we enter "0".
After that we need to print out the Arithmetic mean of the par numbers (ex: 2) and the impair numbers (ex: 3) that we entered earlier.

I've spend all day i can't make it!

i can help you yes, one thing at a time

does your prime number one work now?

now, do you finish your project? i can help you but i cant contact with you, can you send email for me? I will send my project about your problem for you. My email: linhlevandlu@gmail.com. Thank you.

Hope you enjoy the spam you'll get for posting your email on a bulletinboard readily searched by search engines.. Spammers are gonna love you.

Failing that you could start a new thread with your new question and explain it clearly, and more usefully explain the reason why you cant work it out.

I see a modest effort on your side. That's already something.
Why not try it like this :
Make a list of primes
Print the n first numbers from the list.
This is the code to start with, you should be able to complete it.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Primes
{
    class Program
    {
        private static List<int> primes = new List<int>();

        private static bool CheckIfPrime(int P)
        {
            bool Prime = true;
            //You will not find any dividers of P greater than sqrt of P
            //Unless you already found them as a result from a smaller one
            int Q = Convert.ToInt32(Math.Sqrt(P));
            foreach (int prime in primes)
            {
                if ((P % prime) == 0 && prime <= Q)
                {
                    Prime = false;
                    break;
                }
            }
            return Prime;
        }

        static void Main(string[] args)
        {
            //check the first 1000 integers for primeness
            //can be changed to whatever until MaxInt
            const int MaxVal = 1000;

            // Add the first and only even prime to our list
            primes.Add(2);

            // Start with 3 and increment by 2 
            // We don't have to check even numbers
            // because we know they are not prime
            for (int i = 3; i <= MaxVal; i += 2)
            {
                //fill in the primes list using our CheckIfPrime routine
            }

            //input a number here say n
            //print out the first n numbers of the primes list
        }
    }
}

By the way. This algorithm was invented by a very old smart greek
and well known as "The sieve of Eratosthenes"

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.