Repository navigation
Expand file tree
/
Copy pathSumOfDigitsOfGivenNum.java
More file actions
65 lines (61 loc) · 1.51 KB
/
Copy pathSumOfDigitsOfGivenNum.java
File metadata and controls
65 lines (61 loc) · 1.51 KB
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
// Program to calculate and return the sum of the all digits, even digits and prime digits of a user given number.
import java.util.Scanner;
public class SumOfDigitsOfGivenNum
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
System.out.println("The sum of all the digits = "+sumOfDigitsOfGivenNum(n));
System.out.println(" The sum of even digits = "+sumOfEvenDigits(n));
System.out.println(" The sum of odd digits = "+sumOfOddDigits(n));
System.out.println(" The sum of prime digits = "+sumOfPrimeDigits(n));
sc.close();
}
public static int sumOfDigitsOfGivenNum(int n)
{
int sum=0;
while(n!=0)
{
sum=sum+n%10;
n=n/10;
}
return sum;
}
public static int sumOfEvenDigits(int n)
{
int sum=0;
while(n!=0)
{
int r=n%10;
if (r%2==0)
sum=sum+r;
n=n/10;
}
return sum;
}
public static int sumOfOddDigits(int n)
{
int sum=0;
while(n!=0)
{
int r=n%10;
if (r%2!=0)
sum=sum+r;
n=n/10;
}
return sum;
}
public static int sumOfPrimeDigits(int n)
{
int sum=0;
while(n!=0)
{
int r=n%10;
if (r==2 || r==3 || r==5 || r==7)
sum=sum+r;
n=n/10;
}
return sum;
}
}