

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Find 2’c complements for given binary number using C language
Consider an example given below −
Example
The input is as follows:
Enter a binary number:10010001
The output is as follows:
1's complement of 10010001 is 01101110
2's complement of 10010001 is 01101111
Algorithm
Refer an algorithm to find 2’c complements for a given binary number.
Step 1 − Start.
Step 2 − Read the binary number at runtime.
Step 3 − Copy the binary number to strdp.
Step 4 − len: = strlen(str)
Step 5 − For i = 0 to len-1 do
Step 5.1 − if str[i] == ‘1’ then
Step 5.1.1 − str[i] == ‘0’
Step 5.2 − Else
Step 5.2.1 − str[i] == ‘1’
Step 5.3 − i: = i+1
Step 6 − Mask: = 1
Step 7 − For i: = len-1 to 0 do
Step 7.1 − if mask == 1 then
Step 7.1.1 − if str[i] == ‘1’ then
Step 7.1.1.1 − str[i]: = ‘0’
Step 7.1.1.2 − mask: = 1
Step 7.1.2 − else
Step 7.1.2.1 − str[i]: = ‘1’
Step 7.1.2.2 − mask: = 0
Step 7.1.3 − End if
Step 7.2 − End if
Step 8 − Print the 2’s complement.
Step 9 − Stop.
Program
Following is the C program to find 2’c complements for a given binary number −
#include <string.h> #include<stdio.h> main(){ char str[32],strdp[32]; int mask,i; printf("Enter a binary number:"); scanf("%s",str); strcpy(strdp,str); for(i=0;i<strlen(str);i++) /* computing 1's complement */{ if(str[i]=='1') str[i]='0'; else str[i]='1'; } printf("1\'s complement of %s is %s\n",strdp,str); mask=1; for(i=strlen(str)-1;i>=0;i--){ if(mask==1){ if(str[i]=='1'){ str[i]='0'; mask=1; } else{ str[i]='1'; mask=0; } } } printf("2\'s complement of %s is %s",strdp,str); }
Output
When the above program is executed, it produces the following result −
Enter a binary number:11001110 1's complement of 11001110 is 00110001 2's complement of 11001110 is 00110010
- Related Questions & Answers
- Find square root of number upto given precision using binary search in C++
- C++ Program to Find the Number of occurrences of a given Number using Binary Search approach
- Find the sum of first and last digit for a number using C language
- How to find the product of given digits by using for loop in C language?
- Find the slope of the given number using C++
- Binary representation of a given number in C++
- C program to find Fibonacci series for a given number
- Explain Binary search in C language
- How to multiply a given number by 2 using Bitwise Operators in C#?
- Using iterative function print the given number in reverse order in C language
- How to find whether the Number is Divisible by 2 using C#?
- How to find minimum element in an array using binary search in C language?
- Find all combinations that add upto given number using C++
- How to convert binary to Hex by using C language?
- Find 2^(2^A) % B in C++