Delhi Odd Even problem in java how to approach that

Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int rem=0;
int sum1=0;
int sum2=0;
while(n!=0) {
rem=n%10;
n=n/10;
System.out.println(rem);

	 if(rem%2==0) {
		 
		  sum1=sum1+rem;
		  System.out.println("Sum of even numbers is"+sum1);  
	 }
	 else
	 {
		  sum2=sum2+rem;
	  System.out.println("Sum of odd numbers is"+sum2);  	
	  
     }
	  
	 
	 }

}
}
OUTPUT
1234
4
Sum of even numbers is4
3
Sum of odd numbers is3
2
Sum of even numbers is6
1
Sum of odd numbers is4
//I want that sum of even and odd must be final i.e even=6 and odd=4 not first 4 then 6 for even and 1 and 3 for odd

Hi Nirmal
The approach you used for this question is correct. But the output you are printing is wrong. At the end of the while loop, the final even and odd sum is stored in sum1 and sum2 respectively. You dont have to print sum1 ans sum2 in each iteration. Just check if the even sum is divisible by 4 and odd sum is divisible by 3. If either of them is true, print Yes.

Hii Riya
Can u plz tell me how to take input from the user for this question like it is given 2 then 12345 and 12312.So how could I achieve that trying it for so long but donโ€™t get any idea .It will be kind of u if u help me out .

Hi Nirmal
Here the first line of input contains N i.e., the number of test cases and then the subsequent lines contain the number of each car.

public static void main(String args[]) {

   Scanner scn = new Scanner(System.in);
   int n = scn.nextInt();
	
	while(n > 0) {
		
		int num = scn.nextInt();
		
		oddeven(num);
		n--;
	}
}

Here, oddeven() is the function to check if the output will be Yes or No.
Hope this helps.

1 Like

Hey thanx for that and keep helping me others too๐Ÿ˜

NOT ABLE TO UNDERSTAND WHAT I AM DOING WRONG
Scanner scn=new Scanner(System.in);
int n=scn.nextInt();
int tot=0;
int ct=4;
int ctr;

	if (n%2!=0) ctr=1;
	else ctr=2;
	
	for(int i=1;i<=ct;i++) 
	{ int sum=n%10; tot=sum+tot;}
	if (tot%3==0&&ctr==1)
		{System.out.print("YES");

		}
	else if  
	(tot%4==0&&ctr==2)
		{System.out.print("YES");}
	
	else
	
	{System.out.print("NO");}