why do must use assignments (==) in this code? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
- 2

why do must use assignments (==) in this code?

every one can help me? #include <iostream> using namespace std; int main() { int n; cin >> n; int a=0; while(n>a){ cout<<n<<endl; if(n%5==0){ cout<<"Beep"<<endl; } n--; } return 0; }

5th Dec 2021, 5:05 PM
Yasin
Yasin - avatar
7 Answers
+ 5
== is actually an equality comparison operator which returns true when both LHS and RHS is equal. In this perticular program, it is being used to check if the result of ( n%5 ) ,is equals to 0 or not ( indirectly checking if the number *n* is divisible by 5 or not ) Is it necessary ? of course NO ( I leave the alternatives for you to explore ) ---- edit : looks like you are confusing it with assignment operator "=" is an assignment operator which assigns the value of RHS to LHS
5th Dec 2021, 5:12 PM
Arsenic
Arsenic - avatar
+ 5
Yasin no In programming equal to ( assignment operator ) is used to store some data or assign the data For example : String me = ig It means ' me ' is variable and 'ig ' is a literal means 'ig' is store in 'me' variable Whereas == is an equality comparison operator which returns true when both LHS and RHS is equal. For example : 2 == 2 True because both values are equal 2 ==3 False because both values are not equal
5th Dec 2021, 5:15 PM
Pariket Thakur
Pariket Thakur - avatar
+ 2
To check n%5 is equal to 0 ,if yes it will return true else return false
5th Dec 2021, 5:08 PM
Pariket Thakur
Pariket Thakur - avatar
+ 2
Hello Yasin == means equal % modulus operator n % 5 returns the remainder of n/5 It returns 0 if n is divisible by 5 You enter a number n, let's say 10: It runs from 10 to 0 and 5 and 10 are divible by 5 so it print's two times "Beep".
5th Dec 2021, 5:13 PM
Denise Roßberg
Denise Roßberg - avatar
+ 1
but we can also use = to equal 0! can't we?
5th Dec 2021, 5:10 PM
Yasin
Yasin - avatar
0
thanks for answring
5th Dec 2021, 5:26 PM
Yasin
Yasin - avatar
0
i really got it🙏
5th Dec 2021, 5:27 PM
Yasin
Yasin - avatar