Programming Examples

Java program to pass an integer as argument and find the sum of odd digits and even digits separately


Write a program to pass an integer as argument and find the sum of odd digits and even digits separately.

Solution

class SumOfDigit
{
	public static void main(int a)
	{
		int i,d,se=0,so=0;
		for(i=a;i>0;i=i/10)
		{
			d=i%10;
			if(d%2==0)
				se=se+d;
			else
				so=so+d;
		}
		System.out.println("Sum of even digits="+se);
		System.out.println("Sum of odd digits="+so);
	}
}
Output

Online Exam Quiz for One day Exam Online Typing Test CCC Online Test 2026 Best Computer Training Institute in Prayagraj (Allahabad) Best Java Training Institute in Prayagraj (Allahabad) Best Python Training Institute in Prayagraj (Allahabad) O Level Online Test in Hindi Best Website and Software Company in Allahabad