Prime number

This is a discussion on Prime number within the C Programming forums, part of the General Programming Boards category; b % a gives you the remainder of the division of b by a. Ex : 9 % 3 = ...

  1. #1
    Fear the Reaper...
    Join Date
    Aug 2005
    Location
    Toronto, Ontario, Canada
    Posts
    625
    b % a gives you the remainder of the division of b by a.

    Ex :
    9 % 3 = 0
    9 % 2 = 1
    9 % 5 = 4
    etc...

    EDIT : How the heck did this get posted at the top ?
    Teacher: "You connect with Internet Explorer, but what is your browser? You know, Yahoo, Webcrawler...?" It's great to see the educational system moving in the right direction

  2. #2
    Registered User
    Join Date
    Oct 2006
    Posts
    10

    Red face Prime number

    Hi all here is code below i did to find a prime number as far as i m concerned it works fine .
    Code:
    #include<stdio.h>
    #include<conio.h>
    main()
    {
    int a=1;
    int b;
    int count =0;
    clrscr();
    scanf("%d",&b);
    //checks the programs
    if ( b < 0 )
    {
    printf("\n\n\tEnter a postive number program ended.",b);
    return 0;
    }//the program will not be executed if the user has entered a negative number,
    //start for the prime numbers check.
    while (a < b)
    {
    if ( b % a == 0)//check if the given number has a remainder of the previous numbers.
    {
    	/*Enable this printf is you want to check on which numbers
    	the the remainder is zero.
    	printf ("the remainder is zero on %d\n",a);
    	 */
    count++;//a counter to dertmine how many times the given number has remainder zero.
    }
    	 /*
    	Enable this part of the program if you want to check on which numbers
    	the remainder is not zero.
    	else if ( b % a != 0)
    	{
    	printf("the raimder is not zero on %d\n",a);
    	}*/
    a++;
    }
    //this part of the program uses the counter to print if the number has more then 2
    //divisors on which the remainder is zero then it will print the results
    
    if (count >= 2)
    {
    printf("\nThe number %d is not prime ",b);
    }
    else if ( count <= 2)
    {
    printf("\n the number %d is prime ",b);
    }
    //program ended
    
    return 0;
    }
    and anyone can explain me the correct use of "%"
    thanks in advance
    edit: how tht post got up there

  3. #3
    Eager young mind
    Join Date
    Jun 2006
    Posts
    342
    You are talking about the operator right? It just gives you the remainder of the division operation. for example , in your code :

    ( b % a == 0 )

    b%a is zero when the division of b by a gives you no remainder, that is, b is a multiple of a
    In the middle of difficulty, lies opportunity

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. xor linked list
    By adramalech in forum C Programming
    Replies: 23
    Last Post: 10-14-2008, 10:13 AM
  2. largest number prime number that can be produced...
    By ElemenT.usha in forum C Programming
    Replies: 8
    Last Post: 02-17-2008, 12:44 AM
  3. Prime Factor Fxn
    By alpha in forum C++ Programming
    Replies: 2
    Last Post: 10-21-2003, 10:44 AM
  4. Prime Number Generator... Help !?!!
    By Halo in forum C++ Programming
    Replies: 9
    Last Post: 10-20-2003, 07:26 PM
  5. Replies: 3
    Last Post: 01-14-2003, 09:34 PM

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