TCS Coding Practice Question | Sum of Digits of a number Last Updated : 17 Mar, 2023 Comments Improve Suggest changes Like Article Like Report Given a number, the task is to find the Sum of Digits of this number using Command Line Arguments. Examples: Input: num = 687 Output: 21 Input: num = 12 Output: 3 Approach: Since the number is entered as Command line Argument, there is no need for a dedicated input lineExtract the input number from the command line argumentThis extracted number will be in string type.Convert this number into integer type and store it in a variable, say numDeclare a variable to store the sum and set it to 0Repeat the next two steps till the number is not 0Get the rightmost digit of the number with help of remainder '%' operator by dividing it with 10 and add it to sum.Divide the number by 10 with help of '/' operatorPrint or return the sum Program: C // C program to find // the sum of digits of a number // using command line arguments #include <stdio.h> #include <stdlib.h> /* atoi */ // Function to Find the sum of digits int findSumOfDigits(int num) { // Variable to store the // the sum of digits int sum = 0; // Traverse through the number digit by digit while (num > 0) { // Add the last digit of num // to the sum sum = sum + (num % 10); // Remove the last digit from the num num = num / 10; } // Return the sum return sum; } // Driver code int main(int argc, char* argv[]) { int num; // Check if the length of args array is 1 if (argc == 1) printf("No command line arguments found.\n"); else { // Get the command line argument and // Convert it from string type to integer type // using function "atoi( argument)" num = atoi(argv[1]); // Find the sum of digits and print it printf("%d\n", findSumOfDigits(num)); } return 0; } Java // Java program to find // the sum of digits of a number // using command line arguments class GFG { // Function to Find the sum of digits public static int findSumOfDigits(int num) { // Variable to store the // the sum of digits int sum = 0; // Traverse through the number digit by digit while (num > 0) { // Add the last digit of num // to the sum sum = sum + (num % 10); // Remove the last digit from the num num = num / 10; } // Return the sum return sum; } // Driver code public static void main(String[] args) { // Check if length of args array is // greater than 0 if (args.length > 0) { // Get the command line argument and // Convert it from string type to integer type int n = Integer.parseInt(args[0]); // Find the sum of digits and print it System.out.println(findSumOfDigits(n)); } else System.out.println("No command line " + "arguments found."); } } Python # Python program to find the sum of digits of a number def findSumOfDigits(num): sum = 0 # Traverse through the number digit by digit while (num > 0): # Add the last digit of num to the sum sum = sum + (num % 10) # Remove the last digit from the num num = num // 10 return sum # take the input num=(input()) # convert into integer type n=int(num) # check the length of number if len(num)>0: print(findSumOfDigits(n)) else: print("No command line") C++ #include <iostream> #include <cstdlib> // for atoi function using namespace std; // Function to find the sum of digits int findSumOfDigits(int num) { int sum = 0; while (num > 0) { sum += num % 10; // add the last digit to the sum num /= 10; // remove the last digit from num } return sum; } // Main driver code int main(int argc, char* argv[]) { int num; // Check if at least one command line argument was provided if (argc == 1) { cout << "No command line arguments found." << endl; return 0; // exit program } // Convert the first command line argument to an integer num = atoi(argv[1]); // Calculate the sum of digits of the number and output the result cout << findSumOfDigits(num) << endl; return 0; // exit program } Output: In C: In Java: In Python Time complexity: O(logn) Auxiliary Space: O(1) Comment More infoAdvertise with us Next Article TCS Coding Practice Question | Sum of Digits of a number R RishabhPrabhu Follow Improve Article Tags : Interview Experiences TCS TCS-coding-questions Practice Tags : TCS Similar Reads Sum of Digits of a Number Given a number n, find the sum of its digits.Examples : Input: n = 687Output: 21Explanation: The sum of its digits are: 6 + 8 + 7 = 21Input: n = 12Output: 3Explanation: The sum of its digits are: 1 + 2 = 3Table of Content[Approach 1] Digit Extraction - O(log10n) Time and O(1) Space[Approach 2] Using 6 min read How to find Sum the Digits of a given Number in PHP ? We will explore how to find the sum of the digits of a given number in PHP. This task involves extracting each digit from the number and adding them together to get the final sum. Table of Content Using a loop to extract digits one by oneUsing mathematical operations to extract digitsUsing a loop to 2 min read n-th number whose sum of digits is ten Given an integer value n, find out the n-th positive integer whose sum is 10. Examples: Input: n = 2 Output: 28 The first number with sum of digits as 10 is 19. Second number is 28. Input: 15 Output: 154 Method 1 (Simple): We traverse through all numbers. For every number, we find the sum of digits. 8 min read Check if a number can be represented as the sum of numbers with at least one digit equal to K Given integers N and K, the task is to check if a number can be represented as the sum of numbers that have at least one digit equal to K. Example: Input: N = 68, K = 7Output: YESExplanation: 68 = (27 + 17 + 17 + 7). Each number has atleast one digit equal to 7. Input: N = 23, K = 3Output: YESExplan 6 min read Smallest number with sum of digits as N and divisible by 10^N Find the smallest number such that the sum of its digits is N and it is divisible by 10^N . Examples : Input : N = 5 Output : 500000 500000 is the smallest number divisible by 10^5 and sum of digits as 5. Input : N = 20 Output : 29900000000000000000000Recommended PracticeSmallest number with sum of 6 min read Like