Thu Oct 31 2019

Prime Composite

C++ Programming1739 views

File Name: prime-composit.cpp

#include<iostream>
using namespace std;

int main() {
	int i, no;

	/* Declear boolean type variable in C++ */
	bool flag = true;

	/* 'endl' insert linefeed like '\n' */
	cout << "Check the given number is Prime or Composite" << endl;
	cout << "Enter a number:" << endl;
	cin >> no;
	i = no / 2;
	
	/* Loop the process using "while loop" */
	while(i >= 2) {
		if(no % i == 0) {
			flag = false;
			cout << "It's a Composite number!" << endl;
			break;
		}
		i = i - 1;
	}

	if(flag)
		cout << "It's a Prime Number!" << endl;
	return 0;
}



/* Output */
/*
Check the given number is Prime or Composite
Enter a number:
23

It's a Prime Number!
*/


/* ------------------------------- */


/*
Check the given number is Prime or Composite
Enter a number:
99

It's a Composite Number!
*/
Reference:

We use cookies to improve your experience on our site and to show you personalised advertising. Please read our cookie policy and privacy policy.