What is wrong in this code

package Challenge;

import java.util.Scanner;

public class OddEvencar {
public static void main(String[] args) {
Scanner scn = new Scanner(System.in);
int n = scn.nextInt();
for (int i = 1; i <= n; i++) {
int m = scn.nextInt();
int sum = 0;
for (int j = 1; j > 0; j++) {
int num = m % 10;
sum = sum + num;
m = m / 10;
}
if (sum % 2 == 0) {
int div4 = m % 100;
if (div4 % 4 == 0) {
System.out.print(“Yes”);
}
} else {
if (sum % 3 == 0) {
System.out.print(“Yes”);
}
}

	}

}

}

Hey @kalindiyadav5
you are doing wrong
Change your logic
The Question in Simple language is that:

  • You have given a number and in that number, the digits which are even should have sum divisible by 4 or the sum of digits which are odd should be divisible by the 3, Only then you can say yes otherwise you will say no.
    Algorithm
  • Take input of the Number.
  • Declare two variables to store the sum of even numbers and odd numbers.
  • Extract digit one by one ( by % 10).
    1. Check if the number is even or odd.

    2. If even add the number in the variable storing even sum.

Othewise add it in the variable storing odd sum.

  • After the loop, Check if the even sum is divisible by 4 or odd sum is divisible by 3.
    print Yes.
    2. otherwise print No.
1 Like

package Challenge;

import java.util.Scanner;

public class OddEvencar {
public static void main(String[] args) {
Scanner scn = new Scanner(System.in);
int n = scn.nextInt();
for(int i =1; i<=n;i++) {
int m = scn.nextInt();
int sumE=0;
int sumO=0;

	if(m%2==0) {
		int j=1;
		while(j!=0) {
		int v =m%10;
		sumE=v+sumE;
		m=m/10;
		j++;
		}
		}else {
			int k =1;
			while(k!=0) {
				int b=m%10;
				sumO=b+sumO;
			}
		}
	if(sumO%3==0 && sumE%4==0 ) {
		System.out.print("Yes");
	}else {
		System.out.print("No");
	}
	
	}
}

}

What is wrong in this code? i modified and cannot understand at what point i m going wrong

@kalindiyadav5

import java.util.Scanner;

public class Main {
public static void main(String[] args) {
Scanner scn = new Scanner(System.in);
int n = scn.nextInt();
for (int i = 1; i <= n; i++) {
int m = scn.nextInt();
int sumE = 0;
int sumO = 0;
while (m != 0) {
int rem = m % 10;
if (rem % 2 == 0) {
sumE += rem;
} else {
sumO += rem;
}
m=m/10;
}

		if (sumO % 3 == 0 || sumE % 4 == 0) {
			System.out.println("Yes");
		} else {
			System.out.println("No");
		}

	}
}

}

1 Like

I understood your code. And it is a much easier way to do. Thank you. But where was i going wrong?

@kalindiyadav5
if(m%2==0) { // you are checking M is odd ya even but mujhe digit check krna h
int j=1;
while(j!=0) {
int v =m%10;
sumE=v+sumE;
m=m/10;
j++;
}
}

1 Like

Okayyyyyy. Now i got the question . Got it . i understood something different. Thank you so much