Home » C programs » C misc. programs

C Example to subtract two integers without using Minus (-) operator

In this example, we are going to learn how to subtract two integer number without using minus (-) operator in C language program? Here, we will use a bitwise complement to subtract the numbers.
Submitted by Shamikh Faraz, on February 25, 2018

In this program, we will study how we can print subtraction between two integer values without using minus (-) operator? We will use bitwise complement for getting the same.



#include<stdio.h>

int main()
{
	int a,b,sub;
	
	//input numbers
	printf("Enter first number: ");
	scanf("%d",&a);
	printf("Enter second number: ");
	scanf("%d",&b);
	
	//find subtraction using ~
	sub=a+~b+1;
	
	printf("subtraction of %d-%d=%d",a,b,sub);
	
	return 0;
}

Output

Enter first number: 10
Enter second number: 2
subtraction of 10-2=8

Here, sum = a+~b+1 i.e. variable a has its normal value, but the value of variable b is complemented i.e. '2' becomes '-3'.

a+~b+1= 10+(-3)+1=10-3+1=8.






Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.





Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.