Golgappa.net | Golgappa.org | BagIndia.net | BodyIndia.Com | CabIndia.net | CarsBikes.net | CarsBikes.org | CashIndia.net | ConsumerIndia.net | CookingIndia.net | DataIndia.net | DealIndia.net | EmailIndia.net | FirstTablet.com | FirstTourist.com | ForsaleIndia.net | IndiaBody.Com | IndiaCab.net | IndiaCash.net | IndiaModel.net | KidForum.net | OfficeIndia.net | PaysIndia.com | RestaurantIndia.net | RestaurantsIndia.net | SaleForum.net | SellForum.net | SoldIndia.com | StarIndia.net | TomatoCab.com | TomatoCabs.com | TownIndia.com
Interested to Buy Any Domain ? << Click Here >> for more details...

Write a program for Sum of the digits for a given number
Ex: suppose user entered Number is 1214,
It should calculate the above number like 1+2+1+4 = 8

Answer Posted / gunreddy

Scanner scan = new Scanner(System.in);
int in = scan.nextInt();

String s = in + "";

int sum = 0;
int div = 10;
int di = 1;

for (int i = 0; i < s.length(); i++) {
int rem = in % div;
rem = rem / di;
sum += rem;
div *= 10;
di *= 10;

}
System.out.println("sum " + sum);

Is This Answer Correct ?    3 Yes 0 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

Define connection pooling?

849


Is java safe to download?

819


What is element?

879


What is declaration?

908


What is devops in java?

880


What does enterprise javabeans module contain?

858


What is home handle?

901


What is application client?

932


What is java j2ee developer?

821


What is interpreter in java?

793


What is java netbeans?

859


What is application client module?

925


What is java transaction api (jta)?

879


What is tomcat in j2ee?

819


What is pure java?

942