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 ?
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 = ...
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
Hi all here is code below i did to find a prime number as far as i m concerned it works fine .
and anyone can explain me the correct use of "%"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; }
thanks in advance
edit: how tht post got up there
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