Find sum of digits in a given number

Duration: 4 min

This video lesson is available to enrolled students.

Enroll to watch — ACCENTURE Superset

AI summary & chapters

AI Summary

An AI-generated summary of this video lecture.

This educational video, presented by a lecturer from Knowledge Gate, teaches how to write a C program to find the sum of the digits of a given positive integer. The lesson begins with an introduction to the problem, Q12, which requires a program to take an integer as input and output the sum of its digits. The instructor then presents a C code solution using a while loop. The core logic involves repeatedly extracting the last digit of the number using the modulo operator (n % 10) and adding it to a running sum, then removing that digit by integer division (n / 10). The process continues until the number becomes zero. The video includes a step-by-step walkthrough of the algorithm with the example number 1234, showing how the sum is calculated as 1+2+3+4=10. The final segment demonstrates the code running in an online C compiler, confirming the correct output.

Chapters

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

    The video opens with the 'KNOX MEEHEGATF' logo, which is a stylized version of 'KNOWLEDGE GATE'. It then transitions to a slide showing the 'KNOWLEDGE GATE' brand and its social media handles. The main content begins with a chalkboard-style presentation of a C program. The instructor, Yash Jain Sir, introduces the problem: 'Q12: Program to find sum of digits.' The problem statement specifies that the input is any positive integer and the output is the sum of its digits. Two public test cases are shown: for input 1234, the output is 10 (1+2+3+4), and for input 129487, the output is 31 (1+2+9+4+8+7). The instructor explains the problem and prepares to show the solution code.

  2. 2:00 4:17 02:00-04:17

    The instructor displays the C code solution on the screen. The code includes the necessary header files, defines the main function, and initializes variables 'n' and 'sum'. The core logic is a while loop that continues as long as 'n' is not zero. Inside the loop, the last digit of 'n' is obtained using 'n % 10' and added to 'sum', and 'n' is updated by integer division 'n / 10'. The final sum is printed. The instructor then walks through the algorithm with the example number 1234, writing out the steps on the board: 1234 % 10 = 4, sum = 0 + 4 = 4, n = 1234 / 10 = 123; 123 % 10 = 3, sum = 4 + 3 = 7, n = 123 / 10 = 12; 12 % 10 = 2, sum = 7 + 2 = 9, n = 12 / 10 = 1; 1 % 10 = 1, sum = 9 + 1 = 10, n = 1 / 10 = 0. The loop ends, and the final sum of 10 is printed. The video concludes with a demonstration of the code running in the GDB online compiler, showing the correct output for the input 1234.

The video provides a clear, step-by-step tutorial on solving a common programming problem. It effectively combines a problem statement, a complete code solution, a detailed algorithmic walkthrough, and a live code execution to reinforce the learning. The progression from problem definition to practical implementation ensures a comprehensive understanding of the digit-sum algorithm, which is a fundamental concept in programming.

Loading lesson…