Results 1 to 4 of 4

Thread: To write first 100 prime numbers in c++ program.

  1. #1
    Join Date
    Jul 2009
    Posts
    124

    To write first 100 prime numbers in c++ program.

    Hello friends,

    I want a simple program to write 100 prime numbers in c++
    Please help me with the code.

    How to write c++ program the first 100 prime numbers.

    Thanks in advance.

  2. #2
    Join Date
    Oct 2008
    Posts
    79

    Re: To write first 100 prime numbers in c++ program.

    Hey,

    Why dont you use a for loop?

    Code:
    for ( int n = 0, n >= 100, n++)
    here it is:
    Code:
    #include <iostream>
    using namespace std;
     
    int main() {
     
        for (int n = 3; n <= 100; n++) {
            // TO DO
        }
     
        return 0;
    }
    I hope this helps.

  3. #3
    Join Date
    Oct 2008
    Posts
    65

    Re: To write first 100 prime numbers in c++ program.

    Hey I have the code but its for java. The logic is same.
    Try it. It give you first 100 prime numbers.
    Code:
    public class whileprime
    {
        public static void main(String args[])
    {
        int num = 4;
        int count = 0;
        System.out.println("First 100 Prime Numbers: ");
        System.out.println("1");
        System.out.println("2");
        System.out.println("3");
        while(count < 100)
            {
                num++;
                if(num % 2 != 0)
                    {
                        int dv = 3;
                        while((num % dv != 0)&&(dv <= ((num - 1) / 2)))
                            {
                                dv++;
                            }
                                if(num % dv != 0)
                                    {
                                        count++;
                                        System.out.println(num + "         ");
                                       
                                    }
                    }
            }
    
    
    }
    
    
    }
    I hope this helps you.

  4. #4
    Join Date
    Oct 2008
    Posts
    86

    Re: To write first 100 prime numbers in c++ program.

    Here is a code to find out prime numbers between 1 - 100 :

    Code:
    void main()
    {
    int i,number=1;
    clrscr();
    while(number<=100)
         {
         i=2;
         while(i<=number)
             {
             if(number%i==0)
             break;
             i++;
             }
         if(i==number)
         printf("\n%d is Prime",number);
         number++;
         }
    getch();
    }
    Is this what you need?

Similar Threads

  1. Program to print GCD of given numbers
    By Bhavesh_seth in forum Software Development
    Replies: 5
    Last Post: 15-03-2011, 08:03 PM
  2. Need C program code to count numbers of negative & positive numbers
    By Sarfaraj Khan in forum Software Development
    Replies: 5
    Last Post: 16-01-2010, 02:00 PM
  3. Program to print odd and even numbers
    By roodiii in forum Software Development
    Replies: 3
    Last Post: 30-11-2009, 02:13 PM
  4. What is the program to swap two numbers?
    By Bharat89 in forum Software Development
    Replies: 3
    Last Post: 26-11-2009, 09:37 AM
  5. Write numbers in arabic
    By Neider in forum Off Topic Chat
    Replies: 3
    Last Post: 21-08-2009, 06:37 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,425,009.15148 seconds with 17 queries