C++ FOR BEGINNERS (2025) - What is while loop, How to count digits of a number PROGRAMMING TUTORIAL

preview_player
Показать описание
Iteration (looping) is a process where a set of instructions are repeated for a specified number of times or until a certain condition is met.
In a while loop, the condition is evaluated first and if it results as true then the statements inside while loop are executed, this happens repeatedly until the condition results as false. When the condition results as false, the control comes out of the loop and goes to the next statement in the program after the while loop.
In this video, I'm explaining how is while loop used on an example of counting digits of a number

📚 Learn how to solve problems and build projects with these Free E-Books ⬇️

Experience the power of practical learning, gain career-ready skills, and start building real applications!
This is a step-by-step course designed to take you from beginner to expert in no time!
💰 Here is a coupon to save 10% on your first payment (CODEBEAUTY_YT10).
Use it quickly, because it will be available for a limited time.

Follow me on other platforms:

*******CODE IS IN THE COMMENTS*******
Рекомендации по теме
Комментарии
Автор

📚 Learn how to solve problems and build projects with these Free E-Books ⬇️
Experience the power of practical learning, gain career-ready skills, and start building real applications!
This is a step-by-step course designed to take you from beginner to expert in no time!
💰 Here is a coupon to save 10% on your first payment (CODEBEAUTY_YT10).
Use it quickly, because it will be available for a limited time.

#include <iostream>
using namespace std;

void main()
{
//Count digits of a number

int number;
cout << "Number: ";
cin >> number;

if (number == 0)
cout << "You have entered 0.\n";
else {
if (number < 0)
number = -1 * number;

int counter = 0;
while (number>0)//0
{
//number = number / 10;
number /= 10;
counter++;//3
}

cout << "Number contains " << counter << " digits\n";
}

system("pause>0");
}

CodeBeauty
Автор

I appreciate you making all of these. I've been learning C# for the last 6 or so months but recently discovered all of my comp sci classes will be using C++ exclusively. You making these videos will have a huge impact on getting up and running for my classes. As I said before, I really appreciate it. Please keep pumping these out!

austinjohnathan
Автор

C++ also has abs() which will return the absolute value of a number. You can do number = abs(number) and get the same result.

bubblesbaby
Автор

For those that want to have greater than 10 Digits,

change your data type from int number to long long number.
I was able to get 19 digits from this data type.

virx
Автор

I wrote in order to include minus numbers too like that:
while (number > 0 || number < 0)
:) It works:)
Thank you for the lessons :) It helps me a lot :) I am still learning:) And I like your videos so much :D

maysonsplay
Автор

Yeah we need a video on debugging... and breakpoints

IAmIshanSaxena
Автор

Before i even start watching the video i give you a like cause i know the video is going to be amazing👏

saudalkhazriji
Автор

i have exam soon, i am using your tutorials thanks for your videos this is so helpfull for me, Good luck<3

Lafsha
Автор

A little different variation

int num;
int digit = 1;

cout << "Enter a number: ";
cin >> num;

if(num < 0) {

num = pow(num, 2);
num = sqrt(num);

}

while(num / 10 >= 1){
num/=10;
digit++;
}

cout << "Your number have " << digit << " digits";



Working properly!

noperson-ys
Автор

your explanation made understanding easy, thanks.

subratadas
Автор

THANK YOU SO MUCH I NEEDED THIS SO BADLY

hajarelsayed
Автор

Pozdrav, obzirom da nisam bas za C++ svejedno se da nauciti a ucim vec druge programske jezike pa cu dati sansu da iskusam ovaj kanal. Dobila si novi sub

benjaminavdic
Автор

Idk why but your voice is so therapeutic XD Thanks for teaching this, keep up the videos!!

domus_k
Автор

Yes make a video on debugging... and breakpoints, you're doing well.

rianvankessel
Автор

a other way of solution is:
int number=0;
cout << "\nEnter a digit: "; cin >> number;
std::string s = std::to_string(number);
cout << "the length of the digit is: " << s.length();

thomaspfaff
Автор

I really just changed
While(number>0)
To
While(number!=0)
And worked properly

KanekiKen-glqo
Автор

Thank you so much, you help me a lot .💙💙💙

dandon.
Автор

16:27 you should write long long instead of int variable

Lafsha
Автор

Hey I find your programming vedios the most easiest ones, thanks and I really need a mentor like you please be my guardian. I what to learn C++ programming lang.

shamsyumar
Автор

Dear Saldina, there is also another way to count digits beside 0 ->
while(number !=0) {

number /=10;
counter++;
}

AlvinKazaz