Front-end Tutorials

Front-end Tutorials

HTMLCSSSassJavaScriptReactJS
CMS Tutorials

CMS Tutorials

WordPress
Tutorials expand

C Program to find Composite Number

Posted in C Tutorial
Updated on Oct 06, 2024
By Mari Selvan
πŸ‘οΈ 3014 - Views
⏳ 4 mins
πŸ’¬ 1 Comment
C Program to find Composite Number

Photo Credit to CodeToFun

πŸ™‹ Introduction

In the realm of programming, identifying and working with different types of numbers is a fundamental skill.

A composite number is one that has more than two positive divisors, excluding 1 and itself. In this tutorial, we'll explore a C program that efficiently determines whether a given number is composite or not.

πŸ“„ Example

Let's dive into the C code that checks whether a number is composite or not.

isComposite.c
Copied
Copy To Clipboard
#include <stdio.h>

// Function to check if a number is composite
int isComposite(int number) {
  if (number <= 1) {
    return 0; // 0 and 1 are not composite
  }

  for (int i = 2; i <= number / 2; ++i) {
    if (number % i == 0) {
      return 1; // Number has a divisor other than 1 and itself
    }
  }

  return 0; // Number is not composite
}

// Driver program
int main() {
  // Replace this value with your desired number
  int num = 12;

  // Call the function to check if the number is composite
  if (isComposite(num)) {
    printf("%d is a composite number.\n", num);
  } else {
    printf("%d is not a composite number.\n", num);
  }

  return 0;
}

πŸ’» Testing the Program

To test the program with a different number, replace the value of num in the main function.

Output
12 is a composite number.

Compile and run the program to check if the given number is composite or not.

πŸ€“ Understanding the Logic

  1. The program defines a function isComposite that takes an integer as input and returns 1 if the number is composite and 0 if it is not.
  2. Inside the function, it iterates through numbers from 2 to half of the input number.
  3. For each iteration, it checks if the current number is a divisor of the input number.
  4. If it finds a divisor, the number is composite; otherwise, it is not.

πŸ“ Between the Given Range

Let's delve into the C code that checks for composite numbers in the specified range.

isComposite.c
Copied
Copy To Clipboard
#include <stdio.h>

// Function to check if a number is composite
int isComposite(int num) {
  if (num <= 1) {
    return 0; // 0 and 1 are not composite numbers
  }

  for (int i = 2; i < num; ++i) {
    if (num % i == 0) {
      return 1; // The number has a divisor other than 1 and itself
    }
  }

  return 0; // The number is prime
}

// Driver program
int main() {
  printf("Composite numbers in the range 1 to 10 are: \n");

  for (int i = 1; i <= 10; ++i) {
    if (isComposite(i)) {
      printf("%d ", i);
    }
  }

  printf("\n");

  return 0;
}

πŸ’» Testing the Program

Output
Composite numbers in the range 1 to 10 are: 
4 6 8 9 10

The program is designed to check and print composite numbers in the range 1 to 10. To test the program, simply compile and run it.

🧠 How the Program Works

  1. The program defines a function isComposite that takes a number as input and returns 1 if the number is composite and 0 if it is prime.
  2. Inside the function, it iterates from 2 to the number and checks for any divisor other than 1 and itself.
  3. The main function tests the isComposite function for numbers in the range 1 to 10 and prints the composite numbers.

🧐 Understanding the Concept of Composite Numbers

Composite numbers are integers greater than 1 that have divisors other than 1 and themselves.

For example, the number 12 is composite because it can be formed by multiplying 2 and 6 or 3 and 4.

🎒 Optimizing the Program

While the provided program is effective, consider exploring and implementing optimizations for larger numbers, such as checking divisors up to the square root of the number for improved efficiency.

Feel free to incorporate and modify this code as needed for your specific use case. Happy coding!

πŸ‘¨β€πŸ’» Join our Community:

To get interesting news and instant updates on Front-End, Back-End, CMS and other Frameworks. Please Join the Telegram Channel:

Author

author
πŸ‘‹ Hey, I'm Mari Selvan

For over eight years, I worked as a full-stack web developer. Now, I have chosen my profession as a full-time blogger at codetofun.com.

Buy me a coffee to make codetofun.com free for everyone.

Buy me a Coffee

Share Your Findings to All

Subscribe
Notify of
guest
1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Mari Selvan
Mari Selvan
11 months ago

If you have any doubts regarding this article (C Program to find Composite Number) please comment here. I will help you immediately.

We make use of cookies to improve our user experience. By using this website, you agree with our Cookies Policy
AgreeCookie Policy