Write a menu driven program in ‘C’ to accept a number from user and perform following operations on it i) Right Shift ii) Left Shift iii) One’s complement
#includevoid showbits(int); void main() { int num,ch,shifts,temp; clrscr(); do { printf("\nPRESS 1 FOR RIGHT SHIFT."); printf("\nPRESS 2 FOR LEFT SHIFT."); printf("\nPRESS 3 FOR ONE'S COMPLEMENT."); printf("\nPRESS 4 FOR EXIT."); printf("\nENTER YOUR CHOICE ::>"); scanf("%d",&ch); switch(ch) { case 1: printf("ENTER ANY DECIMAL NUMBER::> "); scanf("%d",&num); printf("DECIMAL %d IS SAME AS BINARY ",num); showbits(num); printf("\nHOW MANY SHIFT YOU WANT ::> "); scanf("%d",&shifts); temp = num >> shifts; printf("AFTER %d RIGHT SHIFT %d GIVES", shifts,num); showbits(temp); break; case 2: printf("ENTER ANY DECIMAL NUMBER::> "); scanf("%d",&num); printf("DECIMAL %d IS SAME AS BINARY ",num); showbits(num); printf("\nHOW MANY SHIFT YOU WANT ::> "); scanf("%d",&shifts); temp = num << shifts; printf("AFTER %d LEFT SHIFT %d GIVES ",shifts,num); showbits(temp); break; case 3: printf("ENTER ANY DECIMAL NUMBER::> "); scanf("%d",&num); printf("DECIMAL %d IS SAME AS BINARY ",num); showbits(num); temp = ~num; printf("\nONE'S COMPLEMENT OF %d IS ",num); showbits(temp); break; case 4: exit(0); } }while(ch != 4); getch(); } void showbits( int num) { int i,mask,temp; for( i = 15 ; i >= 0 ; i--) { mask = 1 << i; temp = num & mask; temp == 0 ? printf("0"):printf("1"); } } /*OUTPUT:: PRESS 1 FOR RIGHT SHIFT. PRESS 2 FOR LEFT SHIFT. PRESS 3 FOR ONE'S COMPLEMENT. PRESS 4 FOR EXIT. ENTER YOUR CHOICE ::>1 ENTER ANY DECIMAL NUMBER::> 10 DECIMAL 10 IS SAME AS BINARY 0000000000001010 HOW MANY SHIFT YOU WANT ::> 2 AFTER 2 RIGHT SHIFT 10 GIVES 0000000000000010 PRESS 1 FOR RIGHT SHIFT. PRESS 2 FOR LEFT SHIFT. PRESS 3 FOR ONE'S COMPLEMENT. PRESS 4 FOR EXIT. ENTER YOUR CHOICE ::>2 ENTER ANY DECIMAL NUMBER::> 10 DECIMAL 10 IS SAME AS BINARY 0000000000001010 HOW MANY SHIFT YOU WANT ::> 2 AFTER 2 LEFT SHIFT 10 GIVES 0000000000101000 PRESS 1 FOR RIGHT SHIFT. PRESS 2 FOR LEFT SHIFT. PRESS 3 FOR ONE'S COMPLEMENT. PRESS 4 FOR EXIT. ENTER YOUR CHOICE ::>3 ENTER ANY DECIMAL NUMBER::> 10 DECIMAL 10 IS SAME AS BINARY 0000000000001010 ONE'S COMPLEMENT OF 10 IS 1111111111110101 PRESS 1 FOR RIGHT SHIFT. PRESS 2 FOR LEFT SHIFT. PRESS 3 FOR ONE'S COMPLEMENT. PRESS 4 FOR EXIT. ENTER YOUR CHOICE ::>4 */
Write a menu driven program in ‘C’ to accept a number from user and perform following operations on it i) Right Shift ii) Left Shift iii) One’s complement
Reviewed by
on
April 24, 2015
Rating: