| |
|
#1
| ||||
| ||||
| Program to enter an integer and print if it is prime or composite #include <iostream.h> #include <conio.h> #include <process.h> void main() { clrscr(); int num1,x; cout << "Enter an integer : " << endl; cin>>num1; for(x=2;x<num1;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.
__________________ ![]() |
|
#2
|
| 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 |
![]() |
| Tags |
| composite, enter, integer, prime, print, program |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
| |
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Program to enter an integer and print out its successor | bonfire | Object Oriented Programming | 0 | 01-03-2011 02:55 PM |
| Program to enter an integer and output the cube of that integer | bonfire | Object Oriented Programming | 0 | 01-03-2011 02:53 PM |
| Program to enter an integer and print if it is greater or less than 100 | bonfire | Object Oriented Programming | 0 | 01-03-2011 02:28 PM |
| Program to enter an integer and print its total value based on the formula 'x - 1/3!x^3 + 1/5!x^5 - 1/7!x^7 + 1/9!x^9' | bonfire | Object Oriented Programming | 0 | 28-02-2011 10:58 PM |