+ 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; }

14th Nov 2022, 11:50 AM
Keshav Karn
Keshav Karn - avatar
14 Réponses
+ 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..
14th Nov 2022, 11:59 AM
Jayakrishna 🇮🇳
+ 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
15th Nov 2022, 11:33 AM
Abenezer Tesfaye
Abenezer Tesfaye - avatar
+ 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,....
14th Nov 2022, 12:14 PM
Jayakrishna 🇮🇳
+ 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
14th Nov 2022, 1:37 PM
Sakshi [Offline 🙃]
Sakshi [Offline 🙃] - avatar
+ 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 ?
14th Nov 2022, 12:02 PM
Keshav Karn
Keshav Karn - avatar
+ 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
14th Nov 2022, 1:28 PM
Sakshi [Offline 🙃]
Sakshi [Offline 🙃] - avatar
+ 1
Thank you so much sir 💗 You are now my permanent mentor
14th Nov 2022, 12:34 PM
Keshav Karn
Keshav Karn - avatar
+ 1
Thanks you Shakshi Mam
14th Nov 2022, 5:37 PM
Keshav Karn
Keshav Karn - avatar
+ 1
Sakshi not Shakshi 😅, btw welcome.
14th Nov 2022, 7:25 PM
Sakshi [Offline 🙃]
Sakshi [Offline 🙃] - avatar
0
Thanks Tesfaye Sir
15th Nov 2022, 11:44 AM
Keshav Karn
Keshav Karn - avatar
0
You added 3 to every number. Yay best explain
15th Nov 2022, 11:57 AM
MikroMick
MikroMick - avatar
0
THANKS YOU HRITIK BHAIYA
15th Nov 2022, 2:47 PM
Keshav Karn
Keshav Karn - avatar
0
But I know this
15th Nov 2022, 2:47 PM
Keshav Karn
Keshav Karn - avatar