Whats wrong with this code....

#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
    int x,y;
    cin>>x>>y;
    while(1){
    if((0<x<=2000)&&(0<=y<=2000))
    {
                                    if((x<=y)&&(x%5==0))
                                    cout<<y;
                                    //else if((x/2)>y)
                                  //  cout<<y;
                                  //  else
                                  //  cout<<y;
                                    
                                    else
                                    {
                                        break;
                                        }
                                    }}
                                    getch();
                                    return 0;
                                    }

I want to print the single value of y.I do not find the error of the code.

Dani AI

Generated

Two separate problems are causing the behavior you see. The chained relational expression was already pointed out by and explained by — C/C++ do not support math-style chains like 0 < x <= 2000. The other problem not yet fully discussed is the infinite loop: because x and y are never changed inside while(1) the same branch keeps executing and you see repeated output (or a program that never ends).

If the goal is to print y exactly once for a single input pair, drop the endless loop and validate ranges with explicit comparisons. If you instead want to process many pairs, read inside a loop and break when appropriate. Use a small helper for readability and avoid nonstandard headers like conio.h.

Example: single evaluation run

#include <iostream>

bool in_range(int v) { return v >= 0 && v <= 2000; }

int main() {
    int x, y;
    if (!(std::cin >> x >> y)) return 0;
    if (in_range(x) && in_range(y) && x <= y && (x % 5) == 0)
        std::cout << y << '\n';
    return 0;
}

Example: process input pairs but print only one result then stop

#include <iostream>

bool in_range(int v) { return v >= 0 && v <= 2000; }

int main() {
    int x, y;
    while (std::cin >> x >> y) {
        if (!in_range(x) || !in_range(y)) break;
        if (x <= y && (x % 5) == 0) {
            std::cout << y << '\n';
            break;
        }
    }
    return 0;
}

Notes: avoid getch() (nonstandard); prefer standard I/O and return from main. Add simple debug prints if conditions still fail so you can see the actual values of x, y, and each boolean test.

Recommended Answers

All 4 Replies

if(0<x<=2000)

This does not do what you think it does. This does not check that x is greater than zero and less than, or equal to, 2000.

This does not do what you think it does. This does not check that x is greater than zero and less than, or equal to, 2000.

Then what it does?
How can I will check the condition?

if ( (0 < x) &&
     (x <= 2000) )

This says: if ( x is greater than zero AND x is less than or equal to 2000)

Then what it does?

0<x<=2000 will be parsed as (0 < x) <= 2000 . Or, depending on the value of x, either 0 <= 2000 or 1 <= 2000 since false and true evaluate to 0 and 1, respectively.

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.