-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstringoddevendiff.java
More file actions
33 lines (30 loc) · 859 Bytes
/
stringoddevendiff.java
File metadata and controls
33 lines (30 loc) · 859 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import java.util.Scanner;
//Lab Problem
public class stringoddevendiff {
public static void findDiff(String num)
{
int even = 0;
int odd = 0;
for (int i = 0; i < num.length(); i++)
{
int digit = num.charAt(i);
if (i % 2 == 0)
{
even += digit; //Add Even
} else
{
odd += digit; //Add Odd
}
}
int diff = even - odd;
System.out.println("Even Sum: " + even);
System.out.println("Odd Sum: " + odd);
System.out.println("Difference: " + Math.abs(diff));
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a number: ");
String num = sc.nextLine();
findDiff(num);
}
}