+ 2
Plz explain the code | I couldn't understand
#include <iostream> using namespace std; int main() { int num = 1 ; while (num<=20) { if (num % 3 == 0) { cout << num << endl ; } num+=1; } return 0; }
13 Respuestas
+ 4
int num = 1 ; // assign num to 1
// loop runs until num<=20 is true
while (num<=20) {
// inside if num is divisable by 3 then output num value , add line end.
if (num % 3 == 0) {
cout << num << endl ;
}
// increment num value by 1
num+=1;
// repeat this until condition false
}
So that code outputs :
3,6,9,12,15,18 in between of 1 to 20 .
Hope it helps..
+ 4
First the num variable is assigned to 1
Then using a while loop untill the number is <=20 the will work
If the number is divide by 3 and the remainder is 0 display the number then increase the value of num by one
+ 3
Why num is assign to 1 and not 0
Yes. You can assign. then it output 0 also
Since 0%3 == 0
If 3 is divisible by 3 then why it is == 0 ?
3 is divisible by 3 then ,reminder is 0 so 3%3 must equal to 0
Look 1%3 == 0 false
2%3 == 0 false
3%3 == 0 is true
6%3 == 0 is true
== is equality comparision operator.
Returns true is equal. Otherwise returns false.
Increment num value meaning ? :
num+= 1 increment by 1 means , 1 is added to num so it gives you next number like 2,3,4,5,....
+ 3
If you assign the num = 0 then the your while loop run like this:-
while(0<=20)//because num value=0
// And the while loop will run until 20
If(num%3==0)
// % :- means modulas operator means it gives remainder value
// ==:- means compared the value like 2==2 , means 2 is equal to 2
//Now, (num%3==0) means when num any value divide by 3 like 3%3==0, means if you divide 3 by 3 then the remainder is 0 now 0==0 yes, then if condition is true
I hope it's clear to you
+ 2
Why num is assign to 1 and not 0
If 3 is divisible by 3 then why it is == 0
Increment num value meaning ?
+ 2
int num means num is the variable and it store int data type
And
num increment means it's increament the value like you write num+=1 it's meaning is num = num +1
You directly write like this:-
num = num + 1
and your statement is also correct
+ 1
Thank you so much sir 💗 You are now my permanent mentor
+ 1
Thanks you Shakshi Mam
+ 1
Sakshi not Shakshi 😅, btw welcome.
0
Thanks Tesfaye Sir
0
You added 3 to every number. Yay best explain
0
THANKS YOU HRITIK BHAIYA
0
But I know this