Help me in solving LBJ21 problem Output are correct but fails after submitting

My issue

My code

// Update the code below to solve this problem
import java.util.Scanner;

class Codechef
{
	public static void main (String[] args)
	{
		Scanner read = new Scanner(System.in);
		
		int t = read.nextInt(); 
		for(int i=0; i<t; i++)
		{
    		int p = read.nextInt();
    		
    		if(p%100>9){
    		    System.out.println("-1");
    		}else if (p%100 ==0){
    		    System.out.println(p/100);
    		}
    		else{
    		    int a = p/100;
    		    int b = p%10;
    		    System.out.println(a+b);
    		} 

		}
	}
}

Learning course: Solve Programming problems using Java
Problem Link: CodeChef: Practical coding for everyone

@smparatwar
For p=10 it will print -1 but answer would be 10;

import java.util.Scanner;

class Codechef
{
public static void main (String[] args)
{
Scanner read = new Scanner(System.in);

	int t = read.nextInt(); 
	for(int i=0; i<t; i++)
	{
		int p = read.nextInt();
		
		if(p%100>9){
		    System.out.println("-1");
		}
		else if(p == 10){
		    System.out.println("10");
		    
		}
		
		else if (p%100 ==0){
		    System.out.println(p/100);
		}
		else{
		    int a = p/100;
		    int b = p%10;
		    System.out.println(a+b);
		} 

	}
}

}

still it gives wrong answer

@smparatwar
Your code still print -1 for p=10 just run it .

// Update the code below to solve this problem
import java.util.Scanner;

class Codechef
{
public static void main (String[] args)
{
Scanner read = new Scanner(System.in);

	int t = read.nextInt(); 
	for(int i=0; i<t; i++)
	{
		int p = read.nextInt();
		
		if(p == 10){
		    System.out.println("10");
		}
		else if(p%100>9){
		    System.out.println("-1");
		    }
		
		else if (p%100 ==0){
		    System.out.println(p/100);
		}
		else{
		    int a = p/100;
		    int b = p%10;
		    System.out.println(a+b);
		} 

	}
}

}
Now for 10 it is printing 10 but still wrong ?

@smparatwar
for p=1000 it should print 10 but your code will print -1;

import java.util.Scanner;

class Codechef
{
public static void main (String[] args)
{
Scanner read = new Scanner(System.in);

	int t = read.nextInt(); 
	for(int i=0; i<t; i++)
	{
		int p = read.nextInt();
       
        if(p%100 == 0 || p%100 < 9){
            int a = p/100;
            int b = p%10;
            System.out.println(a+b);
        }
        else{
            System.out.println(-1);
        }
        
        
            
        }
	}
}

tried by editing code but same results the answers are correct after running but after submitting it states wrong answer please help.

@smparatwar
Here u go i have written in much simpler way hope u will get it.

// Update the code below to solve this problem
import java.util.Scanner;

class Codechef
{
	public static void main (String[] args)
	{
		Scanner read = new Scanner(System.in);
		
		int t = read.nextInt(); 
		for(int i=0; i<t; i++)
		{
    		int p = read.nextInt();
    		int sc=p/100  +  p%100;
    		if(sc<=10)
    	    System.out.println(sc);
    	    else
    	    System.out.println(-1);

		}
	}
}

thank you bhaii

1 Like