Thursday, 20 February 2014

UVA Problem ID 10035 (Primary Arithmetic)

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Main
{
	public static void main(String[] args) throws IOException 
    {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringBuilder sb=new StringBuilder("");
        String input;
        int noofoperation=0;
        while((input=br.readLine())!=null)
        {  noofoperation=0;	
        	String s[]=input.split(" ");
        	
        	if(s[0].equals("0") && s[1].equals("0"))
        		break;
        	int maxlength=s[1].length()>s[0].length()?s[1].length():s[0].length();
        	int carry=0;
        	while(s[0].length()=0; i--)
			{
				int sum=Integer.parseInt(s[0].charAt(i)+"")+Integer.parseInt(s[1].charAt(i)+"")+carry;
				carry=0;
				if(sum>=10)
				{	
					carry=1;
					noofoperation++;
				}
			}
        	if(noofoperation==0)
        		sb.append("No carry operation.\n");
        	else if(noofoperation==1)
        		sb.append("1 carry operation.\n");
        	else
        		sb.append(noofoperation+" carry operations.\n");
        }
        System.out.print(sb);
    }
}

Previous Post
Next Post

0 comments:

Advertisement