rajamit872

Alternate Addition of any Number

Sep 22nd, 2014
189
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.68 KB | None | 0 0
  1. import java . util .Scanner;
  2. class Ac{
  3. static void acceptData(){
  4. String str="";
  5. int sum=0,sum1=0;
  6. System.out.println("Enter the String ::");
  7. Scanner in = new Scanner(System.in);
  8. str = in.nextLine();
  9. char ch[] = str.toCharArray();
  10. int ch1[] = new int[ch.length];
  11. int a[] = new int[ch1.length];
  12. for(int i=0;i<ch.length;i++)
  13. System.out.print(ch[i]);
  14. for(int i=0;i<ch.length;i++)
  15. {
  16. ch1[i] = java.lang.Integer.parseInt(String.valueOf(str.charAt(i)));
  17. }
  18. for(int j=0;j<ch1.length;j++)
  19. {
  20. if(j==0 || j%2==0)
  21. sum+=ch1[j];
  22. else
  23. sum1+=ch1[j];
  24. }
  25. System.out.println("Sum::" + sum);
  26. System.out.println("Sum1::" + sum1);
  27. }
  28. public static void main(String args[]){
  29. acceptData();
  30. }
  31. }
Advertisement
Add Comment
Please, Sign In to add comment