Search This Blog
Welcome to 1printf(), your ultimate destination for C, C++, Linux, Data Structures, and Microcontroller programming! ๐ ๐นLearn advanced coding techniques in C& C++ ๐นMaster Linux internals & shell scripting ๐นDeep dive into Data Structures & Algorithms ๐นExplore Embedded Systems & Microcontrollers (8051,UART, RTOS) ๐นGet hands-on coding tutorials, project ideas,and interview preparation tips Whether you're a beginner or an experienced programmer, this channel will help you
Featured
- Get link
- X
- Other Apps
C Program: Prime Number Check
๐ท C Program: Prime Number Check
#include<stdio.h>
int main( )
{
int num, count = 0;
printf("Enter the number:\n");
scanf("%d", &num);
if(num <= 1)
{
printf("%d is not a prime number:\n", num);
}
else
{
for(int i = 2; i <= num / 2; i++)
{
if(num % i == 0)
{
count++;
break;
}
}
if(count == 0)
{
printf("%d is a prime number:\n", num);
}
else
{
printf("%d is not a prime number:\n", num);
}
}
}
๐ Explanation:
This C program checks if a number is prime or not.
๐ A **prime number** is a number greater than 1 that is divisible only by 1 and itself.
๐ธ First, the user enters a number.
๐ธ If the number is less than or equal to 1, it's not prime.
๐ธ If the number is greater than 1, the program checks if it has any divisors (from 2 to num/2).
๐ธ If any divisor is found, `count` is incremented, and the loop breaks early for efficiency.
๐ธ Finally, if no divisors are found (`count == 0`), it's a prime number.
๐ Sample Output:
Enter the number:
7
7 is a prime number:
Enter the number:
10
10 is not a prime number:
Enter the number:
1
1 is not a prime number:
๐ท️ Keywords:
C program to check prime number, prime number logic in C, beginner C programs, isPrime function, modulus operator in C, number theory in C
Popular Posts
C++ Program for Hybrid Inheritance (All Types Together)
- Get link
- X
- Other Apps
C++ Program for Function Overloading Example
- Get link
- X
- Other Apps
Comments
Post a Comment