Composite Numbers

06/10/2012 ·

The following programs will find if a number is composite or not and will print a series of composite numbers.
A composite number is a positive integer that has a positive divisor other than one or itself. In other words a composite number is any positive integer greater than one that is not a prime number.

So, if n > 0 is an integer and there are integers 1 < a, b < n such that n = a × b, then n is composite. By definition, every integer greater than one is either a prime number or a composite number. The number one is a unit – it is neither prime nor composite. For example, the integer 14 is a composite number because it can be factored as 2 × 7. Likewise, the integers 2 and 3 are not composite numbers because each of them can only be divided by one and itself.

The first 105 composite numbers are

4, 6, 8, 9, 10, 12, 14, 15, 16, 18, 20, 21, 22, 24, 25, 26, 27, 28, 30, 32, 33, 34, 35, 36, 38, 39, 40, 42, 44, 45, 46, 48, 49, 50, 51, 52, 54, 55, 56, 57, 58, 60, 62, 63, 64, 65, 66, 68, 69, 70, 72, 74, 75, 76, 77, 78, 80, 81, 82, 84, 85, 86, 87, 88, 90, 91, 92, 93, 94, 95, 96, 98, 99, 100, 102, 104, 105, 106, 108, 110, 111, 112, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 128, 129, 130, 132, 133, 134, 135, 136, 138, 140.



/*checks if it is a composite number or not i.e. has factor other than 1 and itself*/
#include<stdio.h> int main() { int i,n,factor; printf("Enter the number:"); scanf("%d",&n); for(i=1;i<n;i++) { if(n%i==0) { factor=i;/*finds the largest proper divisor*/ } } if(factor>1) { printf ("The number is a composite number!"); } else { printf ("This is not a composite number!"); } return 0; }


input:21
output:The number is a composite number!

input:23
output:This is not a composite number!

Now we will use the above logic to print a series of composite number



/*prints a series of composite numbers*/
#include<stdio.h> int main() { int i,n,factor; printf("Enter the last number of the sequence:"); scanf("%d",&j); for(n=2;n<=j;n++) { for(i=1;i<n;i++) { if(n%i==0) { factor=i;/*finds the largest proper divisor*/ } } if(factor>1) { printf ("%d",n); } } return 0; }



compiled on:gcc and linux
input:100
output:4, 6, 8, 9, 10, 12, 14, 15, 16, 18, 20, 21, 22, 24, 25, 26, 27, 28, 30, 32, 33, 34, 35, 36, 38, 39, 40, 42, 44, 45, 46, 48, 49, 50, 51, 52, 54, 55, 56, 57, 58, 60, 62, 63, 64, 65, 66, 68, 69, 70, 72, 74, 75, 76, 77, 78, 80, 81, 82, 84, 85, 86, 87, 88, 90, 91, 92, 93, 94, 95, 96, 98, 99

5 comments:

Anonymous said...
09/05/2015, 22:31  

Thank God for you!

Unknown said...
26/06/2017, 14:03  

There is a error in your program. j is undeclared.

hameed khan said...
28/07/2017, 20:30  

c++ coding..

#include
using namespace std;
int main()
{
int i,n,factor;
cout<<"Enter the number:";
cin>>n;
for(i=1;i1)
{
cout<<"The number is a composite number!";
}
else
{
cout<<"This is not a composite number!";
}
return 0;
}

Unknown said...
16/11/2019, 00:57  

provide code in java

Unknown said...
05/04/2020, 14:24  

#include
int main()
{
int i,n,factor;
printf("Enter the last number of the sequence:");
scanf("%d",&j);
for(n=2;n<=j;n++)
{
for(i=1;i1)
{
printf ("%d",n);
}
}
return 0;
}
Output of this program=5
4
But I want that when user enters 5 it prints
4,6,8,9,10
can anyone help me by providing the code

Post a Comment

Submit Request

If you need some code or programs just post a comment on this post. I will try to provide you the same at the earliest.

About this blog

Free sample code, example code , code snippet , tutorials in C C++. Find, download and reuse the code database available which vary from small programs to large ones as well. Feel free to request for code that is not in the list.

Followers