C Program to Find Sum of Odd Digits in a Given Number

Introduction

This comprehensive article will guide you to write a c program to find sum of odd digits in a given number.

Also Read: Print Contents of File in Reverse Order in C

In the world of computer programming, the C language holds a prominent position due to its versatility and efficiency.

It allows developers to create powerful and effective programs for various purposes. One common task that programmers often encounter is finding the sum of odd digits in a given number.

Also Read: Search a Character in a String Using a Pointer in C

Whether you are a beginner or an experienced programmer, this article will help you gain a deeper understanding of the topic and enhance your programming skills.

Understanding the Problem

Before diving into the solution, let’s first understand the problem statement. We are given a number, and our task is to find the sum of its odd digits.

Also Read: C Program To Read Two Files Simultaneously

For example, if the given number is 246813, the odd digits are 3 and 1. The sum of these odd digits would be 4.

To achieve this result, we need to devise an algorithm using the C programming language.

Also Read: Armstrong Number in C Programming

Algorithm for Finding the Sum of Odd Digits

To find the sum of odd digits in a given number, we can follow the following algorithm:

Also Read: C Program to Find the Inverse of 2×2 Matrix

  1. Initialize a variable num with the given number.
  2. Initialize a variable sum to store the sum of odd digits and set it to 0.
  3. Start a loop to iterate through each digit of the number.
  4. Extract the last digit of num using the modulo operator % and store it in a variable digit.
  5. Check if digit is odd by checking the remainder of digit divided by 2. If the remainder is not zero, it is an odd digit.
  6. If digit is odd, add it to the sum.
  7. Update num by removing the last digit using integer division by 10 (num = num / 10).
  8. Repeat steps 4-7 until num becomes 0.
  9. The final value of sum will be the sum of odd digits in the given number.

Now that we have a clear algorithm in mind, let’s proceed to implement it in the C programming language.

Also Read: C Program to Copy the Contents of One File into Another File

Implementing the C Program

#include <stdio.h>

int main() {
    int num, digit, sum = 0;
  
    printf("Enter a number: ");
    scanf("%d", &num);

    while (num != 0) {
        digit = num % 10;
      
        if (digit % 2 != 0) {
            sum += digit;
        }

        num /= 10;
    }

    printf("The sum of odd digits is: %d\n", sum);

    return 0;
}

Also Read: Getchar and Putchar Function in C with Example

Let’s break down the program and explain each part:

  • We start by including the necessary header file stdio.h, which provides input and output functionalities.
  • Inside the main() function, we declare three variables: num, digit, and sum. num stores the given number, digit is used to hold each digit during the iteration, and sum stores the sum of odd digits.
  • We prompt the user to enter a number using the printf() function and read the input using the scanf() function, storing it in the variable num.
  • The while loop is used to iterate through each digit of the number until num becomes 0.
  • Inside the loop, we extract the last digit of num using the modulo operator % and store it in the variable digit.
  • We check if digit is odd by using the condition digit % 2 != 0. If true, we add the digit to the sum.
  • We update num by dividing it by 10 (num /= 10) to remove the last digit.
  • Once the loop ends, we display the sum of odd digits using the printf() function.

Also Read: Program To Reverse a String in C using Pointer

FAQs (Frequently Asked Questions)

Q1: How does the modulo operator help in extracting the last digit of a number?

The modulo operator % returns the remainder when the left operand is divided by the right operand. In this case, when we use num % 10, it gives us the last digit of num. For example, if num is 246, num % 10 will give us 6.

Q2: What does the condition digit % 2 != 0 signify?

The condition digit % 2 != 0 checks if digit is not divisible by 2, which means it is an odd digit. If the condition is true, we add digit to the sum.

Q3: Can this program handle negative numbers?

Yes, the program can handle negative numbers as well. The modulo operator works with negative numbers, so the last digit of a negative number can also be extracted using num % 10.

Q4: What happens if I enter a non-integer value?

If you enter a non-integer value, the program will not be able to read it properly, leading to unexpected behavior. It’s important to ensure that the input is of integer type.

Q5: How can I optimize this program further?

The current program already provides an efficient solution for finding the sum of odd digits. However, you can explore optimizations by avoiding unnecessary iterations or using different data structures to solve the problem.

Q6: Can I use this program to find the sum of odd digits in a large number?

Yes, you can use this program to find the sum of odd digits in a large number. The program’s performance does not depend on the size of the number, as it iterates through each digit individually.

Also Read: C Program to Copy the Contents of One File into Another File

Conclusion

In this article, we explored C program to find sum of odd digits in a given number.

We discussed the problem statement, devised an algorithm, and implemented the solution step-by-step.

Also Read: 25 Tricky Questions on Pointers in C: Explained and Answered

The provided C program efficiently calculates the sum of odd digits and can handle various scenarios, including negative numbers.

Additionally, we addressed common questions and provided insights for further optimization.

By understanding and implementing this program, you have enhanced your programming skills and gained a valuable tool for solving similar problems.