Calculations

C program to compute the sum of digits in a given numbers

C program to compute the sum of digits in a given numbers

In this tutorial, we will discuss a concept of  C program to compute the sum of digits in a given numbers

In C programming  language, we can use for loop ,while loop and do-while loop to compute the sum of digits in a given numbers

In this article, we are going to learn  how to find the sum of digits in a given number in C language

 

Compute the sum of digits using loops

Compute the sum of digits in a given numbers using for loop

Program 1

#include <stdio.h>
#include <stdlib.h>

int main()
{
    long num,count,digits,sum=0;
    printf("Enter a number\n");
    scanf("%ld",&num);

for(count=num; num>0; num/=10)
{
    digits=num%10;
    sum=sum+digits;

}
printf("Given number is: %ld\n",count);

printf("\nSum of the digits %ld is = %ld\n",count,sum);
getch();
    return 0;
}

When the above code is executed, it produces the following results

Csae 1

Enter a number for find digits sum
111100
Given number is:111100

Sum of the digits  111100 is = 4

 

Csae 2

Enter a number for find digits sum
12345
Given number is:12345

Sum of the digits  12345 is = 15

 

Compute the sum of digits in a given numbers using while loop

Program 2

#include <stdio.h>
#include <stdlib.h>

int main()
{
    long num,count,digits,sum=0;
    printf("Enter a number for find sum of digits\n");
    scanf("%ld",&num);

    count=num;;
while( num>0)
{
    digits=num%10;
    sum=sum+digits;
    num/=10;

}
printf("Given number is: %ld\n",count);

printf("\nSum of the digits %ld is = %ld\n",count,sum);
getch();
    return 0;
}

When the above code is executed, it produces the following results

Case 1

Enter a number for find sum of digits
1030
Given number is:1030

Sum of the digits  1030 is = 4

 

Case 2

Enter a number for find digits sum
56789
Given number is:56789

Sum of the digits  56789 is = 35

 

Compute the sum of digits in a given numbers using do while loop

Program 3

#include <stdio.h>
#include <stdlib.h>

int main()
{
    long num,count,digits,sum=0;
    printf("Enter the number for find sum of digits\n");
    scanf("%ld",&num);

    count=num;;

do{
    digits=num%10;
    sum=sum+digits;
    num/=10;

}while( num>0);
printf("Given number is: %ld\n",count);

printf("\nSum of the digits %ld is = %ld\n",count,sum);
getch();
    return 0;
}

When the above code is executed, it produces the following results

Case 1

Enter a number for find sum of digits
11111
Given number is:11111

Sum of the digits  11111 is = 5

 

Case 2

Enter a number for find digits sum
76543
Given number is:76543

Sum of the digits  76543 is = 25

 

Methods

  • Declare the variable num,count and digits as long type.
  • Declare and initialize variable sum to Zero.
  • Receive input from the user for the value to find the sum of digits and to store the variable num.
  • Using a loop (for loop , while loop, do-while loop) get each digit of the number and add it to the to a variable to find the sum.
  • Display the sum of the digits of the number.

 

Similar post

C program to compute the sum of digits in a given numbers

C++ program to compute the sum of digits in a given numbers

Python program to compute the sum of digits in a given numbers

Addition of two numbers in Java using method

5 method to find sum of two numbers

JavaScript program to add two numbers 

PHP Addition of two numbers 

 

 

Suggested for you

for loop in C language

while loop in C language

do-while loop in C language

Operator in C language

Data type in C language

variable in C language

loops in C language

 

 

 

 

Java program to compute the sum of digits in a given numbers
C++ program to compute the sum of digits in a given numbers
Karmehavannan

I am Mr S.Karmehavannan. Founder and CEO of this website. This website specially designed for the programming learners and very especially programming beginners, this website will gradually lead the learners to develop their programming skill.

Recent Posts

How to find reverse number using method in Java

How to find reverse number using method In this article, we will discuss the concept…

2 days ago

C# inverted full pyramid star pattern

C# inverted full pyramid star pattern In this article, we will discuss the concept of…

3 weeks ago

C# Full Pyramid star pattern program

C# Full Pyramid star pattern program In this article, we will discuss the concept of…

1 month ago

Program to count vowels,consonants,words, characters and space in Java

Program to count vowels, consonants, words, characters and space in Java In this article, we…

1 month ago

How to print multiplication table using Array in C++ language

How to print multiplication table using Array in C++ language In this post, we will…

1 month ago

C Program to multiplication table using Array

C Program to multiplication table using Array In this tutorial , we will discuss about…

2 months ago