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

 

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

PHP Star Triangle pattern program

PHP Star Triangle pattern program In this tutorial, we will discuss about PHP Star Triangle…

2 months ago

PHP Full Pyramid pattern program

PHP Full Pyramid pattern program In this tutorial, we will discuss about PHP Full Pyramid…

2 months ago

5 methods to add two numbers in Java

5 methods to add two numbers in Java In this tutorial, we will discuss the…

2 months ago

Python Full Pyramid star pattern program

Python full Pyramid star pattern program In this tutorial, we will discuss  the concept of…

5 months ago

Write a function or method to convert C into F -Entered by user

Write a function or method to convert C into F -Entered by the user In…

10 months ago

How to write a function or method to convert Celsius into Fahrenheit

How to write a function or method to convert Celsius into Fahrenheit In this tutorial,…

10 months ago