Check whether a number is armstrong by calculation order

Duration: 7 min

This video lesson is available to enrolled students.

Enroll to watch — LTI MINDTREE Superset Course

AI summary & chapters

AI Summary

An AI-generated summary of this video lecture.

This video is a programming tutorial that teaches how to write a C program to check if a number is an Armstrong number. The instructor begins by introducing the concept of an Armstrong number, which is a number that is equal to the sum of its own digits each raised to the power of the number of digits. The lesson is structured around a step-by-step implementation. First, a function named `order` is defined to calculate the number of digits in a given number using a loop that repeatedly divides the number by 10. This function is then used in the main `isArmstrong` function. The `isArmstrong` function extracts each digit of the number, raises it to the power of the total number of digits (using the `pow` function), and accumulates the sum. Finally, the program compares this sum to the original number to determine if it is an Armstrong number. The tutorial includes a worked example using the number 153, which is confirmed to be an Armstrong number because 1^3 + 5^3 + 3^3 = 153. The video concludes with a demonstration of the complete code running in an online C compiler, showing the program's output.

Chapters

  1. 0:00 2:00 00:00-02:00

    The video starts with the 'KNOX MEEHEGATF' logo, which is a stylized version of 'KNOWLEDGE GATE'. It then transitions to a screen showing the 'KNOWLEDGE GATE' brand and its social media handles. The main content begins with a C code snippet on a digital blackboard. The instructor introduces a function `int order(int x)` designed to calculate the number of digits in a number. The function initializes a counter `n` to 0 and uses a `while` loop that continues as long as `x` is greater than 0. Inside the loop, the counter `n` is incremented, and `x` is divided by 10. The loop terminates when `x` becomes 0, and the function returns the final count `n`. The instructor explains this logic, and on-screen text shows the code and a handwritten example `x=153` to illustrate the process.

  2. 2:00 5:00 02:00-05:00

    The instructor continues to explain the `order` function, using the example `x=153`. He demonstrates the loop iteration by hand, showing that `n` starts at 0 and is incremented three times (n=1, n=2, n=3) as `x` is divided by 10 (153 -> 15 -> 1 -> 0). He confirms that the function correctly returns 3, which is the number of digits. The video then transitions to the main function, `int isArmstrong(int x)`. The instructor explains that this function will use the `order` function to get the number of digits `n`. He initializes a temporary variable `temp` to the input number `x` and a `sum` variable to 0. A `while` loop is used to process each digit of `temp`. Inside the loop, the last digit is extracted using `temp % 10`, and this digit is raised to the power of `n` using the `pow` function and added to the `sum`. The `temp` variable is then updated by integer division by 10. The instructor writes the formula `153 = 1^3 + 5^3 + 3^3` to illustrate the Armstrong condition.

  3. 5:00 7:19 05:00-07:19

    The instructor completes the `isArmstrong` function by adding the final condition. After the loop, the function checks if the calculated `sum` is equal to the original number `x`. If true, it returns 1 (true); otherwise, it returns 0 (false). The video then shows the complete `main` function, which takes user input for a number `x`, calls `isArmstrong(x)`, and prints "True" or "False" based on the result. The final segment demonstrates the code running in the GDB Online compiler. The instructor runs the program with the input 153, and the output is "True", confirming that 153 is an Armstrong number. The video concludes with the program finishing with exit code 0.

The video provides a comprehensive, step-by-step guide to solving the Armstrong number problem in C. It begins with a foundational function to calculate the number of digits, which is a crucial step. This function is then integrated into the main logic, which iteratively processes each digit, applies the power operation, and accumulates the sum. The lesson effectively uses a concrete example (153) to illustrate the mathematical concept and the code's logic. The progression from function definition to the final main program, culminating in a live code execution, creates a clear and practical learning experience for students.

Loading lesson…