Menu driven program for Right Shift & Left Shift

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


Solution:

#include<stdio.h>
#include<stdlib.h>
void showbits(long n)
{
     int r;
     long binary = 0, i = 1,num;
     num=n;
     while(n != 0)
     {
          r = n%2;
          n = n/2;
          binary= binary + (r*i);
          i = i*10;
     }
     printf("Binary of %ld  : %ld", num,binary);
}
int main()
{
     int no,ans,ch,b;
     do
     {
          printf("\tMENU");
          printf("\n--------------------");
          printf("\n 1:Right Shift ");
          printf("\n 2:Left Shift ");
          printf("\n 3:One's Complement ");
          printf("\n 4:Exit");
          printf("\n--------------------");
          printf("\nEnter Your choice: ");
          scanf("%d",&ch);
          switch(ch)
          {
               case 1:
                    printf("\nEnter Number : ");
                    scanf("%d",&no);
                    showbits(no);
                    printf("\nHow Many Bits to Shift Right : ");
                    scanf("%d",&b);
                    ans=no>>b;
                    printf("After Shifting %d Bit to Right, Answer is : %d\n\n",b,ans);
               break;
               case 2:
                    printf("\nEnter Number : ");
                    scanf("%d",&no);
                    showbits(no);
                    printf("\nHow Many Bits to Shift Left : ");
                    scanf("%d",&b);
                    ans=no<<b;
                    printf("After Left Shifting, Answer is : %d\n\n",ans);
               break;
               case 3:
                    printf("\nEnter Number : ");
                    scanf("%d",&no);
                    showbits(no);
                    ans=∼no;
                    printf("\nOne's Compliment=%d\n\n",ans);
               break;
               case 4:
                    exit(0);
               default:
                    printf("\nInvalid Choice\n\n");
          }
     }while(ch!=4);
     return 0;
}


Output:

right shift

left shift

ones complement