Deprecated: mysql_connect(): The mysql extension is deprecated and will be removed in the future: use mysqli or PDO instead in /home/purvgwtw/public_html/tutorials/common.php on line 7
Java find sum of digits
Sponsered Links
Categories
Sponsered Links

Java find sum of digits

Here we are going to find the sum of digits of a number entered by the user.

import java.util.*;

class SumOfDigits{
public static void main(String args[]){
int sum=0;
System.out.println("enter a value");
Scanner input=new Scanner(System.in);
int n=input.nextInt();
int t=n;
while(n>0)
{
int p=n%10;
sum=sum+p;
n=n/10;
}
System.out.println("sum of the digits in "+t+" is "+sum);
}
}


 

Sponsered Links
Latest Updates
 
All Content of this site is for learning only. We do not warrant the correctness of its content. The risk from using it lies entirely with the user. While using this site, you agree to have read and accepted our terms of use and privacy policy.
Copyright © 2014 PURVACOMPUTERS.COM All Right Reserved