FLOW001:Addition of two numbers

// can anyone please tel me whats wrong in this answer

import java.util.Scanner;
public class useradd{
public static void main (String []args){

int result;
Scanner scan =new Scanner (System.in);
int a= scan.nextInt();
int b= scan.nextInt();
int T=scan.nextInt();
if (T<1000){
if (a<10000){
    
if (b<10000){
    

    result  = a+b;
    
    System.out.println (result);
    }
    }

}
System.out.println (“user input addition is conpleted”);
}

}

You should read “how to get started” with Codechef. Link is given on Practice page. By the way,
Here T is no of test cases that means you have to answer T times so you should use a loop that runs T times. And again see the output format. Every time you should print the sum in new line. And don’t print anything extra that will lead to a wrong answer. In your case, printing “user input addition is conpleted” is not expected. So remove that line.
Another mistake is, you are scanning a then b then T. But see the input format, First T is given, Then T times, a and b are given.
So what you need is

  1. Scan T
  2. Put a loop for T test cases
  3. Read a and b for each iteration of loop
  4. Print the sum and go to next line
  5. Remove System.out.println(“user input addition is conpleted”);
    I hope this helps!!!
1 Like

// can anyone tell me what is wrong here.
import java.util.;
import java.lang.
;
import java.io.*;

/* Name of the class has to be “Main” only if the class is public. */
class Codechef
{
public static void main (String[] args) throws java.lang.Exception
{
Scanner sc=new Scanner(System.in);
int t=sc.nextInt();
if(t>=1&&t<=1000)
{
for(int i=1;i<=t;i++)
{
int sum;
int a=sc.nextInt();
int b=sc.nextInt();
if(a>=1 &&a<=10000 && b>=1 && b<=10000)
{
sum=a+b;
System.out.println(sum);
}
}

}

}

  1. You cannot use the local variable without initializing.
  2. Here sum is not initialized.