GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 03-Oct-2009, 19:24
FAT TONY FAT TONY is offline
New Member
 
Join Date: Oct 2009
Posts: 1
FAT TONY is on a distinguished road

Program to find biggest prime number from user input


Hey there.. I'm have to finish a program for tomorrow. What I got to do is this:

The guide will put a number between 1 and 10000

I need to find all primes into the number he put, not print, but save them and print the biggest primes of this primes and the second biggest.

For example the guide put 10
I will say him "There is 4 primes"
The biggest is 7 and the second is 5...

What I've already made is this :

CPP / C++ / C Code:
#include <stdio.h>
#include <conio.h>

int main()
{
int x, y, z, cont=0, cont_divisiveis;
printf("Put a Number between 1 and 10000:\n");
scanf("%i",&z);
{
if(z<1 || z>10000) printf("Wrong value, pu it again:\n");
for (x = 2; x<=z; x++)
{
cont_divisiveis = 0;
for (y = 2; y<=x; y++)
{
if (x%y == 0)
{
++cont_divisiveis;
}
}
if (cont_divisiveis < 2)
{
printf("\n%i", x);
++cont;
}
}
printf("\n\nPrime Numbers Finding: %i", cont);
}
}
but i couldn´t made the final part that said in the guide , i post above.

sorry for bad english, and thanks for your help.

hugs from brazil
Last edited by admin : 03-Oct-2009 at 22:59. Reason: Please insert your example C/C++ codes between [CPP] and [/CPP] tags
  #2  
Old 03-Oct-2009, 21:26
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,335
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all

Re: Need some help to finish a program !!


Read the Guidelines and repost so we can read your code.
__________________

During the election they said Obama could only be elected when pigs fly. Well, we currently have an epidemic of Swine Flu. Coincidence?
  #3  
Old 04-Oct-2009, 09:16
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,218
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Program to find biggest prime number from user input


Quote:
Originally Posted by FAT TONY
...
I need to find all primes into the number he put, not print, but save them and print the biggest primes of this primes and the second biggest.

If you need to save all of the primes, then allocate an array, say primes and, inside your loop, whenever you find a prime number, store its value in primes[cont] (and, of course increment cont each time, as you already do). After you have found all of them, print the last two values in the array.


If you only need to count the number of primes that you found and don't need to save them all, but only print the largest two, then you can try something like

1. Declare integer variables biggest, second

2. Get user input.
You have already done this, but if the user enters an invalid number, you should either abort the program or go back and ask the user for to try again.

3. Make a loop that finds all prime numbers up to and including the user input, and increments a counter for each one that is found.
You have already done this, but you need to keep track of the largest and second largest numbers found as you go through the loop

CPP / C++ / C Code:
    for (x = 2.....) {/* your loop to find prime numbers */
    /* 
     * your code to count factors of z
     */
        if (cont_divisiveis < 2) {
            printf("%d ", x); /* For debugging purposes, print the primes as they are found */
            second = biggest; /* The one that used to be biggest is now second */
            biggest = x;      /* This one is now the biggest                   */
            ++cont;
        }
    }
    if (cont == 1) { /* This only happens if z is equal to 2, right? */
        /* Print the value of biggest, which is 2 */
    }
    else {
        /* Print the value of biggest and second */
    }


Maybe you can make it a little more elegant, but that is one way to approach the problem.


Regards,

Dave
 
 

Recent GIDBlogOnce again, no time for hobbies by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Equation solver RazoR C Programming Language 3 18-May-2008 10:24
Two-Tier data dissemination code installation problem nidhibansal1984 Computer Software Forum - Linux 6 16-Sep-2007 11:13
BOOKEEPING program, HELP!! yabud C Programming Language 10 17-Nov-2006 04:48
Help with a complex program lordfuoco C++ Forum 5 24-Jun-2006 07:03

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 13:14.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.