Sie sind auf Seite 1von 6

RAGHU INSTITUTE OF TECHNOLOGY

RAGHU INSTITUTE OF TECHNOLOGY


DEPARTMENT OF CSE
College Code : 3J

R13 Syllabus

C PROGRAMMING LAB MANUAL


=======================================================================
Exercise 2
a) 2s complement of a number is obtained by scanning it from right to left and complementing all
the bits after the first appearance of a 1. Thus 2s complement of 11100 is 00100. Write a C
program to find the 2s complement of a binary number.
b) Write a C program to find the roots of a quadratic equation.
c) Write a C program, which takes two integer operands and one operator form the user, performs
the operation and then prints the result. (Consider the operators +,-,*, /, % and use Switch
Statement)
=========================================================================
SPECIFICATION:
(2)(a). 2s complement of a number is obtained by scanning it right to left complementing all the bits
after the first appearance of a 1. Thus 2s complement of 11100 is 00100.
Write a C program to find the 2s complement of a binary number.
DESCRIPTION:
The Given binary number is converted into its 1s Complement i.e.,
Example : 11101

is a binary number and its 1s complement is 00010

And for obtaining 2s compliment, add 1 to the 1s compliment binary digit obtained
i.e.,

00010
+
1
-------------------------00011

Thus , the 2s compliment of 11101 is 00011

Department of Computer Science & Engg

RAGHU INSTITUTE OF TECHNOLOGY

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.

Department of Computer Science & Engg

RAGHU INSTITUTE OF TECHNOLOGY

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

For j=i-1 in steps of


1(decrement) do where j>=0
True
False
a[j]==1
True
a[j]=0

a[j]=1

For i in steps of 1 do
where i<n
True
Display 2s
complement

STOP

Department of Computer Science & Engg

False

RAGHU INSTITUTE OF TECHNOLOGY

PROGRAM
/* C Program to calculate 2s complement */
Program name:
/* Done By : C-Faculty

// wk2a.c
Dated: 15/10/2013*/

#include <stdio.h>

// stdio.h imports i/p and o/p functions

#include <curses.h>

// It imports i/p and o/p functions on curses(Output screen)

int main()

// User defined program

// Main Program Begins

int a[50],i,j,n;

// array is declared for calculating 2s complement

clear();

// Clears the output Screen

printf(Enter how many bits you want); // Displays Enter how many bits you want
scanf(%d,&n);

// Reads n value from the keyboard

printf(enter the bits);

// Displays enter the bits

for(i=0;i<n;i++)

// Running a loop

scanf(%d , &a[i]);

// Reads a[i] value from the keyboard

for(i=n-1;i<=0;i--)

// Running a loop

//Begins the for loop

if (a[i])==1)

// Check for condition

{
break;
}
}

// End of the for loop

for(j=i-1;j>=0;j--)

// Running a loop

{
if (a[j] == 1)

// Check for condition

{
a[j]=0;
}

Department of Computer Science & Engg

// Condition is satisfied then assign 0 to a[j]

RAGHU INSTITUTE OF TECHNOLOGY

else
{
a[j]=1;

// Condition is not satisfied then assign 1 to a[j]

}
}
printf(The 2s complement is :); // Displays the 2s complement
for(i=0;i<n;i++)

// Running a loop

printf(%d,a[i]);

// Displays the a[i] value

return(0);

// It returns the value 0

// Main Program Ends

PROCEDURE FOR EXECUTING THE PROGRAM:


Step 1: After typing the program, press ESC button+shift+: and then type wq(to save the program and
quit)
Step 2: Now compile the program by using the following command
cc wk2a.c lcurses
Step 3: Now go for running the program by using the command
./a.out

Step 4: To create an executing file use the command


cc wk2a.c -curses o twoscomplement

EXPECTED I/P AND O/P:


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
Department of Computer Science & Engg

0
The 2s complement is 000100

RAGHU INSTITUTE OF TECHNOLOGY

The entered binary number is 10000

The 2s complement is 10001

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

VIVA VOCE QUESTIONS:


1) Expand ASCII ?
Ans: American standarad code for information
interchange

2)What is binary number ?


Ans: The number which contains only 0 and 1 is called binary number
.
3) Define 2s complement ?
Ans: The given binary number is first covert the numbers 0 to1 and 1 to 0. And finally add the 1 to the
converted number. Then we will get the 2s complement number.
--xXx--

Department of Computer Science & Engg