Calculations

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

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

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

In the Java 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 numbers

program to find the sum of digits

Calculate the sum of digits using the for loop

Program 1

import java.util.Scanner;
class SumOfDigits1{
public static void main(String args[]){
    long count,sum=0,digits;
    Scanner scan=new Scanner(System.in);
//create a scanner object for input

//get input from the user for rows
System.out.print("Enter the number for sum: ");
long num=scan.nextInt();
 
count=num;
for(count=num; num>0; num/=10)
{
    digits=num%10;
    sum=sum+digits;
    
}

System.out.print("Given number is: "  + count);
System.out.print("\nSum of the digits: "  + sum);
}
}

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

Case 1

Enter the number for sum:1100
Given number is: 1100
Sum of the digits:2

Case 2

Enter the number for sum:3456
Given number is: 3456
Sum of the digits:18

 

Calculate the sum of digits using the while loop

Program 2

import java.util.Scanner;
class SumOfDigits{
public static void main(String args[]){
    long count,sum=0,digits;
    Scanner scan=new Scanner(System.in);
//create a scanner object for input

//get input from the user for rows
System.out.print("Enter the number for num: ");
long num=scan.nextInt();
 count=num;

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

System.out.print("Given number is: "  + count);
System.out.print("\nSum of the digits: "  + sum);
}
}

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

case 1

Enter the number:1200
Given number is: 1200
Sum of the digits:3

 

case 2

Enter the number:1468
Given number is: 1468
Sum of the digits:19

 

Calculate the sum of digits using the do-while loop

Program 3

import java.util.Scanner;
class SumOfDigits2{
public static void main(String args[]){
    long count,sum=0,digits;
    Scanner scan=new Scanner(System.in);
//create a scanner object for input

//get input from the user for rows
System.out.print("Enter the number for num: ");
long num=scan.nextInt();
 count=num;
do{
    digits=num%10;
    sum=sum+digits;
    num/=10;
    
}
while(num>0);


System.out.print("Given number is: "  + count);
System.out.print("\nSum of the digits: "  + sum);
}
}

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

case 1

Enter the number for num: 1111
Given number is:1111
Sum of the digits:4

 

case 2

Enter the number for num: 9876
Given number is:9876
Sum of the digits:30

 

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 store the variable num and to find the sum of digits.
  • 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 Java language

while loop in Java language

do-while loop in Java language

Loops in Java language

Operator in Java language

Data type and variable in Java language

 

Find the product of two numbers using recursion in Java
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