C Programs | IT Developer
IT Developer

C Programs



Share with a Friend

Loop Programs in C

Sum of digits of a number

Introduction

The sum of digits of a positive number is the sum of all its individual digits.

Example:

  • Input: 12345 → Output: 15 (1 + 2 + 3 + 4 + 5 = 15)
  • Input: 0 → Output: 0

We’ll use a for loop to extract digits and compute their sum.

 

C Program: Sum of digits of a number

NOTE: The Program is written using for, while and do..while loop.
Method 1 is for loop, Method 2 is while loop and Method 3 is do..while loop

Method 1: Using for loop

C

#include <stdio.h>

 

int main() {

    int num, original, digit, sum = 0;

 

    // Input

    printf("Enter a positive integer: ");

    scanf("%d", &num);

 

    // Validate input

    if (num < 0) {

        printf("Invalid input! Please enter a positive number.\n");

        return 1;

    }

 

    original = num; // store original number

 

    // Sum of digits using for loop

    for (; num != 0; num /= 10) {

        digit = num % 10;   // extract last digit

        sum += digit;       // add to sum

    }

 

    // Display result

    printf("Sum of digits of %d = %d\n", original, sum);

 

    return 0;

}

Output

 
OUTPUT 1 :
Enter a positive integer: 12345
Sum of digits of 12345 = 15

OUTPUT 2 :
Enter a positive integer: 0
Sum of digits of 0 = 0

OUTPUT 3 :
Enter a positive integer: -987
Invalid input! Please enter a positive number.

Explanation

  1. Read the positive number num and store it in original.
  2. Validate that the number is not negative.
  3. Use a for loop:
    • Extract the last digit with digit = num % 10.
    • Add it to sum.
    • Remove the last digit using num /= 10.
  4. Print the total sum of digits.

 

C Program: Sum of digits of a number

Method 2: Using while loop

Introduction

The sum of digits of a positive number is obtained by adding all its digits.

Example:

  • Input: 12345 → Output: 15 (1 + 2 + 3 + 4 + 5 = 15)
  • Input: 0 → Output: 0

We’ll use a while loop to extract digits and compute their sum.

 

C

#include <stdio.h>

 

int main() {

    int num, original, digit, sum = 0;

 

    // Input

    printf("Enter a positive integer: ");

    scanf("%d", &num);

 

    // Validate input

    if (num < 0) {

        printf("Invalid input! Please enter a positive number.\n");

        return 1;

    }

 

    original = num; // store original number

 

    // Sum of digits using while loop

    while (num != 0) {

        digit = num % 10;   // extract last digit

        sum += digit;       // add to sum

        num /= 10;          // remove last digit

    }

 

    // Display result

    printf("Sum of digits of %d = %d\n", original, sum);

 

    return 0;

}

Output

 
OUTPUT 1 :
Enter a positive integer: 12345
Sum of digits of 12345 = 15

OUTPUT 2 :
Enter a positive integer: 0
Sum of digits of 0 = 0

OUTPUT 3 :
Enter a positive integer: -987
Invalid input! Please enter a positive number.

Explanation

  1. Read the positive number num and store it in original.
  2. Validate that the input is not negative.
  3. Use a while loop:
    • Extract the last digit: digit = num % 10.
    • Add it to sum.
    • Remove the last digit: num /= 10.
  4. Print the total sum of digits.

 

C Program: Sum of digits of a number

Method 3: Using do..while loop

Introduction

The sum of digits of a positive number is obtained by adding all its digits.

Example:

  • Input: 12345 → Output: 15 (1 + 2 + 3 + 4 + 5 = 15)
  • Input: 0 → Output: 0

We’ll use a do-while loop, which ensures that the loop executes at least once, even if the number is 0.

C

#include <stdio.h>

 

int main() {

    int num, original, digit, sum = 0;

 

    // Input

    printf("Enter a positive integer: ");

    scanf("%d", &num);

 

    // Validate input

    if (num < 0) {

        printf("Invalid input! Please enter a positive number.\n");

        return 1;

    }

 

    original = num; // store original number

 

    // Sum of digits using do-while loop

    if (num == 0) {

        sum = 0;

    } else {

        do {

            digit = num % 10;   // extract last digit

            sum += digit;       // add to sum

            num /= 10;          // remove last digit

        } while (num != 0);

    }

 

    // Display result

    printf("Sum of digits of %d = %d\n", original, sum);

 

    return 0;

}

Output

 
OUTPUT 1 :
Enter a positive integer: 12345
Sum of digits of 12345 = 15

OUTPUT 2 :
Enter a positive integer: 0
Sum of digits of 0 = 0

OUTPUT 3 :
Enter a positive integer: -987
Invalid input! Please enter a positive number.

Explanation

  1. Read the positive number num and store it in original.
  2. Validate that the input is non-negative.
  3. Use a do-while loop:
    • Extract the last digit: digit = num % 10.
    • Add it to sum.
    • Remove the last digit: num /= 10.
  4. Loop continues until num becomes 0.
  5. Print the sum of digits.