Prime Numbers: C Program Example and Explanation

28 Dec 2022 Balmiki Kumar 0 C Programming

Program to check whether the given number is a prime or not.

 ⇒ A number that has only two factors is called a prime number

⇒ Numbers that can only be divided by itself and 1 without remainder is called prime number.

⇒ A prime number is a natural number that has only one and itself as factors.

Example:- 2,3,5,7,11,13,17,21,23,27


Prime Numbers: C Program Example and Explanation

#include<stdio.h>
void main()
{
    int i,n,c=0;
    printf("enter any number:\n");
    scanf("%d",&n);
    for(i=1;i<=n;i++)
    {
    if(n%i==0)
    c++;// count when the condition is met
    }
    if(c==2)
    printf("number is prime:");
    else
    printf("number is not prime:");
        
}

output: 

Enter the n: 5
5 is prime
Enter the n: 8
8 is  not prime

Top Resources

Prime Numbers: C Program Example and Explanation

Design a function to check the given number is prime or not if prime return 1 else 0

Write a C Program to Check if a Number is Prime or Not Using For Loop

Writing a C Program to Print Prime Numbers Between 50 and 100

write a program to print prime numbers in 10 array elements.

Write a C program to input 10 numbers through the keyword and find the number of prime counts it stores the into a separate array and display it.

Further Reading:

 For further information and examples, Please visit[ C-Programming From Scratch to Advanced 2023-2024]

 

Note: If you encounter any issues or specific errors when running this program, please let me know and I'll be happy to help debug them!

BY: Balmiki Kumar

Related Blogs

Post Comments.

Login to Post a Comment

No comments yet, Be the first to comment.