 Hello friends. I'm Sanjay Gupta. I welcome you on Sanjay Gupta Tech School. In this video, I'm going to explain how you can check whether the input number is Armstrong or not. Before starting, if you go to detail of this video, you will find various links of a playlist. Those are related to various programs. You can watch them if you want to learn more programming. So now starting, so first I'm going to explain what Armstrong is. Then I will be implementing a program that is related to C. So that you can learn how we can check whether the input number is Armstrong or not. So if we take a number as input, let's say 1 by 3 and we perform this operation. If we divide individual digits, then we take cube of each individual digit and we add all those values. And if the addition comes equal to the original number, then that number is known as Armstrong number. So 1 cube is 1 plus 5 cube is 125 plus 3 cube is 27. Now add all these values. So 126, then 7, 1, 3, 3 and we add 12 and it will be 1 by 3. So if you add all these three numbers, 1, 125 and 27, so output will be 150. So it means 1 by 3 is Armstrong number. Now we have two methods to check whether the number is Armstrong or not. So if number is 1, 2, 3, 4 and if we are taking this, so here notice it carefully. Here number is having four digits, but still we are having power as 3. So this is first method of calculating Armstrong. It means we are applying power as 3 irrespective of the number of digits those are available in the number. Now next example will be if number is 1, 2, 3, 4. So we need to apply this. If number is having four digits, so power will be 4. If number is let's say 12, so power will be 2 and so on. And after calculating these, if number is equal to horizontal number, then it will become Armstrong number. So first I am going to explain this logic. Like every time power will be 3, we need to calculate the cube. And after implementation of this, I will convert that program into this so that first we will identify number of digits in the given number and then we will take that count as a power of individual digit. So first I am going to implement this. So starting with main, I am declaring some variables. So first is n, then I am taking variable a, then I am taking variable s and variable t as a temporary. Now printers will display a message, enter number, then scanners will read that number into variable n. Now after reading that number, now I am going to apply a while loop where I will be dividing these individual values and then I am calculating the cube. So yn greater than 0, then inside this loop, I am applying this. a equals to n modulus 10. So I am taking modulus of this n by 10. So reminder will be available into a, so a will be having a particular digit. So now I have to calculate its cube. So I am writing this. So I am adding cube of a with s and the final result of this expression will again assign it to s. And then n equals to n by 10. Now after completion of this loop, I will apply if condition. So my final addition will be available into s. So I am going to compare this s with original value so that I can identify whether the solution is having number as prime, number as Armstrong or not. So first I am going to explain this logic and then I will apply a condition. So for example, number is 153. Number is 153. So starting from here, so if we check this condition, it is true. We are coming here. So n modulus 10, so whenever we divide number by 10, so reminder will be last digit. So a will be having p as result here. Then initially s is the loop. So multiply a three times, so three into three into three. It is 27 and that will be added into s and stored into s. So new value of s will become 27. Now divide n by 10. So n is right now 150. So if we divide the number by 10, so its last digit will be removed automatically. And remaining will be stored inside the variable again if we write n equals to n by 10. So here you can see n is 150 if we divide it by 10. So result will be 15.3. So we are signing 15 to n because data is in here. Now repeat this again. So here you can see right now n is 15. So condition is true. 15 modulus 10. So a will be having 5 as a reminder. Now 5 into 5 into 5. So if we multiply 5 three times, so it will be 125. And we have to add 125 with s. So s is 27. So if we add 27 and 125, so it will be 152. Then again divide n by 10. So n is 15. So it will be having n. So 15 by 10 is 1.5. So a new value of n will be 1. Now again check this condition. So 1 is greater than 0. Take the modulus. So modulus will be 1. Then 1 into 1 into 1 will be 1. 1 will be added to s. So s is 152. Now it will be 253. Then again divide n by 10. So right now n is 1. So if we divide 1 by 10, so the result will be 0. So now n is 0. Now check this condition. It is false. So it means your loop is complicated. And the sum s variable is containing this value 152. So here after completion of this loop, you can apply a condition. Where you can write nw equals to n. Sorry, nw equals to s. So s is having sum of cubes. And n is the variable which received the actual number from the user. So we are going to compare them. But before going forward, after this completion, after completion of this loop, if you check the value of n, what it is? 0. So now if you compare n and s, so every time n will become 0 after completion of this loop. So this condition will become false always. Right? So now to overcome with this problem, I have declared one variable s t. So before starting this loop, before starting this loop, here you need to write this. Here you need to write this t equals to n. So what we are doing? We are assigning value of n temporarily into t variable. And that is before starting of this value. So after completion of this y, n will become 0. But original value of n is copied into t. So t will be having the original value that is provided by the user. So in place of n, we can write t here. So whenever you want to maintain a copy of a particular variable, so assign that value into another variable, so that if actual variable is not having actual value after a certain process. So the value will be available in the other variable who is having the copy. So here we are using the copy of n in comparison. So copy of n is available in t. S is having some of cubes. So if both are equal, then we can print. Number is Armstrong. Else we can print. Number is not Armstrong. And then you will use the main function. So this way I took example of 153. If I take example of 1234, then also here you can see its cube will be calibrated because A is multiplied three times whatever number of digits are available in the number. So it will be always calculating cubes of particular digits. So I hope you understood this method like if we have any number, so we will be separating its individual digits and we are calculating its cubes and we are adding together all the results and then comparing whether it is Armstrong or not. Now I am going to convert this program for this logic. So here first we need to count how many digits are available in the number and then that digit will be used as a part of a particular digit. So before starting this loop, I need to add one more loop here. So I am going to use that. So here you can see this loop is written here. So now before starting this loop, before starting this loop, we need to write one more loop so that we can count number of digits. Those are available in the number. So here I am implementing by n greater than zero. So I am using one more variable as C. So here it will calculate C plus plus so that we can count the number then n equals to n by 10. So now see the order. First print f, then scan f. Then you need to write this. The value of n will be available temporarily into T. So this time both are having same value. After this, you need to write this. So this loop will calculate how many digits are available in the number and again after completion of this loop n will become zero because we are dividing n by 10 and assigning it into n. So here you can write this n equals to T. So T is having copy of n. So again it is assigning back to n. So again n is having the original value and then you can start this loop and here instead of this, you need to write a predefined function that is P o w a comma C. So P o w is defined in math dot h. So above main you need to include a header file that is math dot h. So you can use its predefined function P o w. Then a is the base and C will be the bar. So this loop will identify number of digits and after calculating number of digits that value will be available into C and that C will be treated as R here and rest of the process will be C. So this way this logic will be implemented. So I hope you understood how we can check whether input number is Armstrong or not. So I explain you two different methods. You can implement anyone that is to give it to your department. So I hope you understood whatever I explained in this video. If you want to watch more programming related videos, you can open my channel. Go to playlist and there you will see more than 1000 videos related to computer programming. So do watch them. Thank you for watching this video.