I’ve to write a program that prints prime numbers from 1 to 100 (every 5 numbers on a line) using 2 functions: the first to test the number if it’s odd and the second to test it if it’s prime
I wrote that code but it didn’t work
it only prints the last prime number before 100
thanx ^_^
#include "stdafx.h"
bool is_odd(int x)
{
if (x%2==0)
return false;
else
return true;
}
bool is_prime(int x)
{
int j=0;
if (!(is_odd(x)))
return false;
if(is_odd(x))
{
for(int i=1;i<=x;i++)
if (x%i==0)
j=j+1;
if (j==2)
return true;
else
return false;
}
}
void main()
{
int x[100][100];
int i=1;
while (i<=100)
{
for(int j=1;j<=20;j++)
for(int k=1;k<=5;k++)
if (is_prime(i))
x[j][k]=i;
i++;
}
for(int j=1;j<=20;j++)
{
for(int k=1;k<=5;k++)
cout<<x[j][k]<<' ';
cout<<endl;
}
}
Such amazing complexity for a simple problem. You don’t need arrays to do this, not even multi-dimensional arrays.