Dart Programming to Check Prime and Composite Numbers

In programming, checking whether a number is prime or composite is a common problem that helps reinforce fundamental concepts like loops, conditionals, and number theory. This post will explain what these numbers are and show you how to check them using Dart programming.

Dart Program to Check Prime and Composite Numbers

Getting Started

When learning Dart, especially in the context of problem-solving or algorithm development, understanding how to work with prime and composite numbers is essential. Here we will see how to write a simple Dart program to determine whether a given number is prime or composite.

What Are Prime and Composite Numbers?

Prime Number

Prime and numbers are a natural number greater than 1 that has exactly two distinct positive divisors: 1 and itself.

Characteristics of Prime Numbers:
  • It can only be divided evenly by 1 and itself.
  • It cannot be formed by multiplying two smaller natural numbers (other than 1 × itself).
Example : 2, 3, 5, 7, 11, 13

Note:- 2 is the only even prime number. All other even numbers are divisible by 2, so they are composite.

Composite Number

A composite number is a natural number greater than 1 that has more than two positive divisors.

Characteristics of Composite Numbers:
  • It can only be divided evenly by numbers other than just 1 and itself.
  • In other words, a composite number has at least one additional factor besides 1 and itself.
Example : 4, 6, 8, 9, 10

Note: 1 is neither prime nor composite number.

Check Prime and Composite Numbers

 import 'dart:io';  
 void main() {  
  stdout.write("Enter a positive integer: ");  
  int? number = int.tryParse(stdin.readLineSync()!);  
  if (number == null || number < 1) {  
   print("Please enter a valid positive integer greater than 0.");  
   return;  
  }  
  if (number == 1) {  
   print("1 is neither prime nor composite.");  
   return;  
  }  
  bool isPrime = true;  
  for (int i = 2; i <= number ~/ 2; i++) {  
   if (number % i == 0) {  
    isPrime = false;  
    break;  
   }  
  }  
  if (isPrime) {  
   print("$number is a prime number.");  
  } else {  
   print("$number is a composite number.");  
  }  
 }  

Sample Output
 Enter a positive integer: 7  
 7 is a prime number.  
 Enter a positive integer: 10  
 10 is a composite number.  
 Enter a positive integer: 1  
 1 is neither prime nor composite.  

How It Works:
  1. User Input: The program prompts the user to enter a number.
  2. Validation: It checks if the input is a valid positive integer.
  3. Special Case for 1: The program acknowledges that 1 is neither prime nor composite.
  4. Prime Check Logic: It loops from 2 to number / 2 and checks for divisors.
  5. Output: Based on the loop result, it prints whether the number is prime or composite.

Summary

This Dart program provides a straightforward way to classify numbers as prime or composite using basic logic. It's a great exercise for beginners looking to strengthen their understanding of control flow and number theory in Dart.

Thanks

Kailash Chandra Behera

An IT professional with over 13 years of experience in the full software development life cycle for Windows, services, and web-based applications using Microsoft .NET technologies. Demonstrated expertise in delivering all phases of project development—from initiation to closure—while aligning with business objectives to drive process improvements, competitive advantage, and measurable bottom-line gains. Proven ability to work independently and manage multiple projects successfully. Committed to the efficient and effective development of projects in fast-paced, deadline-driven environments. Skills: Proficient in designing and developing applications using various Microsoft technologies. Total IT Experience: 13+ years

Previous Post Next Post

نموذج الاتصال