Thread: hello friends.

  1. #1
    Registered User
    Join Date
    Feb 2011
    Posts
    14

    Smile hello friends.

    hello friends, am vijay shankar.. am new to this forum as well as c program. so pls help me out by correcting me. i have written a program for prime, pls do tell me my mistakes.

    main()
    {
    int i,n;
    printf("number:");
    scanf("%d",&n);
    for(i=2;i<n;i++)
    {
    if(n%i==0)
    break;
    else continue;
    }
    if(n==i)
    printf("\nits prime");
    else
    printf("\nnot prime");
    getch();
    }

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    Quote Originally Posted by vijayshankar View Post
    hello friends, am vijay shankar.. am new to this forum as well as c program. so pls help me out by correcting me. i have written a program for prime, pls do tell me my mistakes.
    Use code tags first!
    Last edited by GReaper; 02-04-2011 at 10:06 AM.
    Devoted my life to programming...

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Welcome to the forum, Vijay!

    Unfortunately, code without code tags around it, looks like something the cat drug in from the street - very hard to study.

    In your program, you're missing that second loop or function:

    Loop 1. Get the next number to be tested to see if it's prime

    Loop 2. In this inner loop, get the next number to be tested to see if it's an even factor into the number from loop 1.

    Loop 2 can easily be another function, instead of a nested loop, in your design.

  4. #4
    Registered User
    Join Date
    Feb 2011
    Posts
    14
    @Adak:


    but am getting the ouput correctly..

  5. #5
    Registered User
    Join Date
    Feb 2011
    Posts
    14
    Code:
    #include<stdio.h>
    #include<conio.h>
    void main()
    {
    int i,n;
    printf("enter no:");
    scanf("%d",&n);
    for(i=2;i<n;i++)
    {
    if(n%i==0)
    break;
    else
    continue;
    }
    if(n==i)
    printf("\n prime");
    else
    printf("\n not prime");
    getch();
    }

  6. #6
    -bleh-
    Join Date
    Aug 2010
    Location
    somewhere in this universe
    Posts
    463
    Code:
    for(i=2;i<n;i++)
    {
          if(n%i==0)
              break;
          //// don't need these line. 
          ///////////
          ///else
          ///continue;
         /////////////////
    }

    but am getting the ouput correctly..
    Your program works to test whether a number is primes. It will be slow to use it in the future to generate primes though, just so you know.
    Last edited by nimitzhunter; 02-04-2011 at 12:26 PM.
    "All that we see or seem
    Is but a dream within a dream." - Poe

  7. #7
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    OK, I misunderstood. I thought you wanted to find a LOT of primes, instead of testing only one number to see if it's prime.

    Code tags look great! :thup:

    main() in C, is always int main with a return of zero for a normal run.

    No void main(), ever. It may run, but you should be getting a warning from your compiler, even if it's Turbo C.

    Subordinate lines of code should be indented 2 to 5 spaces, to let you easily see how the logic is working in your program. With practice, your eye will become good at finding some errors, quite easily if you use that format.

    Also, others can much more easily study your code.

    As noted above, some of the code is redundant. More clear and concise code is always the way you want to go, with any programming code.

    Rube Goldberg's are not anyone's friends, in programming.
    Last edited by Adak; 02-04-2011 at 12:27 PM.

  8. #8
    Registered User
    Join Date
    Feb 2011
    Posts
    14
    Thank you friends.... am just a beginner in C. so pls bear with my silly mistakes and questions...

    @nimitzhunter: you mean , the following coding , wright?
    Code:
    else
    continue;
    so, other than this part, is my coding short or still is it lenghty?

  9. #9
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    It's fine, otherwise.

  10. #10
    Registered User
    Join Date
    Feb 2011
    Posts
    14
    If i wanna clear more such doubts, shall i continue it in the same thread or a new one.

  11. #11
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    New code you have doubts about = new thread.

    This code you have doubts about = this thread.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. need a help friends... pls
    By joelove in forum C Programming
    Replies: 4
    Last Post: 02-08-2010, 12:32 PM
  2. Friends and Members
    By MarlonDean in forum C++ Programming
    Replies: 6
    Last Post: 06-18-2008, 11:52 AM
  3. problem with friends
    By noob2c in forum C++ Programming
    Replies: 8
    Last Post: 09-15-2003, 07:43 AM
  4. Error: Friends must be functions or classes
    By pimming in forum C++ Programming
    Replies: 1
    Last Post: 06-10-2003, 05:31 PM
  5. Help me, Friends and Gurus ????
    By yescha in forum C Programming
    Replies: 3
    Last Post: 12-03-2001, 03:04 AM