Hi there i am wondering if there is easy way of writing the following problem in C:-
For everytime constant A increases, constant B should decrease by 10 until A is equals to 120 !!

Thanks in advance guys
I dont really want to clutter my project with tons of IF statements

Recommended Answers

All 6 Replies

You cannot increase or decrease a constant. I would call it a variable.
Tons of If? Start with a while loop.

yh sorry my bad !! a variable
i tried the following but the code apparntaly stucks in this loop for infinite time

while (A++){
B += 10;
}

Think what would be the exit condition of the loop according to task. How could you express it as continueing condition of while?

You didn't check to see when A equals 120. The implicit check is against 0, so your loop actually looks like this:

while (A++ != 0) {
    B += 10;
}

There are a number of things that can go wrong here, which you can limit a bit by changing the test to this:

while (A++ < 120) {
    B += 10;
}

However, it would also be wise to limit the lower end too, just to ensure that you're not starting with A at something like INT_MIN. If that happens then B will easily overflow:

if (A < 0) {
    /* Assuming negatives are an error, for the sake of the example */
}
else {
    while (A++ < 120) {
        B += 10;
    }
}

Thanks for replying so quick guys but still having the same problem
jus to be clear A is an input So am checking against an input
so When A increase Then B should decrease

Post all of your code. If it's too long, shrink the code down into something smaller, but still complete and compilable.

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.