TCS Coding Practice Question | Sum of Digits of a number

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 line
  • Extract the input number from the command line argument
  • This extracted number will be in String type.
  • Convert this number into integer type and store it in a variable, say num
  • Declare a variable to store the sum and set it to 0
  • Repeat the next two steps till the number is not 0
  • Get 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 ‘/’ operator
  • Print or return the sum

Program:

C

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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.");
    }
}

chevron_right



Output:

  • In C:

  • In Java:



My Personal Notes arrow_drop_up

Technical Content Engineer

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.