Computer for SEE and NEB

It is a complete SEE and NEB solution for computer science. It includes Computer Fundamentals, Database (SQL), Programming in C QBASIC, CSS, JavaScript, and PHP for beginners.

Breaking

Post Top Ad

Your Ad Spot

Sunday, July 31, 2022

C Program to Check Whether an Input Number is Prime or Composite

 

C Program to Check Whether an Input Number is Prime or Composite




C Program to Check Whether an Input Number is Prime or Composite


Introduction:

In the world of computer programming, prime numbers are an essential topic. Prime numbers are those natural numbers that are only divisible by 1 and themselves. In this blog article, we will discuss how to write a C program to check whether an input number is prime or composite.

Before diving into the program, let's first understand the basic concepts of prime and composite numbers.

A prime number is a natural number greater than 1 that has no positive divisors other than 1 and itself. For example, 2, 3, 5, 7, 11, and 13 are prime numbers.

On the other hand, composite numbers are natural numbers that have more than two positive divisors. For example, 4, 6, 8, 9, 10, and 12 are composite numbers.

Now, let's discuss how to write a C program to check whether an input number is prime or composite.


Coding:

#include<stdio.h>
int main()
{
int num,test=0,i;
printf("Enter any positive number\n");
scanf("%d",&num);
for(i=1;i<=num;i++)
{
if(num%i==0)
{
test++;
}
}
if(test==2)
printf("The number is prime\n");
else
printf("The number is composite\n");
return 0;
}

Output:





Conclusion:

In conclusion, we have explored how to write a C program to check whether an input number is prime or composite. By using a simple algorithm to iterate over potential divisors, we can efficiently determine the nature of the input number. This program can be useful for a wide variety of applications, from mathematical research to computer science and beyond.

No comments:

Post a Comment

Post Top Ad

Your Ad Spot

Pages