Sum OR Difference: DIFFSUM

Sum OR Difference CodeChef Solution in C, C++, Java, and Python

Public Submission link: Click here (anyone can submit here )

CU Submission link: Click here (need login by CU account)


Write a program to take two numbers as input and print their difference if the first number is greater than the second number otherwiseotherwise print their sum.

Input Format

  • First line will contain the first number (N1N1)
  • Second line will contain the second number (N2N2)

Output Format

Output a single line containing the difference of 2 numbers (N1−N2)(N1−N2) if the first number is greater than the second number otherwise output their sum (N1+N2)(N1+N2).

Constraints

  • −1000≤N1≤1000−1000≤N1≤1000
  • −1000≤N2≤1000−1000≤N2≤1000

Sample Input 1 

82 28

Sample Output 1 

54

Sum OR Difference CodeChef Solution in C

// your code goes here
	int a,b;
	scanf("%d%d",&a,&b);
	if(a>b){
	printf("%d",a-b);
	}
	else{
	    printf("%d",a+b);
	}

Sum OR Difference CodeChef Solution in C++ 14


// your code goes here
    int a,b;
    cin>>a>>b;
    if(a>b)
    cout << a-b;
    else
    cout << a+b;
	return 0;
}

Sum OR Difference CodeChef Solution in Python 3


a=int(input())
b=int(input())
if a>b:
    print(a-b)
else:
    print(a+b)

Sum OR Difference CodeChef Solution in Java

/* package codechef; // don't place package name! */

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 n=sc.nextInt();
        int m=sc.nextInt();
        if(n>m)
        {
            System.out.println(n-m);
        }
        else
        {
            System.out.println(m+n);
        }
	}
}

More Codechef Solution: Click Here




Sum OR Difference CodeChef Solution from

Sum OR Difference CodeChef Solution
See also  Chef And Operators: CHOPRT