Prime Numbers

I'm making an prime number code which should print all the prime numbers for example:
1
3
5
7
11
....
all prime numbers from 1 to 100 but there are alot of problems in my code can someone correct them please thanks!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include<stdio.h>
#include<conio.h>

int main()
{
clrscr();
int c,d;
for(int a=1;a<=100;a++)
{
  for(int b=1;b<a;b++)
  {
    c=a%b;   if(c==0) { d=1; }
    }
 }

 printf("%d",d);


 getch();

 }
Hi,
Try this :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include<stdio.h>
#include<conio.h>
int main()
{
clrscr();
printf("Prime numbers from 1 - 100 : \n");
for(int a = 1; a <= 100; a++)
{
   int num_factors = 0;
  for(int b = 1; b <= a; b++)
  {
      if(a%b == 0) num_factors++;
    }
  if(num_factors == 2)
  printf("\t%d\n", a);
 }
 getch();
}
Does that help you? :)
it helped bro :o
thank you soo much brother you are truely an awesome programmer :)
bro can you explain what is \t???
printf("\t%d\n", a);
> Bro can you explain what is \t???
Tab character.
Does that help? :)
how it works??
> How it works??

printf(" "); // prints out 1 space character

printf("\t"); // prints out 4 space characters
ohh thanks
Glad it helped :)
> Bro can you please explain how this code is working??

> A prime number can be divided, without a remainder, only by itself and by 1. For example, 17 can be divided only by 17 and 1

A prime number only has two factors. You count the number of factors for each number and check if the number of factors for the number you are progressing is exactly 2, then the number is a prime number.
(Does that help?) :)
Topic archived. No new replies allowed.