Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    Regular Coder
    Join Date
    Feb 2006
    Posts
    110
    Thanks
    23
    Thanked 0 Times in 0 Posts

    to get 10 output in each line.

    i'm writing a code which is to be shown in such a way that there are 10 prime numbers in each line.

    here's the code but it displays everything in one line:
    Code:
    main ()
    {
        int r1=1,r2,s,p,c,flag;
        printf ("\n Enter the Range : ");
        scanf ("%d",&r2);
        printf ("\n The Prime Numbers in the Given range are : \n");
        for (s=r1;s<=r2;s++)
            {
            flag =0;
            for (p=2;p<s;p++)
                {
                c=s%p;
                if (c==0)
                    {
                    	flag=1;
                    	break;
                	}
                }
            if ((flag==0) && (s!=1))
            printf ("\t %d",s);
        	}
     }
    i couldn't figure a way out to have the out in lines of 10 digits each.
    for example:
    Code:
    2       3       5       7       11      13      17      19      23      29       
    31     37     41     43      47      53      59      61      67      71       
    73     79     83     89      97
    any help would be greatly appreciated.

  • #2
    Supreme Overlord Spookster's Avatar
    Join Date
    May 2002
    Location
    Marion, IA USA
    Posts
    6,280
    Thanks
    4
    Thanked 83 Times in 82 Posts
    Seems pretty simple and straightforward. Inside your loop you'll need to keep a count of how many numbers have been computed and maybe set a boolean flag that lets you know each time 10 numbers have been computed. Since you only care about every tenth number just reset the count each time it hits 10. You will also need a conditional block that checks that flag and then either executes a print statement with a carriage return else one without.
    Spookster
    CodingForums Supreme Overlord
    All Hail Spookster

  • #3
    Regular Coder
    Join Date
    Feb 2006
    Posts
    110
    Thanks
    23
    Thanked 0 Times in 0 Posts
    thanx for the reply.

    could you please help me with the code. i cant seem to figure out what you are trying to say. besides, m not so good with the codes.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •