forked from NischayKG/My_Java_Programs_of_Strings
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsum_of_digits.java
More file actions
21 lines (19 loc) · 739 Bytes
/
sum_of_digits.java
File metadata and controls
21 lines (19 loc) · 739 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
//Program to Read a String and find the Sum of all Digits in the String
import java.util.*;
class sum_of_digits
{
public static void main(String arr[])
{
Scanner sc=new Scanner(System.in);
StringBuffer str=new StringBuffer(sc.nextLine());
int sum=0;
for(int i=0;i<str.length();i++)
{
if(str.charAt(i)=='0'||str.charAt(i)=='1'||str.charAt(i)=='2'||str.charAt(i)=='3'||str.charAt(i)=='4'||str.charAt(i)=='5'||str.charAt(i)=='6'||str.charAt(i)=='7'||str.charAt(i)=='8'||str.charAt(i)=='9')
{
sum+=Character.getNumericValue(str.charAt(i));
}
}
System.out.println("The sum of digits are ="+sum);
}
}