I want help in my approach ,it is showing wrong but i am getting right output as expected

For a positive integer MM, MoEngage defines digitSum(M)digitSum(M) as the sum of digits of the number MM (when written in decimal).

For example, digitSum(1023)=1+0+2+3=6digitSum(1023)=1+0+2+3=6.

Given a positive integer NN, find the smallest integer XX strictly greater than NN such that:

  • digitSum(N)digitSum(N) and digitSum(X)digitSum(X) have different parity, i.e. one of them is odd and the other is even.

Input Format

  • The first line contains an integer TT, the number of test cases. The description of the TT test cases follow.
  • Each test case consists of a single line of input with a single integer, the number NN.

Output Format

  • For each test case, print in a single line, an integer, the answer to the problem.

Constraints

  • 1≤T≤10001≤T≤1000
  • 1≤N<1091≤N<109

Sample Input 1

3
123
19
509

Sample Output 1

124
21
511

Explanation

Test Case 11: digitSum(123)=1+2+3=6digitSum(123)=1+2+3=6 is even and digitSum(124)=1+2+4=7digitSum(124)=1+2+4=7 is odd, so the answer is 124124.

Test Case 22: digitSum(19)=1+9=10digitSum(19)=1+9=10 is even, digitSum(20)=2+0=2digitSum(20)=2+0=2 is also even, whereas digitSum(21)=2+1=3digitSum(21)=2+1=3 is odd. Hence, the answer is 2121.

Test Case 33: digitSum(509)=5+0+9=14digitSum(509)=5+0+9=14 is even, digitSum(510)=5+1+0=6digitSum(510)=5+1+0=6 is also even, whereas digitSum(511)=5+1+1=7digitSum(511)=5+1+1=7 is odd. Hence, the answer is 511511.

–>Here is the question and this my approach.

#include<iostream>
using namespace std;

int next_digitsum(int n,int res)
{
int sum = 0, temp = n,r;
while (temp != 0)
{ // int temp1=n;
sum += temp % 10;
temp = temp / 10;
}
if(sum%2==0)
r=1; //here also i take a variable r which will be initialized by 1 if the sum of the digit is even.
else
r=0; //here 0 for odd sum.
if(r!=res) //this condition is check for even the digit of next number even or odd .
return n;
else
return next_digitsum(n+1,r); //this is the recursive way to call again function with the passed valeue+1,and resent value of r(which may be 1 or 0 as considered) 

}

int main()
{
int t, num,sum=0;
cin >> t;
for (int i = 0; i < t; i++)
{
cin >> num;
int temp=num;
while(num!=0)
{
sum+=num%10;
num=num/10;
}
if(sum%2==0)    //here it will calculate the sum of given number digit and if this sum is even then it will call the function where i am passing value   entered num+1,and 1)(which i have considerd for even sum) 
cout<< next_digitsum(temp+1,1)<<endl;
else
cout<<next_digitsum(temp+1,0); //here the function is called for odd sum so i am passing 0 which i considered for odd sum. 
}
return 0;
}

now please help to find wrong.

Hey @chayan_7478
Thanks for sharing your doubt.

Your code is correct but it has some silly implementation mistakes.

you miss the endl for cout in else statement.

cout<<next_digitsum(temp+1,0)<<endl;

also you have to initialize sum = 0 after loop

for (int i = 0; i < t; i++)

But you declared it before this loop