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

What is awt panel java?

980


What is java advanced?

951


Is a relationship java?

945


What is server in j2ee?

938


What is the concept of connection pooling?

1065


how to run servlet program between two computer through internet ?

2995


What is iso 3166?

1085


What is java 2 platform, enterprise edition (j2ee)?

956


What is application client?

1043


What is full form of j2ee?

1105


What is jta and jts?

975


What are struts?

966


Is core java and j2se same?

905


What is j2ee and j2se?

997


What is java api for xml processing (jaxp)?

1021