Doubts in C/ C++ language

I’m struggling with nested loop programs and loops in general.
How do I write a program to display all the primes from 1 to 100?

I’ll give you a hint. First setup a for loop counter from 1 to 100(incremental).
Inside that loop, use the % binary operator(it checks if remainder is zero or not)

So basically u need 2 loops(one is nested).
First loop gives you a number from 1-100. Inner loop checks if it is prime or not. If it satisfies condition of prime, print it and break the inner loop

1 Like

Yeah, I got it till there, I even did the break part if i==j.

1 Like


i, j are just variable names(for the counter)

1 Like

Sorry, I used the break for something else… Let me try again with this approach.

1 Like

Take a variable x. Set its value to 2. And another variable say t, set it to 1.

For each iteration, check if it’s divisible by all numbers less than or equal to x/2 ( actually you can do √x according to mathematical theorem). You need to do this checking in a loop inside the bigger loop ( nested loop i.e I hope you get it).

Now I am talking about the innermost loop. If a number divides x, set t=0 and get out of the loop ( break, i mean).

Now the overall structure,

Loop 1 ( each iteration increases i by 1)
Set t=1
Loop 2 ( the checking part, run a loop from 2 to x/2 or √x)
If, it’s divisible, t=0 and break
If t=1, number is prime
Else it isn’t.

The indentations imply the braces, I hope you get it.

Lol, why aren’t the indentations showing? @DJT

1 Like

Tum kya kya karte ho ek din me :face_with_monocle:

1 Like

I got the primes now in order (some progress), but it is printing each number 5 times, 6 times etc.

1 Like

I did exactly this.


Check placing of your ‘print’ command in the nested loops(check if it is before or after break)

It is after the break, should I send the code?

1 Like

Check the braces properly once more. Make sure you opened and closed in the right places.

Also, always make sure you indent your code. It becomes easier to find mistakes.

1 Like

It shouldn’t be after

That’s just fancy. It doesn’t matter. Messier codes exist too. It’s just like leaving margin space and top space of a notebook, or leaving lines after a paragraph.

It makes it easier to identify errors in syntax, but it won’t affect interactions much

1 Like

I know this is wrong, I can’t seem to correct it.

for(i=1; i<=100;i++)
for(j=2; j<=i/2;j++)
if(i%j ==0)
printf("%d ", i);

No I meant something else. When I was writing my answer I indented some lines. But they weren’t showing up in the post.

Whatever, but indentations really make a difference. I was one of those people who didn’t use to indent and later got the hang of it.


Yeah, indentations don’t show up when I copy and paste code in chat, or here.

I don’t know C so I don’t know what exactly this is supposed to do, but I feel this is wrong.

After break and before else it needs to come @Cooljet123

@DJT you sure the print is supposed to come there?

loop ke andar print hua ? i think

1 Like

Print should be outside the inner loop as you want to print the identified prime number just once in a desired iteration