Create a program that will display all the composite numbers from 0 to 1000 and has a maximum column of 5 . A composite number is a positive integer that has at least one 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.

SAMPLE OUTPUT:

4 6 8 9 10

12 14 15 16 18

20 21 22 24 25

26 27 28 30 32

....and so on.

SAMPLE OUTPUT:

4 6 8 9 10

12 14 15 16 18

20 21 22 24 25

26 27 28 30 32

....and so on.

What have you written so far? Show us your code, and we'll help. But we're not going to do your homework for you.

How can i make it 5 columns like my sample output ?

#include <iostream>

#include <conio.h>

#include<iomanip>

using namespace std;

int main()

{

int i,n,factor;

for(n=2;n<=1000;n++)

{

for(i=1;i<n;i++)

{

if(n%i==0)

{

factor=i;/*finds the largest proper divisor*/

}

}

if(factor>1)

{

cout<<setw(4)<<n;

}

}

getch();

return 0;

}

#include <iostream>

#include <conio.h>

#include<iomanip>

using namespace std;

int main()

{

int i,n,factor;

for(n=2;n<=1000;n++)

{

for(i=1;i<n;i++)

{

if(n%i==0)

{

factor=i;/*finds the largest proper divisor*/

}

}

if(factor>1)

{

cout<<setw(4)<<n;

}

}

getch();

return 0;

}

Last edited on

int count=0;

for(int i=2;i<=1000;i++)

{

for(int j=2;j<=i/2;j++)

{

if(count==5)

{

cout<<"\n";

count=0;

}

if( i%j==0)

{

cout<<i<<" ";

count=count+1;

break;

}

}

}

for(int i=2;i<=1000;i++)

{

for(int j=2;j<=i/2;j++)

{

if(count==5)

{

cout<<"\n";

count=0;

}

if( i%j==0)

{

cout<<i<<" ";

count=count+1;

break;

}

}

}

Please DO NOT double post your questions! You have people already helping you for the same question in the beginner forum. We could be helping others instead of wasting time on a question that is already answered elsewhere.

Topic archived. No new replies allowed.