newline loop

This is a discussion on newline loop within the C Programming forums, part of the General Programming Boards category; Code: #include <stdio.h> #include <math.h> int isprime(int n); int main(void) { int i; int count=1; printf( "The prime numbers from ...

  1. #1
    Registered User
    Join Date
    Apr 2011
    Posts
    22

    newline loop

    Code:
    #include <stdio.h>
    #include <math.h>
    
    int isprime(int n); 
    
    int main(void)
    {
    int i; 
    int count=1; 
    printf( "The prime numbers from 1 to 1000 are \n" );
    for ( i = 2;i < 1000;i++){
    	if (isprime(i) && count%10!=0)
    		printf("%d\t",i);
    		count++;
    	if (isprime(i) && count%10==0) 
    		printf("%d\n",i);
    		count++;
    }}
    int isprime( int n )
    {
    int sroot = sqrt(n);
    int i;
    for ( i = 2;i <= sroot;i++){
    	if (n % i == 0)
    		{return 0;}
    	}
    return 1;
    }
    i want a newline to be created after every 10th prime number and the if statements for int count do not work. Suggestions?
    Last edited by lancehumiston; 04-01-2011 at 02:11 AM.

  2. #2
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    count % 10 can give you from 0-9 only.
    You need count % 11.
    And don't just tell it's not working. How it's not working!?

    You don't need to test twice.
    If count variable is supposed to keep track of number of prime printed, it should start from zero. Isn't it?

    Code:
    count = 0;                                       
    for ( i = 2;i < 1000;i++){
    	if (isprime(i)) {
    		printf("%d",i);
    		count++;
            }
    	if ( count%11 ==0)  {
    		putchar('\n');
            } else {
                   putchar('\t');
           }
    }
    printf("%d primes!\n",count);
    Last edited by Bayint Naung; 04-01-2011 at 02:20 AM.

  3. #3
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,640
    Code:
    	if (isprime(i) && count%10!=0) {
    		printf("%d\t",i);
    		count++;
    	}
    	if (isprime(i) && count%10==0) {
    		printf("%d\n",i);
    		count++;
    	}
    You need braces around those statements. Without them, only the printfs get put inside the if block, and you do count++ twice per loop. Considering you start count at 1, you then go to 3, 5, 7, 9, 11, etc, skipping every multiple of 10. A better solution would be to keep common code out of the if statements, and to use an else instead of you strange two-if setup:
    Code:
    if (isprime(i)) {
        if (count % 10) {  // count is not a multiple of 10, print a tab
            printf("%d\t", i);
        }
        else {  // count is a multiple of 10, print a new line
            printf("%d\n", i);
        }
        count++;  // only increment count once
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. My loop within loop won't work
    By Ayreon in forum C Programming
    Replies: 3
    Last Post: 03-18-2009, 11:44 AM
  2. Personal Program that is making me go wtf?
    By Submeg in forum C Programming
    Replies: 20
    Last Post: 06-27-2006, 01:13 AM
  3. A somewhat bizzare problem!!! - WHILE LOOP
    By bobthebullet990 in forum C Programming
    Replies: 3
    Last Post: 03-31-2006, 07:19 AM
  4. loop issues
    By kristy in forum C Programming
    Replies: 3
    Last Post: 03-05-2005, 09:14 AM
  5. comparing int to newline character
    By RedZippo in forum C++ Programming
    Replies: 5
    Last Post: 05-13-2004, 07:37 PM

Tags for this Thread


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