Monday, August 4, 2014

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

This C Program converts a decimal number into binary & count the number of 1s. The program uses module operation and multiplication with base 2 operation for conversion. It also uses modulo operation to check for 1′s and accordingly increments the count of 1s.
Here is source code of the C program to convert a decimal number to binary & count the number of 1s. The C program is successfully compiled and run on a Linux system. The program output is also shown below.
  1. /*
  2.  * C program to accept a decimal number and convert it to binary
  3.  * and count the number of 1's in the binary number
  4.  */
  5. #include <stdio.h>
  6.  
  7. void main()
  8. {
  9.     long num, decimal_num, remainder, base = 1, binary = 0, no_of_1s = 0;
  10.  
  11.     printf("Enter a decimal integer \n");
  12.     scanf("%ld", &num);
  13.     decimal_num = num;
  14.     while (num > 0)
  15.     {
  16.         remainder = num % 2;
  17.         /*  To count no.of 1s */
  18.         if (remainder == 1)
  19.         {
  20.             no_of_1s++;
  21.         }
  22.         binary = binary + remainder * base;
  23.         num = num / 2;
  24.         base = base * 10;
  25.     }
  26.     printf("Input number is = %d\n", decimal_num);
  27.     printf("Its binary equivalent is = %ld\n", binary);
  28.     printf("No.of 1's in the binary number is = %d\n", no_of_1s);
  29. }

$ cc pgm46.c
$ a.out
Enter a decimal integer
134
Input number is = 134
Its binary equivalent is = 10000110
No.of 1's in the binary number is = 3

C Program to Illustrate how User Authentication is Done

This C Program illustrate how user authentication is done. The program accepts the username and password. It checks whether the password is correct with respect to the username.
Here is source code of the C program to illustrate user authentication. The C program is successfully compiled and run on a Linux system. The program output is also shown below.
  1. /*
  2.  * C program is to illustrate how user authentication is done.
  3.  * Program asks for the user name and password and displays
  4.  * the password as '*' character
  5.  */
  6. #include <stdio.h>
  7.  
  8. void main()
  9. {
  10.  char password[10], username[10], ch;
  11.  int i;
  12.  
  13.  printf("Enter User name: ");
  14.  gets(username);
  15.  printf("Enter the password < any 8 characters>: ");
  16.  for (i = 0; i < 8; i++)
  17.  {
  18.             ch = getchar();
  19.             password[i] = ch;
  20.             ch = '*' ;
  21.             printf("%c", ch);
  22.  }
  23.         password[i] = '\0';
  24.  /*  Original password can be printed, if needed */
  25.  printf("\n Your password is :");
  26.  for (i = 0; i < 8; i++)
  27.  {
  28.             printf("%c", password[i]);
  29.  }
  30. }

$ cc pgm43.c
$ a.out
Enter User name: rajaraman
Enter the password <any 8 characters>: shashi12
********
Your password is :shashi12

C Program to Reverse a Given Number

This C Program reverses a given number by using modulo operation.
Here is source code of the C program to reverse a given number. The C program is successfully compiled and run on a Linux system. The program output is also shown below.
  1. /*
  2.  * C program to accept an integer and reverse it
  3.  */
  4. #include <stdio.h>
  5.  
  6. void main()
  7. {
  8.     long  num, reverse = 0, temp, remainder;
  9.  
  10.     printf("Enter the number\n");
  11.     scanf("%ld", &num);
  12.     temp = num;
  13.     while (num > 0)
  14.     {
  15.         remainder = num % 10;
  16.         reverse = reverse * 10 + remainder;
  17.         num /= 10;
  18.     }
  19.     printf("Given number = %ld\n", temp);
  20.     printf("Its reverse is = %ld\n", reverse);
  21. }

$ cc pgm42.c
$ a.out
Enter the number
567865
Given number   = 567865
Its reverse is = 568765

C Program to Convert the given Binary Number into Decimal

This C Program converts the given binary number into decimal. The program reads the binary number, does a modulo operation to get the remainder, multiples the total by base 2 and adds the modulo and repeats the steps.
Here is source code of the C program to covert binary number to decimal. The C program is successfully compiled and run on a Linux system. The program output is also shown below.
  1. /*
  2.  * C program to convert the given binary number into decimal
  3.  */
  4. #include <stdio.h>
  5.  
  6. void main()
  7. {
  8.     int  num, binary_val, decimal_val = 0, base = 1, rem;
  9.  
  10.     printf("Enter a binary number(1s and 0s) \n");
  11.     scanf("%d", &num); /* maximum five digits */
  12.     binary_val = num;
  13.     while (num > 0)
  14.     {
  15.         rem = num % 10;
  16.         decimal_val = decimal_val + rem * base;
  17.         num = num / 10 ;
  18.         base = base * 2;
  19.     }
  20.     printf("The Binary number is = %d \n", binary_val);
  21.     printf("Its decimal equivalent is = %d \n", decimal_val);
  22. }

$ cc pgm38.c
$ a.out
Enter a binary number(1s and 0s)
10101001
The Binary number is = 10101001
Its decimal equivalent is = 169S

C Program to Read Two Integers M and N & Swap their Values

This C Program reads two integers & swap their values.
Here is source code of the C program to read two integers & swap their values. The C program is successfully compiled and run on a Linux system. The program output is also shown below.
  1. /*
  2.  * C program to read two integers M and N and to swap their values.
  3.  * Use a user-defined function for swapping. Output the values of M
  4.  * and N before and after swapping.
  5.  */
  6. #include <stdio.h>
  7. void swap(float *ptr1, float  *ptr2);
  8.  
  9. void main()
  10. {
  11.     float m, n;
  12.  
  13.     printf("Enter the values of M and N \n");
  14.     scanf("%f %f", &m, &n);
  15.     printf("Before Swapping:M = %5.2ftN = %5.2f\n", m, n);
  16.     swap(&m, &n);
  17.     printf("After Swapping:M  = %5.2ftN = %5.2f\n", m, n);
  18. }
  19. /*  Function swap - to interchanges the contents of two items */
  20. void swap(float *ptr1, float *ptr2)
  21. {
  22.     float temp;
  23.  
  24.     temp = *ptr1;
  25.     *ptr1 = *ptr2;
  26.     *ptr2 = temp;
  27. }

$ cc pgm36.c
$ a.out
Enter the values of M and N
2 3
Before Swapping:M =  2.00    N =  3.00
After Swapping:M  =  3.00    N =  2.00

C Program to Find the Number of Integers Divisible by 5

This C Program calculates the number of integers divisible by 5. This program checks if the given number is divisible by 5 and then prints an appropriate message.
Here is source code of the C program to calculate the number of integers divisible by 5. The C program is successfully compiled and run on a Linux system. The program output is also shown below.
  1. /*
  2.  * C program to find the number of integers divisible by 
  3.  * 5 between the given range num1 and num2, where num1 < num2.
  4.  *
  5.  * Also find the sum of all these integer numbers which are divisible
  6.  * by 5 and display the total.
  7.  */
  8. #include <stdio.h>
  9.  
  10. void main()
  11. {
  12.     int i, num1, num2, count = 0, sum = 0;
  13.  
  14.     printf("Enter the value of num1 and num2 \n");
  15.     scanf("%d %d", &num1, &num2);
  16.     /* Count the number and compute their sum*/
  17.     printf("Integers divisible by 5 are \n");
  18.     for (i = num1; i < num2; i++)
  19.     {
  20.         if (i % 5 == 0)
  21.         {
  22.             printf("%3d,", i);
  23.             count++;
  24.             sum = sum + i;
  25.         }
  26.     }
  27.     printf("\n Number of integers divisible by 5 between %d and %d =
  28.  %d\n", num1, num2, count);
  29.     printf("Sum of all integers that are divisible by 5 = %d\n", sum);
  30. }

$ cc pgm18.c
$ a.out
Enter the value of num1 and num2
12 17
Integers divisible by 5 are
 15,
Number of integers divisible by 5 between 12 and 17 = 1
Sum of all integers that are divisible by 5 = 15

C Program to Reverse a Number & Check if it is a Palindrome

This C Program reverses a number & checks if it is a palindrome or not. First it reverses a number. Then it checks if given number and reversed numbers are equal. If they are equal, then its a palindrome. Here is source code of the C program to reverse a number & checks it is a palindrome or not. The C program is successfully compiled and run on a Linux system. The program output is also shown below.
  1. /*
  2.  * C program to reverse a given integer number and check
  3.  * whether it is a palindrome. Display the given number
  4.  * with appropriate message
  5.  */
  6. #include <stdio.h>
  7.  
  8. void main()
  9. {
  10.     int num, temp, remainder, reverse = 0;
  11.  
  12.     printf("Enter an integer \n");
  13.     scanf("%d", &num);
  14.     /*  original number is stored at temp */
  15.     temp = num;
  16.     while (num > 0)
  17.     {
  18.         remainder = num % 10;
  19.         reverse = reverse * 10 + remainder;
  20.         num /= 10;
  21.     }
  22.     printf("Given number is = %d\n", temp);
  23.     printf("Its reverse is  = %d\n", reverse);
  24.     if (temp == reverse)
  25.         printf("Number is a palindrome \n");
  26.     else
  27.         printf("Number is not a palindrome \n");
  28. }

$ cc pgm13.c
$ a.out
Enter an integer
6789
Given number is = 6789
Its reverse is  = 9876
Number is not a palindrome
 
$ a.out
Enter an integer
58085
Given number is = 58085
Its reverse is  = 58085
Number i