C# Basic
C# Interview Programs
- C# Interview Programs
- C# Abundant Number
- C# Amicable Number
- C# Armstrong Number
- C# Average of N Numbers
- C# Automorphic Number
- C# Biggest of three numbers
- C# Binary to Decimal
- C# Common Divisors
- C# Composite Number
- C# Condense a Number
- C# Cube Number
- C# Decimal to Binary
- C# Decimal to Octal
- C# Disarium Number
- C# Even Number
- C# Evil Number
- C# Factorial of a Number
- C# Fibonacci Series
- C# GCD
- C# Happy Number
- C# Harshad Number
- C# LCM
- C# Leap Year
- C# Magic Number
- C# Matrix Addition
- C# Matrix Division
- C# Matrix Multiplication
- C# Matrix Subtraction
- C# Matrix Transpose
- C# Maximum Value of an Array
- C# Minimum Value of an Array
- C# Multiplication Table
- C# Natural Number
- C# Number Combination
- C# Odd Number
- C# Palindrome Number
- C# Pascalβs Triangle
- C# Perfect Number
- C# Perfect Square
- C# Power of 2
- C# Power of 3
- C# Pronic Number
- C# Prime Factor
- C# Prime Number
- C# Smith Number
- C# Strong Number
- C# Sum of Array
- C# Sum of Digits
- C# Swap Two Numbers
- C# Triangular Number
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.
using System;
class Program {
// Function to check if a number is composite
static bool IsComposite(int number) {
if (number <= 1)
return false;
for (int i = 2; i <= Math.Sqrt(number); i++) {
if (number % i == 0)
return true;
}
return false;
}
// Driver program
static void Main() {
// Replace this value with your desired number
int testNumber = 12;
// Check if the number is composite
if (IsComposite(testNumber))
Console.WriteLine($"{testNumber} is a composite number.");
else
Console.WriteLine($"{testNumber} is not a composite number.");
}
}
π» Testing the Program
To test the program with a different number, replace the value of testNumber in the Main function.
12 is a composite number.
Compile and run the program to see if the given number is composite.
π§ How the Program Works
- The program defines a function IsComposite that takes an integer as input and returns true if the number is composite, and false otherwise.
- Inside the function, it checks if the given number is less than or equal to 1. If so, it returns false since 1 and numbers less than 1 are not composite.
- It then iterates from 2 to the square root of the given number and checks if any of these values evenly divide the number. If so, the number is composite.
- The Main function tests the program by providing a test number and prints whether it is composite or not.
π Between the Given Range
Let's delve into the C# code that checks for composite numbers in the range of 1 to 10.
using System;
class Program {
// Function to check if a number is composite
static bool IsComposite(int number) {
if (number < 2)
return false;
for (int i = 2; i < number; i++) {
if (number % i == 0)
return true;
}
return false;
}
// Driver program
static void Main() {
Console.WriteLine("Composite numbers in the range 1 to 10:");
// Check numbers from 1 to 10
for (int i = 1; i <= 10; i++) {
if (IsComposite(i))
Console.Write($"{i} ");
}
Console.WriteLine();
}
}
π» Testing the Program
Composite numbers in the range 1 to 10 are: 4 6 8 9 10
The program is designed to check and display composite numbers in the range of 1 to 10. Run the program, and it will output the composite numbers in that range.
π§ How the Program Works
- The program defines a function IsComposite that takes a number as input and returns true if the number is composite, and false otherwise.
- Inside the function, it checks if the number has any divisors other than 1 and itself.
- The Main method then checks and prints the composite numbers in the range of 1 to 10 using a for loop.
π§ 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:
Author
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
If you have any doubts regarding this article (C# Program to find Composite Number), please comment here. I will help you immediately.