Who can explain this code ? Why it shows 1 [closed] - c++

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
How does this work?
int a=5<=5;
cout<<a; // output : 1
Who can explain why output is 1?

That's because
<= has higher priority than = so it is calculated first.
5 <= 5 returns a boolean, which is true.
It is converted into an integer, which is 1.
then 1 is assigned to a.
so a equals 1 now.

5<=5 is true which, when converted to an integer is 1.
You assign this value to a which then holds the value 1. Which you then output.
You may want to read https://en.cppreference.com/w/cpp/language/operator_precedence

Related

Is there a way to enter a number through another number? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I'm taking a C++ class in school, (No experience with programming) so I realize that this is a very dumb question and also horribly worded but here it is:
Is there any way to enter 6-21 (As in 6 through 21) into a boolean expression? I've tried it like this:
if (regs = 6-21)
But it assumes that I'm trying to subtract. What I'm trying to ask is, is there a way to enter a number through another number? Is it even possible?
Here is how you do it:
if (regs >= 6 && regs <= 21) {
// some code
}
>= means "greater than or equal to".
<= means "less than or equal to".
&& means "and", as in "if this AND this is true".

Regular expression for numeric range from -a to b where a is not equal to b [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
Let a and b be digits. The problem is easily solved if a=b, I can test the sign/no sign and then proceed to match the range of numbers from 0-a. I don't know how to solve this when the numbers are unequal.
How can I do it in python using a regular expression?
If you want for example between -7 and 4 :
(-[1-7]|[0-4])
Here is an example : https://regex101.com/r/8TnSVn/1

C++ input value process as math function [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I’m very new in C++ and I need your help. I want to use the input value as a mathematical function then print the result.
What i need is:
When user input: 2 +1 and hit Enter then output (cout>>)
should be your result is: 3 (the sum of 2 + 1)
The mathematical symbol could be + or * or / or -.
Could you please help me with some example?
Thanks in advance!
Search for "Reverse Polish notation" and implementation in C++

What does the modulus do? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I never really get it, but what does the modulus operator (%) do.
And when would you want to use it in a mathematical context? By the way, I
just want to know what it does, not how it works.
It calculates the remainder of a division operation.
5 / 2 == 2 // whole part of the division
5 % 2 == 1 // remainder
In other words you could reconstruct the original number by
2 * 2 + 1 == 5
| | ^ remainder
| ^ whole part
^ divisor

C++ prime number logic [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I'm creating a program that will output whether a number is prime or not. although, I'm trying to make it a little more complicated. I created a vector which I now want to store every prime number up to and including the number input by the user.
And then for the number input by the user (i.e. if the user types 13) will return true for the function hence it is a prime number. Here are some examples:
**U
primechecker() : plist(2) {;}
bool operator()(int);
3 5 7
then 3 5 6 7 7
The reason your vector is starting with two zeros is because you are initializing it with two elements of the default constructor:
primechecker() : plist(2)
Check out the documentation of std::vector (you are using the 'fill constructor')