answersLogoWhite

0


Best Answer

The brute force way would be to iterate through all the integers smaller than the sought number and check the number is divisible by each of them. However this algorithm is extremely inefficient, but improving upon the simple idea, we can come up with an algorithm:

#include <stdio.h>

#include <stdlib.h>

#include <math.h>

/*

*

*/

int main(int argc, char** argv) {

int checked = 72; //put your number that you want to check

int temp = 2; //the temporary variable to check against

char isPrime = 1; //intially assume it is prime

if (checked != 1 checked != 2) {

while (temp <= sqrt((double) checked)) { //only have to check up to the square root of checked

if (checked % temp 0) {

temp += 1; //only have to check odd numbers } else {

temp += 2; } } }

if (isPrime) {

printf("Checked is prime!\n"); } else {

printf("Checked is not prime!\n"); }

return (EXIT_SUCCESS);

}

or a better edited version here: http://img689.imageshack.us/img689/8164/primecodec.png

User Avatar

Wiki User

14y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Write a C program To check whether given number is a prime number or not?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

Write a program By using if else statement to read a number and check whether it is positive or negative?

write a c++program by using if statement to read a number and check whether it is positive or negative


Lab manual in mca 1st sem from ignou?

write a program in C to check whether a given number is apalindrome or not


How do you write a C program to check whether the number is odd or even Ps-using DEV complier?

To write a C program to determine if something is odd or even you need to be a programmer. To write a program in C is complicate and only done by programmers.


Could you write a assembly language program in tasm To check whether a given number present in a sequence of given memory location containing the string to be checked in 8086?

8086 assembly language program to check wether given number is perfect or not


Can you write a C program to check whether a string is an IP address or not?

Use the Exception class and type converters.


Write an algorithm to check whether a number is a prime number or not?

You can write out this algorithm. This will then be programmed into the device to make determining prime numbers easier.


Write a c program to check whether a string follows English capitalization rules?

Active Time spent online


Write an algorithm to check whether the given number is odd or even?

Type your answer here... i think we should first enter 1 number then check it


Write a c program using while loop to accept a number check and display message whether number is perfect or not?

The program is here guys.......... //Finding whether the given number is perfect or not //Starts here #include&lt;stdio.h&gt; void main() { int i=1,temp=0,number; scanf("%d",&amp;number); while(i&lt;=number/2){ if(number%i==0) temp+=i; i++; } if(temp==number) printf("Its a perfect number"); else printf("Its not a perfect number"); } //ends here


Write a shell programs for to check whether the given number is an Avogadro number or not?

Avogadro's number is a constant. Therefore only one number is equal to Avogadro's number.


Write a 8085 microprocessor program to check whether a give number is even or odd?

Lxi h, 2000h mov a,m mov b,a inx h mov a,m add b sta 2002h hlt


Write a PHP program to check whether the string is palindrome or not?

You can do this: &lt;?php if ( $word === strrev( $word ) ) { echo "The word is a palindrome"; } else { echo "The word is not a palindrome"; }