anybody tell me please what is wrong in this code it will give me only blank console screen?

#include <iostream>

using namespace std;

int main()
{
    int x=1;
    int y=2;
    int z;
    while(x<=50)
    {
        while(y<x)
        {
          z=x%y;
          if(z==0)
          {
              cout<<x<<"is a prime number"<<endl;
          }
          else
          {
              y++;
          }
        }

x++;
    }
    return 0;
}
{

``

Recommended Answers

All 2 Replies

Well, to start off, your second while loop will not work because you put while(y < x)

On lines 7 and 8, you made x=1; and y=2;
y is greater than x, so the second while loop will never run.

I'm sorry, I quickly looked over your code. After the third iteration, the second while loop will run.

Here is a good way to test your code:

Write down on a piece of paper what the values will be after each iteration.

Also, line 29 has an open { with no closing curly brace. So that could cause your program to not run.

while(x <= 50)
{
    while(y < x)
    {
        z= x%y;
        if(z == 0)
        {
            cout << x << " is a prime number" << endl; 
        }

        else
        {
            y++;
        }
    }

    x++;
}

so:
x =1
y=2

1st: Iteration
x=2
y=2( 2 < 1 so, the 2nd while loop will not run yet since x does not become 2 until the end

2nd: Iteration
x=3
y=2(The next iteration, the loop will begin to run)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.