Sum of K digits in JAVA




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

public class SumOfK {

/**
* @param args
* @throws IOException
* @throws NumberFormatException
*/
public static void main(String[] args) throws NumberFormatException, IOException {

BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int m = Integer.parseInt(br.readLine());
int a[] = new int[m];

for(int i=0;i<m;i++){
a[i]= Integer.parseInt(br.readLine());

}
int k = Integer.parseInt(br.readLine());
int sum=0;
try {
for(int i=0;i<m-1;i++){

sum=0;
sum+=a[i];
if((sum+=a[i+1])!=k){

continue;

}else{

System.out.println(a[i]);
System.out.println(a[i+1]);
break;
}

}

} catch (Exception e) {
// TODO: handle exception
}


}

}

 


Sandeep Kumar D

Hi, I have written and developed this post so that most of people will be benefited. I'm committed to provide easy and in-depth tutorials on various technologies.I hope it will help you a lot.
- Sandeep Kumar D

Follow Me @Google+
SHARE

    Blogger Comment
    Facebook Comment

0 comments :

Post a Comment