help!!!

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.

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.

up
here's my code.. how can i arrange it to become 5 columns only? and also there's something wrong with my code.. the output is counting by 4,6,8,10,12,14,16 and so on.. please help me to solve that problem

#include <iostream>
#include <conio.h>
#include<iomanip>
using namespace std;
int main()
{
int num=1;

while (num<=1000)
{if((num%2==0)&& (num!=2))
cout<<setw(4)<<num<<setw(6);
num++;
}
getch ();
}
Last edited on
What do you mean maximum column of 5?

Also, your formula isn't correct. All it will print is any even numbers that aren't 2.
how about this one sir>?? but i want to make it like in my sample output.. can you help me ?

#include <iostream>
#include <conio.h>
#include<iomanip>
using namespace std;
int p(int asal)
{
int y=1;
int i4;


for(i4=2;i4<asal;i4++)
{
if (asal%i4==0) y=0;
}
return y;
}

int main()
{
int i;
for(i=0;i<=1000;i++)
{
if((i==0)||(i==1)) cout<<setw(4)<<i;

if (p(i)==1) continue;
else cout<<setw(4)<<i;

}
getch();
return 0;
}
You could add something like this...

1
2
3
4
5
6
7
8
9
10
11
12
int numbersOutput = 0;

for( int i=0; i <= 1000; i++ )
{
   . . .
   else
   {
      cout << setw(4) << i;
      ++numbersOutput;
      cout << ( numbersOutput % 5 == 0 ? "\n" : " " );
   }
}
Last edited on
still the same ..this one sir i make it short..


#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
thanks for the idea.. i already get it :))
Topic archived. No new replies allowed.