C Program to Check Whether a Number is Prime or Not Using Function फंक्शन का उपयोग करके दी गई संख्या Prime है या नहीं ज्ञात करने का C प्रोग्राम
A prime number is a natural number greater than 1 that is divisible only by 1 and itself. In C language, we can use a user-defined function to check whether a given number is prime or not.
Prime Number वह प्राकृतिक संख्या होती है जो 1 से बड़ी होती है तथा केवल 1 और स्वयं से विभाजित होती है। C भाषा में User-Defined Function का उपयोग करके जाँच सकते हैं कि दी गई संख्या Prime है या नहीं।
C Program
#include <stdio.h>int prime(int y);int main(void){int n, x;printf("Enter a number: ");scanf("%d", &n);x = prime(n);if (x == 1)printf("%d is a prime number\n", n);elseprintf("%d is not a prime number\n", n);return 0;}int prime(int y){int i;if (y <= 1)return 0;for (i = 2; i <= y / i; i++){if (y % i == 0)return 0;}return 1;}
Output
Example 1:
Enter a number: 77 is a prime number
Example 2:
Enter a number: 1212 is not a prime number
Example 3:
Enter a number: 11 is not a prime number
Explanation (व्याख्या)
The user enters an integer number.
यूजर एक integer number इनपुट करता है।
The
prime()function is called by passing the number as an argument.संख्या को argument के रूप में पास करके
prime()function को call किया जाता है।If the number is less than or equal to 1, the function returns 0.
यदि संख्या 1 या उससे छोटी है, तो function 0 return करता है।
The
forloop checks whether the number is divisible by any integer from 2 up to its square root.forloop जाँचता है कि संख्या 2 से उसके वर्गमूल तक किसी पूर्णांक से विभाजित होती है या नहीं।If a divisor is found, the function returns 0; otherwise, it returns 1.
यदि कोई भाजक मिल जाता है, तो function 0 return करता है, अन्यथा 1 return करता है।
The
main()function displays the result.main()function परिणाम प्रदर्शित करता है।
Important Points
prime()is a user-defined function.The function returns 1 for a prime number and 0 for a non-prime number.
1 is neither prime nor composite.
Checking divisors up to the square root reduces unnecessary iterations.
Comments
Post a Comment