Subtract Two Numbers Operator without using Minus(-) operator

Problem statement:- Program to Subtract Two Numbers Operator without using Minus(-) operator.

 Data requirement:-

  Input Data:- num1,num2

  Output Data:-sub

Program in C
  
Here is the source code of the C Program to Subtract Two Numbers Operator without using Minus(-) operator.

#include <stdio.h>
int main ()
{
    int num1,num2;
    printf("Enter the first number\n");
    scanf("%d",&num1);
    printf("Enter the second number\n");
    scanf("%d",&num2);
    int sub=num1+(~num2+1);//number + 2's complement of number
    printf("Subtraction of two number is %d",sub);
}

Input/Output:
Enter the first number
10
Enter the second number
5
Subtraction of two number is 5

Program in C++
  
Here is the source code of the C++ Program to Subtract Two Numbers Operator without using Minus(-) operator.

#include <iostream>
using namespace std;
int main ()
{
    int num1,num2;
    cout<<"Enter the first number\n";
    cin>>num1;
    cout<<"Enter the second number\n";
    cin>>num2;
    int sub=num1+(~num2+1);//number + 2's complement of number
    cout<<"Subtraction of two number is "<<sub;
}

Input/Output:
Enter the first number
50
Enter the second number
100
Subtraction of two number is -50

Program in Java
  
Here is the source code of the Java Program to Subtract Two Numbers Operator without using Minus(-) operator.

import java.util.Scanner;
public class SubtractionWithoutSubtractionSign {

public static void main(String[] args) {
Scanner cs=new Scanner(System.in);
int num1,num2;
    System.out.println("Enter the first number:");
    num1=cs.nextInt();
    System.out.println("Enter the second number:");
    num2=cs.nextInt();
    int sub=num1+(~num2+1);//number + 2's complement of number
   System.out.println("Subtraction of two number is "+sub);
   cs.close();
}
}

Input/Output:
Enter the first number:
78
Enter the second number:
25
Subtraction of two number is 53

Program in Python
  
Here is the source code of the Python Program to Subtract Two Numbers Operator without using Minus(-) operator.

num1=int(input("Enter first number:"))
num2=int(input("Enter  second number:"))
sub=num1+(~num2+1)#number + 2's complement of number
print("Subtraction of two number is ",sub)

Input/Output:
Enter first number:3350
Enter  second number:1000
Subtraction of two number is  2350


Post a Comment

0 Comments