logic of the program.

This is a discussion on logic of the program. within the C Programming forums, part of the General Programming Boards category; hello guys, can u just explain me the logic that is inside the while loop in the code below. unable ...

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

    logic of the program.

    hello guys, can u just explain me the logic that is inside the while loop in the code below. unable to understand the logic of variable c .
    Code:
    #include <stdio.h>
    main()
    {
    		 int n,i=1,j,c;
    		 clrscr();
    		 printf("Enter Number Of Terms
    ");
    		 printf("Prime Numbers Are Follwing
    ");
    		 scanf("%d",&n);
    		 while(i<=n)
    		 {
    		    c=0;
    		    for(j=1;j<=i;j++)
    		    {
                  if(i%j==0)
    		 		 c++;
     		    }
               if(c==2)
               printf("%d	",i)
               i++;
            }
            getch();
    }

  2. #2
    Bit Fiddler
    Join Date
    Sep 2009
    Posts
    67
    I've got a problem understanding the logics behind the indentation in this piece of code.

    c counts times i % j = 0, which will be at least two for every numbers above 1 (j = 1 & j = i). So if c is less or more than two, (divisible with more or less numbers than 1 & i) i is not a prime.

  3. #3
    Registered User
    Join Date
    Feb 2011
    Posts
    14
    but that program is working, i tried it.. i dont understand why the condition of c=2 is used to print the primes.

  4. #4
    The Dragon Reborn
    Join Date
    Nov 2009
    Location
    Dublin, Ireland
    Posts
    629
    because a prime is only divisible by itself and 1. hence if is C is 2...then it is a prime number...c is merely a counter
    You ended that sentence with a preposition...Bastard!

  5. #5
    Registered User
    Join Date
    Jan 2011
    Posts
    141
    as fader_berg mentioned...

    any prime number above greater thn 1 is only divisibly by 1 or itself.

    example, say you enter 5 terms.
    5%1=0 --->c=1
    5%2=1
    5%3=2
    5%4=1
    5%5=0---->c=2

    so say you want first 5 prime numbers =n

    you initialise i=1. Is 1<5? yes, so enter the while loop

    enter the for loop since j=1 and j<=i i.e.1<=1

    1%1=0 so increment c from 0 to 1.

    you CAN'T enter the for loop again since j=2 and 2<=1. So fall through the loops and go to i++

    i=2 now

    enter the for while loop since 2<=5
    enter the if loop and check if 1%1=0. It does so increment c.
    c=1

    Run through the for loop again this since j=2 and 2<=2
    enter the if loop and see if 2%2=0. It does so increment c to 2. c=2

    Leave the for loop amd go to the second if statement. It says if c==2 print i.
    So it will print 2

    now follow the same procedure for 3, 4 etc...

  6. #6
    Registered User
    Join Date
    Feb 2011
    Posts
    14
    Quote Originally Posted by bos1234 View Post
    as fader_berg mentioned...

    any prime number above greater thn 1 is only divisibly by 1 or itself.

    example, say you enter 5 terms.
    5%1=0 --->c=1
    5%2=1
    5%3=2
    5%4=1
    5%5=0---->c=2

    so say you want first 5 prime numbers =n

    you initialise i=1. Is 1<5? yes, so enter the while loop

    enter the for loop since j=1 and j<=i i.e.1<=1

    1%1=0 so increment c from 0 to 1.

    you CAN'T enter the for loop again since j=2 and 2<=1. So fall through the loops and go to i++

    i=2 now

    enter the for while loop since 2<=5
    enter the if loop and check if 1%1=0. It does so increment c.
    c=1

    Run through the for loop again this since j=2 and 2<=2
    enter the if loop and see if 2%2=0. It does so increment c to 2. c=2

    Leave the for loop amd go to the second if statement. It says if c==2 print i.
    So it will print 2

    now follow the same procedure for 3, 4 etc...
    thanks a lot.. now i got the idea behind the logic. Am just a beginner in C. thanks again and keep helping.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Hi, Quiz C program Assignment updated
    By Eman in forum C Programming
    Replies: 19
    Last Post: 11-22-2009, 04:50 PM
  2. Need help with my program...
    By Noah in forum C Programming
    Replies: 2
    Last Post: 03-11-2006, 07:49 PM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 07:39 AM
  4. logic on a program
    By officegirl in forum C Programming
    Replies: 0
    Last Post: 10-13-2001, 11:41 PM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 11:04 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21