First, solve the problem. Then, write the code. ” - John Johnson

It's not at all important to get it right the first time. It's vitally important to get it right the last time. ” - Andrew Hunt and David Thomas

Tuesday, July 15, 2014

Primary Arithmetic

Name of programmer: Jisan
Programming language: java
Problem number: UVA-10035
Difficulty: Easy
http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=976

Help: http://docs.oracle.com/javase/7/docs/api/java/lang/Math.html

Code:
import java.util.*;
public class Uva_10035 {
public static void main(String args[]){
Scanner in=new Scanner(System.in);
while(in.hasNext()){
int a=in.nextInt();
int b=in.nextInt();
if(a==0 && b==0){
break;
}
int count =count(a,b);
if(count==0)
               System.out.println("No carry operation.");
           else if(count==1)
               System.out.println("1 carry operation.");
           else
               System.out.println( count + " carry operations.");
}
}

public static int count(int a, int b) {
int sum=0;
int carry=0;
int count=0;
while(a>0 || b>0){
sum=carry+(a%10)+(b%10);
if(sum>=10){
count++;
}
carry=sum/10;
a=a/10;
b=b/10;

}
return count;
}

}

No comments:

Post a Comment