BZU PAGES: Find Presentations, Reports, Student's Assignments and Daily Discussion; Bahauddin Zakariya University Multan

BZU PAGES: Find Presentations, Reports, Student's Assignments and Daily Discussion; Bahauddin Zakariya University Multan (http://bzupages.com/)
-   Object Oriented Programming (http://bzupages.com/300-object-oriented-programming/)
-   -   Program to enter an integer and print if it is prime or composite (http://bzupages.com/f300/program-enter-integer-print-if-prime-composite-14558/)

bonfire 01-03-2011 02:39 PM

Program to enter an integer and print if it is prime or composite
 
1 Attachment(s)
Program to enter an integer and print if it is prime or composite

#include
#include
#include

void main()
{
clrscr();
int num1,x;
cout << "Enter an integer : " << endl;
cin>>num1;
for(x=2;x {
if(num1%x==0)
{
cout << num1 << " is a composite number." << endl;
getch();
exit(0);
}
else
{
cout << num1 << " is a prime number." << endl;
getch();
exit(0);
}
}
}
This program takes in an integer num1 as a screen input from the user.
It then determines whether the integer is prime or composite.
It finally outputs the approriate message by writing to the 'cout' stream.

Sample Input
23
Sample Output
23 is a prime number.

BSIT07-01 22-11-2011 07:45 PM

Re: Program to enter an integer and print if it is prime or composite
 
Don't you think, this program will check even and odd numbers instead of composite and prime?

---------- Post added at 07:45 PM ---------- Previous post was at 07:42 PM ----------

ok, got it ... this will work fine :p


All times are GMT +5. The time now is 01:35 AM.

Powered by vBulletin® Version 3.8.2
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.