Raghu Institute of Technology
Raghu Institute of Technology
R13 Syllabus
And for obtaining 2s compliment, add 1 to the 1s compliment binary digit obtained
i.e.,
00010
+
1
-------------------------00011
ALGORITHM:
STEP 1: START
STEP 2: Declare array a[50],I,j,n
STEP 3: Read n,a[i]
STEP 4: For I in steps of 1 do where i>=0 and i=n-1
4.1: Check the condition a[i]==1
4.1.1 :Until 4.1 condition satisfies the loop is executed.
STEP 5: For j=i-1 in steps of 1 where j>=0
5.1:
5.1.1: If true then a[j]0
5.1.2: If the condition is false then a[j]1
STEP 6: DISPLAY 2s complement
STEP 7: STOP.
FLOWCHART:
START
Declare a[50],i,j,n
Read n,a[i]
False
For i in steps of 1 do
where i>=0 and i=n-1
True
a[i]==1
True
break
False
a[j]=1
For i in steps of 1 do
where i<n
True
Display 2s
complement
STOP
False
PROGRAM
/* C Program to calculate 2s complement */
Program name:
/* Done By : C-Faculty
// wk2a.c
Dated: 15/10/2013*/
#include <stdio.h>
#include <curses.h>
int main()
int a[50],i,j,n;
clear();
printf(Enter how many bits you want); // Displays Enter how many bits you want
scanf(%d,&n);
for(i=0;i<n;i++)
// Running a loop
scanf(%d , &a[i]);
for(i=n-1;i<=0;i--)
// Running a loop
if (a[i])==1)
{
break;
}
}
for(j=i-1;j>=0;j--)
// Running a loop
{
if (a[j] == 1)
{
a[j]=0;
}
else
{
a[j]=1;
}
}
printf(The 2s complement is :); // Displays the 2s complement
for(i=0;i<n;i++)
// Running a loop
printf(%d,a[i]);
return(0);
0
The 2s complement is 000100
ORIGINAL OUTPUT :
Output (1)
Enter how many bits you want
5
Enter the bits 1
1
1
0
The entered binary number is 11100
Output (2)
Enter how many bits you want
5
Enter the bits 1
0
0
0
The entered binary number is 10000
0
The 2s complement is 000100
0
The 2s complement is 10001