Friday, November 28, 2014

C Convert binary pro



C Program to convert the given Binary Number into Decimal

#include 
        #include
void main()
{
int  num, binary_val, decimal_val = 0, base = 1, rem; 
clrscr();
printf("Enter a binary number(1s and 0s) \n");
scanf("%d", &num); 
binary_val = num;
while (num > 0)
{
rem = num % 10;
decimal_val = decimal_val + rem * base;
num = num / 10 ;
base = base * 2;
}
printf("The Binary number is = %d \n", binary_val);
printf("Its decimal equivalent is = %d \n", decimal_val);
getch();
}

Output
 
Enter a binary number(1s and 0s)
10101001
The Binary number is = 10101001
Its decimal equivalent is = 169S

C Program to Reverse a Given Number

#include 
    #include
void main()
{
long  num, reverse = 0, temp, remainder;
clrscr();
printf("Enter the number\n");
scanf("%ld", &num);
temp = num;
while (num > 0)
{
remainder = num % 10;
reverse = reverse * 10 + remainder;
num /= 10;
}
printf("Given number = %ld\n", temp);
printf("Its reverse is = %ld\n", reverse);
getch();
}

Output
 
Enter the number
567865
Given number   = 567865
Its reverse is = 568765

C Program to illustrate how User Authentication is done

#include 
#include
void main()
{
   char password[10], username[10], ch;
   int i;
   clrscr();
   printf("Enter User name: ");
   gets(username);
   printf("Enter the password < any 8 characters>: ");
   for (i = 0; i < 8; i++)
   {
            ch = getchar();
            password[i] = ch;
            ch = '*' ;
            printf("%c", ch);
   }
        password[i] = '\0';
   printf("\n Your password is :");
   for (i = 0; i < 8; i++)
   {
            printf("%c", password[i]);
   }
}
Output
 
Enter User name: rajaraman
Enter the password <any 8 characters>: shashi12
********
Your password is :shashi12

C Program to Convert a Decimal Number to Binary & Count the Number of 1s

#include 
    #include
void main()
{
long num, decimal_num, remainder, base = 1, binary = 0, no_of_1s = 0;
   clrscr();
printf("Enter a decimal integer \n");
scanf("%ld", &num);
decimal_num = num;
while (num > 0)
{
remainder = num % 2;
if (remainder == 1)
{
no_of_1s++;
}
binary = binary + remainder * base;
num = num / 2;
base = base * 10;
}
printf("Input number is = %d\n", decimal_num);
printf("Its binary equivalent is = %ld\n", binary);
printf("No.of 1's in the binary number is = %d\n", no_of_1s);
getch();
}
 
Output
 
Enter a decimal integer
134
Input number is = 134
Its binary equivalent is = 10000110
No.of 1's in the binary number is = 3

No comments:

Post a Comment