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

Is tomcat a java ee?

852


What is the difference between java and core java?

908


Who gave the name java?

814


Explain the four types of container that the j2ee specification defines

882


Define connection pooling?

864


What is data?

911


What is actionform?

913


What is a java applet?

918


What are the j2ee client types?

885


Is downloading java safe?

828


What is declarative security ?

993


What is bean factory, have you used xmlbean factory?

877


What is full form of j2ee?

963


Why java is oop language?

899


What is ear file?

885