Thread: /i need help in C- program for prime numbers plzZzZ

  1. #1
    Registered User
    Join Date
    Aug 2007
    Posts
    2

    Unhappy /i need help in C- program for prime numbers plzZzZ

    Hi everyone how r u ... plz i need help am having exam next week and i have the tips but dont have the answer
    anyways this is the question:
    1- create a program to print prime number .. and for example it can be all the prime numbers under 200 or under 300 and print ... i hope u understand my question .
    plz use normal prog like this
    Code:
    #include <stdio.h>
    main()
    {
      here put where ever 
    }
    return 0;
    coz am not good at the other types of program ..
    plz try to do it for me as soon as u can or as soon as u see my question ....
    thanxx
    Last edited by Salem; 08-29-2007 at 07:59 AM. Reason: [code] [/code] tags around the code only

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    None of us here will do your work for you (this sounds like a homework situation, which there is a policy on this board not to do). But we will try to help you learn how to do it.

    The first step is to figure out how you would solve the problem by hand, and write down each step. Once you have that, it shouldn't be THAT hard to write a C-program to solve the problem.

    Also, if you haven't done that yet, try writing a program that just prints numbers from one to n where n is (say) 100, 200 or 300.

    Then you just have to add "is x a prime then print x" instead of the simple print x in "print all numbers".

    This technique of development is called "stepwise refinement", and works quite well for small project like this.

    --
    Mats

  3. #3
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    You could also google "sieve of eratosthenes" for an algorithm to find prime numbers. But like matsp said, no-one will do your homework for you!

    QuantumPete

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Using sieve if you don't have a good understanding of programming is probably not a particularly good idea - trivial [1] method is much easier to implement.

    [1] using the "brute force and ignorance" method is never ideal for REAL work, but since this is intended for relatively small numbers and most likely a teaching excercise early on in a student course, it's probably the BEST method.

    --
    Mats

  5. #5
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    Fair enough :-D I remember those days at uni, when efficiency was not #1 priority in coding...

    QuantumPete

  6. #6
    Registered User
    Join Date
    Jul 2006
    Posts
    162
    I've been noticing this influx of users who use this "i'm a n00b" grammar, it's getting worse, i'm becoming afraid to post here.

    i'll have to start going to gamedev more

  7. #7
    Registered User
    Join Date
    Aug 2007
    Posts
    2
    OOOops okay no prob but slowly on me .... i didnt expect someone to do it for me coz i will hav it in exam so i hv to understand it ...i tried my best ok i will tell u wat i did was like wat
    i used for statement to print numbers from 1 to 200 and then i just put if statement under it
    if (x%2=!0 || x%3=!0 || x%4=!0 ...................||x%9=!0) i mean not equal
    printf("%d",a)
    but i couldnt continue it worked for %2=! and 3 but the rest didnt work . . . i will try to post wat i did next time coz now i just replied .. thnx anyway..

  8. #8
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Perhaps you wanted &&, with all of the operators == 0? . . . anyway, that would be much better implemented with a loop. Then you could go
    Code:
    if(x &#37; a != 0) { /* ... */ }
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  9. #9
    Registered User
    Join Date
    Aug 2007
    Posts
    34
    Code:
    #include<stdio.h>
    main()
    {
    int n,i,f=0;
    printf("enter any number");
    scanf("&#37;d",&n);
    for(i=2;i<n;i++)
    {
    if(n%i==0)
    f=1;
    }
    if(f==1)
    printf("entered number is not a prime");
    else
    printf("entered number is a prime");
    getch();
    }

  10. #10
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    Quote Originally Posted by shuaib.akram View Post
    Code:
    #include<stdio.h>
    main()
    {
    int n,i,f=0;
    printf("enter any number");
    scanf("%d",&n);
    for(i=2;i<n;i++)
    {
    if(n%i==0)
    f=1;
    }
    if(f==1)
    printf("entered number is not a prime");
    else
    printf("entered number is a prime");
    getch();
    }
    There we go, the home work is done. lol

    ssharish2005

  11. #11
    Registered User
    Join Date
    Feb 2008
    Posts
    1
    Thank you so much!

  12. #12
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Personally, I'm glad that you found this thread useful, useful enough to express your gratification . . . but you should be aware that you were technically going against the rules of this board.
    5. Don't bump threads. (Bumping: posting messages on threads to move them up the list or to post on a thread that has been inactive for two weeks or longer).
    Best of luck with your prime numbers . . .
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  13. #13
    Registered User
    Join Date
    Mar 2008
    Posts
    2
    This code is not accurate because if n=2 then f=1, so it will say entered number is not a prime, but it is. Think about it, from here you could go further.
    Last edited by shonit; 03-03-2008 at 10:44 PM.

  14. #14
    uint64_t...think positive xuftugulus's Avatar
    Join Date
    Feb 2008
    Location
    Pacem
    Posts
    355
    Quote Originally Posted by linda View Post
    Hi everyone how r u ... plz i need help am having exam next week and i have the tips but dont have the answer
    anyways this is the question:
    1- create a program to print prime number .. and for example it can be all the prime numbers under 200 or under 300 and print ... i hope u understand my question .
    plz use normal prog like this
    Code:
    #include <stdio.h>
    main()
    {
      here put where ever 
    }
    return 0;
    coz am not good at the other types of program ..
    plz try to do it for me as soon as u can or as soon as u see my question ....
    thanxx
    I just couldn't help it so i followed the instructions of the student, and because brevity is
    a virtue i decided to use a recursive technique for the solution
    Code:
    #include <stdio.h>
    int main(int a,int n,int i,int j){return a?puts("Enter number:"),scanf("%d",
    &n),main(0,n,2,1):(i%j)?main(0,n,i,j-1):i<n?(j==1?printf("%d\n",i):0),main(0
    ,n,i+1,(i+1)/2):0;}
    There i put wherever between the {} and look what happened !
    Code:
    ...
        goto johny_walker_red_label;
    johny_walker_blue_label: exit(-149$);
    johny_walker_red_label : exit( -22$);
    A typical example of ...cheap programming practices.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Using variables in system()
    By Afro in forum C Programming
    Replies: 8
    Last Post: 07-03-2007, 12:27 PM
  2. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  3. Perfect Numbers C Program
    By SlayerBlade in forum C Programming
    Replies: 2
    Last Post: 08-28-2005, 05:11 PM
  4. Replies: 11
    Last Post: 11-12-2001, 05:25 PM
  5. Replies: 1
    Last Post: 10-01-2001, 10:39 AM