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

Answers were Sorted based on User's Feedback



Write a program for Sum of the digits for a given number Ex: suppose user entered Number is 1214, ..

Answer / vishal

public void getDigitSum(int num){
int remainder=0;
int sum=0;
String strResult="";
int counter=0;
while(num>0){
remainder=num%10;
sum=sum+remainder;
counter++;
if(counter==1){
strResult=String.valueOf(remainder);
}
else{
strResult=String.valueOf(remainder)+"+"+strResult;
}
num=num/10;


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

Is This Answer Correct ?    11 Yes 0 No

Write a program for Sum of the digits for a given number Ex: suppose user entered Number is 1214, ..

Answer / 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

More J2EE Interview Questions

What is j2ee stand for?

0 Answers  


What is the difference between sendRedirect() and forward()? in what situations do we have to use send redirect() instead of forward().

13 Answers   Honeywell, Mind Tree, Samsung, Wipro,


what is interface in java ?can you explain with simple example?and what is the difference between abstract and interface?

0 Answers   HCL,


where exactly the servlets and jsp's are stored?

6 Answers   CG-VAK, JHJ, TCS,


What is included in j2ee?

0 Answers  


What are the four components of j2ee application?

0 Answers  


Is j2ee a framework?

0 Answers  


What are j2ee technologies?

0 Answers  


Which framework is best for java?

0 Answers  


How does javac compiler work?

0 Answers  


What spring is in related to j2ee?

0 Answers  


Diffrence between arraylist and hashmap

3 Answers   Oracle,


Categories