C Program to find Composite Number

Last Updated:

What is Composite Number?

A positive integer that has at least one divisor other than 1 and itself.

Tips: It is recommended to use our online Composite Numbers calculator for better understanding.

Examples

The following table provides few examples of composite numbers.

NumberDivisorResult
13 1, 13 Not a Composite Number
15 1, 3, 5, 15 Composite Number
47 1, 47 Not a Composite Number

Using for loop

In the following example, we will check whether the number 12 is a Composite number or not using for loop.

Example

C Compiler
#include <stdio.h> int main() { int num = 12; int i; int count = 0; for(i=1; i<=num; i++) { if(num % i == 0) count++; } if(count > 2) printf("%d is a composite number", num); else printf("%d is not a composite number", num); return 0; }

Using while loop

In the following example, we will check whether the number 12 is a Composite number or not using while loop.

Example

C Compiler
#include <stdio.h> int main() { int num = 12; int i = 1; int count = 0; while(num >= i) { if(num % i == 0) count++; i++; } if(count > 2) printf("%d is a composite number", num); else printf("%d is not a composite number", num); return 0; }

Using do while loop

In the following example, we will check whether the number 12 is a Composite number or not using do while loop.

Example

C Compiler
#include <stdio.h> int main() { int num = 12; int i = 1; int count = 0; do{ if(num % i == 0) count++; i++; }while(i <= num); if(count > 2) printf("%d is a composite number", num); else printf("%d is not a composite number", num); return 0; }

Composite Numbers between the Given Range

In the following example, we will find all the Composite numbers between 1 and 10.

Example

C Compiler
#include <stdio.h> int main() { int start = 1; int end = 10; int count = 0; int i = 1; printf("Composite Numbers between %d and %d:\n", start, end); for(start=start; start<=end; start++) { for(i=1; i<=start; i++) { if(start % i == 0) count++; } if(count > 2) printf("%d ", start); count = 0; } return 0; }

Check Whether the Given Number is Prime or Composite

In the following example, we will check whether the given number is a Prime number or Composite number.

Example

C Compiler
#include <stdio.h> int main() { int num, i; int count = 0; printf("Enter a (int) number: "); scanf("%d", &num); for(i=1; i<=num; i++) { if(num % i == 0) count++; } if(count == 2) printf("%d is a prime number", num); else printf("%d is a composite number", num); return 0; }

Share this Page

Meet the Author