Check if A Number is a Composite number or Not in C++

(652 Views)


What is a Composite Number?

A composite number is a positive integer number that is not prime number. In other words, it is a positive integer number which has at least one divisor other than 1 and itself. The composite numbers up to 20 are 4, 6, 8, 9, 10, 12, 14, 15, 16, 18, 20.

For example:
Input: 10
Output: yes, its Composite (Because 10 has divisors other than 1 and itself, for ex, 2 or 5).

Input: 5
Output: no, its not Composite (Because 5 is a prime number, as it has no other divisors other than 1 and itself).
Composite Number Program Implementation in C++

Write a program to check if the user input is a Composite Number or not in C++

Code:

#include <iostream> using namespace std; bool isComposite(unsigned int i) { for( unsigned int j=2;j<i;j++) { if(i%j==0) { return true; } } return false; } int main() { unsigned int input = 1000; cout<<"Composite no. between 0 to "; cout<<input<<" are :\n\n"; for( unsigned int i=3;i<=input;i++) { if(isComposite(i)) { cout<<i<<" "; } } return 0; }

Solution Worked 0 UpvotesUpvote

        

Solution Didn't Worked 0 DownvotesDownvote



Comments



Search
Play 2048 Game Online

Search Tags

    C++ program for Composite number

    Find if a number is a composite number using C++