Find sum of digits in a given number

Duration: 4 min

This video lesson is available to enrolled students.

Return to /learn/ACCENTURE-PREPARATION/coding-assessment-questions/coding-questions/coding-question/asset-find-sum-of-digits-in-a-given-number after enrolling

Inside: a video lesson and guided study material.

Module outline

  1. Behavioral Assessment (Psychometric): Behavioral Assessment (Behavioral Competency Profiling) (Psychometric Test)
  2. Cognitive Assessment (Game-Based): Gamified / Game Based Aptitude Selection Decision Table (Decision Making) Puzzle Solving (Puzzles) Non Verbal Reasoning (Spatial Aptitude) (Spatial Reasoning) (Visual Reasoning) Analytical Reasoning (Counting Figures Reasoning) Cubes & Dices Coding Decoding Ranking, Order and Sequence Series (Number and Letter Series) (Numerical Relations and Reasoning)
  3. Common Applications & MS Office (Technical Assessment): MS Office by Yash Sir
  4. Pseudo Code (Technical Assessment): Pseudo Codes
  5. Networking, Security & Cloud (Technical Assessment): Introduction to CN Data Communication DLL: Access Control DLL: Flow Control DLL: Error Control DLL: Framing Data Link Layer - Ethernet Net Layer: IPv4 & Proto Net Layer: IP Addressing Net Layer:Routing Protocol Transport Layer Services TL: Congestion & UDP Application Layer Hardware basics Network Security Cloud Computing and IoT Cyber and Info Security Emerging Tech Trends
  6. Python (Technical Assessment): Python for Placements by Yash Sir
  7. Java (Technical Assessment): Java
  8. OOPs (Technical Assessment): Object Oriented Programming (OOPS)
  9. Data Structure (Coding Round): Introduction to DS Array Stack Queue Linked List Tree Graphs Hashing
  10. Algorithms (Coding Round): Algorithm Analysis Sorting Algorithms Greedy Algorithms Dynamic Programming Minimum Spanning Trees Shortest Path Algos Time Complexity Analysis Advanced Algorithms
  11. Coding Assessment Questions: C Programming by Yash Sir Coding Questions by Yash Sir Programming in C++
  12. SQL (Coding Round): SQL
  13. JavaScript Frontend (Coding Round): JavaScript
  14. Communication Test (Basic Verbal Ability): Vocabulary Sentence Construction Noun Pronoun Verb Tenses Subject Verb Agreement (Verb Noun Agreement) Articles Preposition Conjunction Adverb Modals Sentence Correction (Error Correction) Sentence Completion (Fill in the blanks) Comprehension / Reading Comprehension / Unseen Passages (Critical Reasoning) (Paragraph Questions) Communication Assessment
  15. HR Interview: MR & HR Interview Preparation
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…