Tuesday, 24 March 2015

C program for linear search using function

#include <stdio.h>
 
long linear_search(long [], long, long);
 
int main()
{
   long array[100], search, c, n, position;
 
   printf("Enter number of elements in array\n");
   scanf("%ld", &n);
 
   printf("Enter %d numbers\n", n);
 
   for (c = 0; c < n; c++)
      scanf("%ld", &array[c]);
 
   printf("Enter number to search\n");
   scanf("%ld",&search);
 
   position = linear_search(array, n, search);
 
   if (position == -1)
      printf("%d is not present in array.\n", search);
   else
      printf("%d is present at location %d.\n", search, position+1);
 
   return 0;
} 
 
long linear_search(long a[], long n, long find) {
   long c;
 
   for (c = 0 ;c < n ; c++ ) {
      if (a[c] == find)
         return c;
   }
 
   return -1;
}
 

Output of code:

Linear Search program output for multiple occurrence 

C Program for Linear search

#include <stdio.h>
 
int main()
{
   int array[100], search, c, n;
 
   printf("Enter the number of elements in array\n");
   scanf("%d",&n);
 
   printf("Enter %d integer(s)\n", n);
 
   for (c = 0; c < n; c++)
      scanf("%d", &array[c]);
 
   printf("Enter the number to search\n");
   scanf("%d", &search);
 
   for (c = 0; c < n; c++)
   {
      if (array[c] == search)     /* if required element found */
      {
         printf("%d is present at location %d.\n", search, c+1);
         break;
      }
   }
   if (c == n)
      printf("%d is not present in array.\n", search);
 
   return 0;
}
 

Output of program:

Linear search c program 

C Program of Linear search for multiple occurrences

#include <stdio.h>
 
int main()
{
   int array[100], search, c, n, count = 0;
 
   printf("Enter the number of elements in array\n");
   scanf("%d", &n);
 
   printf("Enter %d numbers\n", n);
 
   for ( c = 0 ; c < n ; c++ )
      scanf("%d", &array[c]);
 
   printf("Enter the number to search\n");
   scanf("%d", &search);
 
   for (c = 0; c < n; c++) {
      if (array[c] == search) {
         printf("%d is present at location %d.\n", search, c+1);
  count++;
      }
   }
   if (count == 0)
      printf("%d is not present in array.\n", search);
   else
      printf("%d is present %d times in array.\n", search, count);
 
   return 0;
}

 

Output of code:

Linear Search program output for multiple occurrence 

Monday, 23 March 2015

C program to find minimum element in array using pointers

#include <stdio.h>
 
int main() 
{
    int array[100], *minimum, size, c, location = 1;
 
    printf("Enter the number of elements in array\n");
    scanf("%d",&size);
 
    printf("Enter %d integers\n", size);
 
    for ( c = 0 ; c < size ; c++ )
        scanf("%d", &array[c]);
 
    minimum = array;
    *minimum = *array;
 
    for ( c = 1 ; c < size ; c++ ) 
    {
        if ( *(array+c) < *minimum ) 
        {
           *minimum = *(array+c);
           location = c+1;
        }
    } 
 
    printf("Minimum element found at location %d and it's value is %d.\n",
            location, *minimum);
    return 0;
}
 

Output of program:

Minimum element in array program 

C program to find minimum element in array using function

#include <stdio.h>
 
int find_minimum(int[], int); 
 
int main() {
  int c, array[100], size, location, minimum;
 
  printf("Enter the number of elements in array\n");
  scanf("%d", &size);
 
  printf("Input %d integers\n", size);
 
  for (c = 0; c < size; c++)
    scanf("%d", &array[c]);
 
  location = find_minimum(array, size);
  minimum  = array[location];
 
  printf("Minimum element location = %d and value = %d.\n", location + 1, minimum);
  return 0;
}
 
int find_minimum(int a[], int n) {
  int c, min, index;
 
  min = a[0];
  index = 0;
 
  for (c = 1; c < n; c++) {
    if (a[c] < min) {
       index = c;
       min = a[c];
    }
  }
 
  return index;
}
 

Output of program:

Minimum element in array program 

C program to find minimum element in array

#include <stdio.h>
 
int main() 
{
    int array[100], minimum, size, c, location = 1;
 
    printf("Enter the number of elements in array\n");
    scanf("%d",&size);
 
    printf("Enter %d integers\n", size);
 
    for ( c = 0 ; c < size ; c++ )
        scanf("%d", &array[c]);
 
    minimum = array[0];
 
    for ( c = 1 ; c < size ; c++ ) 
    {
        if ( array[c] < minimum ) 
        {
           minimum = array[c];
           location = c+1;
        }
    } 
 
    printf("Minimum element is present at location %d and it's value is %d.\n"       ,
            location, minimum);
    return 0;
}
 
Output of program:

Minimum element in array program 

Wednesday, 18 March 2015

C program to find maximum element in array using function

#include <stdio.h>
 
int find_maximum(int[], int); 
 
int main() {
  int c, array[100], size, location, maximum;
 
  printf("Input number of elements in array\n");
  scanf("%d", &size);
 
  printf("Enter %d integers\n", size);
 
  for (c = 0; c < size; c++)
    scanf("%d", &array[c]);
 
  location = find_maximum(array, size);
  maximum  = array[location];
 
  printf("Maximum element location = %d and value = %d.\n", location + 1, maximum);
  return 0;
}
 
int find_maximum(int a[], int n) {
  int c, max, index;
 
  max = a[0];
  index = 0;
 
  for (c = 1; c < n; c++) {
    if (a[c] > max) {
       index = c;
       max = a[c];
    }
  }
 
  return index;
}
 
Output of program: 
Input number of elements in array
5
 
Enter 5 integers
 
8
6
9
3
Maximum element location = 3 and value = 9

C program to find maximum element in array

#include <stdio.h>
 
int main()
{
  int array[100], maximum, size, c, location = 1;
 
  printf("Enter the number of elements in array\n");
  scanf("%d", &size);
 
  printf("Enter %d integers\n", size);
 
  for (c = 0; c < size; c++)
    scanf("%d", &array[c]);
 
  maximum = array[0];
 
  for (c = 1; c < size; c++)
  {
    if (array[c] > maximum)
    {
       maximum  = array[c];
       location = c+1;
    }
  }
 
  printf("Maximum element is present at location %d and it's value is %d.\n", location, maximum);
  return 0;
}
 
Output of program: 
Enter the number of elements in array
5
Enter 5 integers 
4
5
6
8

Maximum element is present at location 4 and it's value is 8

 

C program to add numbers using call by reference

#include <stdio.h>
 
long add(long *, long *);
 
int main()
{
   long first, second, *p, *q, sum;
 
   printf("Input two integers to add\n");
   scanf("%ld%ld", &first, &second);
 
   sum = add(&first, &second);
 
   printf("(%ld) + (%ld) = (%ld)\n", first, second, sum);
 
   return 0;
}
 
long add(long *x, long *y) {
   long sum;
 
   sum = *x + *y;
 
   return sum;
}
 

Output of program:

Input two integers to add
5
6
 
5+6=11 

C program to add numbers using call by reference

#include <stdio.h>
 
long add(long *, long *);
 
int main()
{
   long first, second, *p, *q, sum;
 
   printf("Input two integers to add\n");
   scanf("%ld%ld", &first, &second);
 
   sum = add(&first, &second);
 
   printf("(%ld) + (%ld) = (%ld)\n", first, second, sum);
 
   return 0;
}
 
long add(long *x, long *y) {
   long sum;
 
   sum = *x + *y;
 
   return sum;
}
 
Output of program:
Input two integers to add 
4
5

4+5=9
 
 

C program to add two numbers using pointers

#include <stdio.h>
 
int main()
{
   int first, second, *p, *q, sum;
 
   printf("Enter two integers to add\n");
   scanf("%d%d", &first, &second);
 
   p = &first;
   q = &second;
 
   sum = *p + *q;
 
   printf("Sum of entered numbers = %d\n",sum);
 
   return 0;
}
 
Output of program:
 
Enter two integers to add 
2
5
 Sum of entered numbers =
7 

C program to print Pascal triangle

Pascal Triangle in c: 

C program to print Pascal triangle which you might have studied in Binomial Theorem in Mathematics. Number of rows of Pascal triangle to print is entered by the user. First four rows of Pascal triangle are shown below :-
   1
  1 1
 1 2 1
1 3 3 1 
 

Program Code

 
#include <stdio.h>
 
long factorial(int);
 
int main()
{
   int i, n, c;
 
   printf("Enter the number of rows you wish to see in pascal triangle\n");
   scanf("%d",&n);
 
   for (i = 0; i < n; i++)
   {
      for (c = 0; c <= (n - i - 2); c++)
         printf(" ");
 
      for (c = 0 ; c <= i; c++)
         printf("%ld ",factorial(i)/(factorial(c)*factorial(i-c)));
 
      printf("\n");
   }
 
   return 0;
}
 
long factorial(int n)
{
   int c;
   long result = 1;
 
   for (c = 1; c <= n; c++)
         result = result*c;
 
   return result;
}
 
 

Output 

Enter the number of rows you wish to see in pascal triangle 
4
 
   1
  1 1
 1 2 1
1 3 3 1 

C program to print Floyd's triangle using recursion

#include <stdio.h>
 
void print_floyd(int);
 
int main() 
{
  int n, i,  c, a = 1;
 
  printf("Input number of rows of Floyd's triangle to print\n");
  scanf("%d", &n);
 
  print_floyd(n);
 
  return 0;
}
 
void print_floyd(int n) {
   static int row = 1, c = 1;
   int d;
 
   if (n <= 0)
      return;
 
   for (d = 1; d <= row; ++d)
      printf("%d ", c++);
 
   printf("\n");
   row++;
 
   print_floyd(--n);   
}
 
 Output
 Input number of rows of Floyd's triangle to print
4
 
1

2 3

4 5 6

7 8 9 10 
 
 
 

C program to print Floyd's triangle

C program to print Floyd's triangle:- This program prints Floyd's triangle. Number of rows of Floyd's triangle to print is entered by the user. First four rows of Floyd's triangle are as follows :-
1
2 3
4 5 6
7 8 9 10
It's clear that in Floyd's triangle nth row contains n numbers.

Program Code

#include <stdio.h>
 
int main()
{
  int n, i,  c, a = 1;
 
  printf("Enter the number of rows of Floyd's triangle to print\n");
  scanf("%d", &n);
 
  for (i = 1; i <= n; i++)
  {
    for (c = 1; c <= i; c++)
    {
      printf("%d ",a);
      a++;
    }
    printf("\n");
  }
 
  return 0;
}

C program to Fibonacci series program using recursion

#include<stdio.h>
 
int Fibonacci(int);
 
main()
{
   int n, i = 0, c;
 
   scanf("%d",&n);
 
   printf("Fibonacci series\n");
 
   for ( c = 1 ; c <= n ; c++ )
   {
      printf("%d\n", Fibonacci(i));
      i++; 
   }
 
   return 0;
}
 
int Fibonacci(int n)
{
   if ( n == 0 )
      return 0;
   else if ( n == 1 )
      return 1;
   else
      return ( Fibonacci(n-1) + Fibonacci(n-2) );
} 
 
Output Of Program:
5 
Fibonacci series 
0
1
1
2
3

C program to Fibonacci series using for loop

/* Fibonacci Series c language */
#include<stdio.h>
 
int main()
{
   int n, first = 0, second = 1, next, c;
 
   printf("Enter the number of terms\n");
   scanf("%d",&n);
 
   printf("First %d terms of Fibonacci series are :-\n",n);
 
   for ( c = 0 ; c < n ; c++ )
   {
      if ( c <= 1 )
         next = c;
      else
      {
         next = first + second;
         first = second;
         second = next;
      }
      printf("%d\n",next);
   }
 
   return 0;
}
 
Output 
Enter the number of terms
5 
First %d terms of Fibonacci series are :-
0
1123 

C program to generate and print armstrong numbers

#include <stdio.h>
 
int check_armstrong(int);
int power(int, int);
 
int main () {
   int c, a, b;
 
   printf("Input two integers\n");
   scanf("%d%d", &a, &b);
 
   for (c = a; c <= b; c++) {
      if (check_armstrong(c) == 1)
         printf("%d\n", c);
   }
 
   return 0;
}
 
int check_armstrong(int n) {
   long long sum = 0, temp;
   int remainder, digits = 0;
 
   temp = n;
 
   while (temp != 0) {
      digits++;
      temp = temp/10;
   }
 
   temp = n;
 
   while (temp != 0) {
      remainder = temp%10;
      sum = sum + power(remainder, digits);
      temp = temp/10;
   }
 
   if (n == sum)
      return 1;
   else
      return 0;
}
 
int  power(int n, int r) {
   int c, p = 1;
 
   for (c = 1; c <= r; c++) 
      p = p*n;
 
   return p;   
}
 
Output:
 
Input two integers
100 400
 
153
370
371  
 
 

C program to check Armstrong number using function

#include <stdio.h>
 
int check_armstrong(long long);
long long power(int, int);
 
int main () {
   long long n;
 
   printf("Input a number\n");
   scanf("%lld", &n);
 
   if (check_armstrong(n) == 1)
      printf("%lld is an armstrong number.\n", n); 
   else
      printf("%lld is not an armstrong number.\n", n);   
 
   return 0;
}
 
int check_armstrong(long long n) {
   long long sum = 0, temp;
   int remainder, digits = 0;
 
   temp = n;
 
   while (temp != 0) {
      digits++;
      temp = temp/10;
   }
 
   temp = n;
 
   while (temp != 0) {
      remainder = temp%10;
      sum = sum + power(remainder, digits);
      temp = temp/10;
   }
 
   if (n == sum)
      return 1;
   else
      return 0;
}
 
long long power(int n, int r) {
   int c;
   long long p = 1;
 
   for (c = 1; c <= r; c++) 
      p = p*n;
 
   return p;   
}
 
Output of program:

Input a number
35641594208964132
35641594208964132 is an Armstrong number.

C program for prime number using function

#include<stdio.h>
 
int check_prime(int);
 
main()
{
   int n, result;
 
   printf("Enter an integer to check whether it is prime or not.\n");
   scanf("%d",&n);
 
   result = check_prime(n);
 
   if ( result == 1 )
      printf("%d is prime.\n", n);
   else
      printf("%d is not prime.\n", n);
 
   return 0;
}
 
int check_prime(int a)
{
   int c;
 
   for ( c = 2 ; c <= a - 1 ; c++ )
   { 
      if ( a%c == 0 )
	 return 0;
   }
   if ( c == a )
      return 1;
}
 
 
Output
 
Enter an integer to check whether it is prime or not.
 5
5 is prime. 
 
 
 

C program to print diamond pattern using recursion

Print Diamond shape is as follows Using Recursion
  *
 ***
*****
 ***
  *
 
 
Program Code
 
#include <stdio.h>
 
void print (int);
 
int main () {
   int rows;
 
   scanf("%d", &rows);
 
   print(rows);
 
   return 0;
}
 
void print (int r) {
   int c, space;
   static int stars = -1;
 
   if (r <= 0)
     return;
 
   space = r - 1;
   stars += 2;
 
   for (c = 0; c < space; c++)
      printf(" ");
 
   for (c = 0; c < stars; c++)
      printf("*"); 
 
   printf("\n");
 
   print(--r);
 
   space = r + 1; 
   stars -= 2;
 
   for (c = 0; c < space; c++)
      printf(" ");
 
   for (c = 0; c < stars; c++)
      printf("*");
 
   printf("\n");
}
 

C program to print diamond pattern

Print Diamond shape is as follows:

  *
 ***
*****
 ***
  *
 
 
Program
 
#include <stdio.h>
 
int main()
{
  int n, c, k, space = 1;
 
  printf("Enter number of rows\n");
  scanf("%d", &n);
 
  space = n - 1;
 
  for (k = 1; k <= n; k++)
  {
    for (c = 1; c <= space; c++)
      printf(" ");
 
    space--;
 
    for (c = 1; c <= 2*k-1; c++)
      printf("*");
 
    printf("\n");
  }
 
  space = 1;
 
  for (k = 1; k <= n - 1; k++)
  {
    for (c = 1; c <= space; c++)
      printf(" ");
 
    space++;
 
    for (c = 1 ; c <= 2*(n-k)-1; c++)
      printf("*");
 
    printf("\n");
  }
 
  return 0;
}
 

C pattern programs

Pattern:
        1
       232
      34543
     4567654
    567898765
 
 
#include<stdio.h>
 
main()
{
      int n, c, d, num = 1, space;
 
      scanf("%d",&n);
 
      space = n - 1;
 
      for ( d = 1 ; d <= n ; d++ )
      {
          num = d;
 
          for ( c = 1 ; c <= space ; c++ )
              printf(" ");
 
          space--;
 
          for ( c = 1 ; c <= d ; c++ )
          {
              printf("%d", num);
              num++;
          }
          num--;
          num--;
          for ( c = 1 ; c < d ; c++)
          {
              printf("%d", num);
              num--;
          }
          printf("\n");
 
      }
 
      return 0;
}
 

C pattern programs

Pattern:
   
   *
  *A*
 *A*A*
*A*A*A* 
 
#include<stdio.h>
 
main()
{
    int n, c, k, space, count = 1;
 
    printf("Enter number of rows\n");
    scanf("%d",&n);
 
    space = n;
 
    for ( c = 1 ; c <= n ; c++)
    {
        for( k = 1 ; k < space ; k++)
           printf(" ");
 
        for ( k = 1 ; k <= c ; k++)
        {
            printf("*");
 
            if ( c > 1 && count < c)
            {
                 printf("A");    
                 count++; 
            }      
        }    
 
        printf("\n");
        space--;
        count = 1;
    }
    return 0;
}
 

C program to print patterns of stars

Print Following Star pattern
*
**
***
****
*****

#include <stdio.h>
 
int main()
{
    int n, c, k;
 
    printf("Enter number of rows\n");
    scanf("%d",&n);
 
    for ( c = 1 ; c <= n ; c++ )
    {
        for( k = 1 ; k <= c ; k++ )
            printf("*");
 
        printf("\n");
    }
 
    return 0;
}


Output of Program :

*
**
***
****
*****

Tuesday, 17 March 2015

C program to print patterns of stars

 // Print the Following Pattern
    *
   ***
  *****
 *******
*********
 
#include <stdio.h>
 
int main()
{
   int row, c, n, temp;
 
   printf("Enter the number of rows in pyramid of stars you wish to see ");
   scanf("%d",&n);
 
   temp = n;
 
   for ( row = 1 ; row <= n ; row++ )
   {
      for ( c = 1 ; c < temp ; c++ )
         printf(" ");
 
      temp--;
 
      for ( c = 1 ; c <= 2*row - 1 ; c++ )
         printf("*");
 
      printf("\n");
   }
 
   return 0;
}
 
 Output of Program
 
    *
   ***
  *****
 *******
********* 
 
 
 
 

C program to reverse number using recursion

#include <stdio.h>
 
long reverse(long); 
 
int main()
{
   long n, r;
 
   scanf("%ld", &n);
 
   r = reverse(n);
 
   printf("After reverse the number %ld\n", r);
 
   return 0;
}
 
long reverse(long n) {
   static long r = 0;
 
   if (n == 0) 
      return 0;
 
   r = r * 10;
   r = r + n % 10;
   reverse(n/10);
   return r;
} 
 
 
Output Of Program:
1234

After reverse the number 4321
 

C program to reverse a number

#include <stdio.h>
 
int main()
{
   int n, reverse = 0;
 
   printf("Enter a number to reverse\n");
   scanf("%d", &n);
 
   while (n != 0)
   {
      reverse = reverse * 10;
      reverse = reverse + n%10;
      n       = n/10;
   }
 
   printf("Reverse of entered number is = %d\n", reverse);
 
   return 0;
}
 
Output Of Program:
 
Enter a number to reverse 
1234 
Reverse of entered number is = 4321 

C program to check whether input alphabet is a vowel or not using switch statement

#include <stdio.h>
 
int main()
{
  char ch;
 
  printf("Input a character\n");
  scanf("%c", &ch);
 
  switch(ch)
  {
    case 'a':
    case 'A':
    case 'e':
    case 'E':
    case 'i':
    case 'I':
    case 'o':
    case 'O':
    case 'u':
    case 'U':
      printf("%c is a vowel.\n", ch);
      break;
    default:
      printf("%c is not a vowel.\n", ch);
  }              
 
  return 0;
}
 
 
Output of Program:
 
Input a character
e 
e is a vowel.

C program to check whether input alphabet is a vowel or not

#include <stdio.h>
 
int main()
{
  char ch;
 
  printf("Enter a character\n");
  scanf("%c", &ch);
 
  if (ch == 'a' || ch == 'A' || ch == 'e' || ch == 'E' || ch == 'i' || ch == 'I' || ch =='o' || ch=='O' || ch == 'u' || ch == 'U')
    printf("%c is a vowel.\n", ch);
  else
    printf("%c is not a vowel.\n", ch);
 
  return 0;
}
 
Output Of program:
 
Enter a character 

a

a is a vowel.

C Program to swap using bitwise XOR

#include <stdio.h>
 
int main()
{
  int x, y;
 
  scanf("%d%d", &x, &y);
 
  printf("Before Swapping x = %d\ny = %d\n", x, y);
 
  x = x ^ y;
  y = x ^ y;
  x = x ^ y;
 
  printf("After Swqpping x = %d\ny = %d\n", x, y);
 
  return 0;
}
 
Output of Program

Before Swapping x=10
y=50

After Swapping x=50
y=10

C Program to Swapping numbers using call by reference

#include <stdio.h>
 
void swap(int*, int*);
 
int main()
{
   int x, y;
 
   printf("Enter the value of x and y\n");
   scanf("%d%d",&x,&y);
 
   printf("Before Swapping\nx = %d\ny = %d\n", x, y);
 
   swap(&x, &y); 
 
   printf("After Swapping\nx = %d\ny = %d\n", x, y);
 
   return 0;
}
 
void swap(int *a, int *b)
{
   int temp;
 
   temp = *b;
   *b   = *a;
   *a   = temp;   
}
 
 Output Of program
 
Enter the value of x and y
4
5
 
Before Swapping
x=4
y=5
 
After Swapping
x=5
y=4 

C Program to Swap two numbers using pointers

#include <stdio.h>
 
int main()
{
   int x, y, *a, *b, temp;
 
   printf("Enter the value of x and y\n");
   scanf("%d%d", &x, &y);
 
   printf("Before Swapping\nx = %d\ny = %d\n", x, y);
 
   a = &x;
   b = &y;
 
   temp = *b;
   *b   = *a;
   *a   = temp;
 
   printf("After Swapping\nx = %d\ny = %d\n", x, y);
 
   return 0;
}
 
 
 
Output of Program:
 
Enter the value of x and y
10
20
Before Swapping
x=10
y=20
After Swapping
x=20
y=10 

C program to Swapping of two numbers without using third variable

#include <stdio.h>
 
int main()
{
   int a, b;
 
   printf("Enter two integers to swap\n");
   scanf("%d%d", &a, &b);
 
   a = a + b;
   b = a - b;
   a = a - b;
 
   printf("a = %d\nb = %d\n",a,b);
   return 0;
}
 
Output Of Program: 

Enter two integers to swap
10
20 

a=20
b=10

C program to swap two numbers

#include <stdio.h>
 
int main()
{
   int x, y, temp;
 
   printf("Enter the value of x and y\n");
   scanf("%d%d", &x, &y);
 
   printf("Before Swapping\nx = %d\ny = %d\n",x,y);
 
   temp = x;
   x    = y;
   y    = temp;
 
   printf("After Swapping\nx = %d\ny = %d\n",x,y);
 
   return 0;
}
 
 
Output of program:


Swap numbers c program 

C program to store decimal to binary conversion in a string

#include <stdio.h>
#include <stdlib.h>
 
char *decimal_to_binary(int);
 
main()
{
   int n, c, k;
   char *pointer;
 
   printf("Enter an integer in decimal number system\n");
   scanf("%d",&n);
 
   pointer = decimal_to_binary(n);
   printf("Binary string of %d is: %s\n", n, t);
 
   free(pointer);
 
   return 0;
}
 
char *decimal_to_binary(int n)
{
   int c, d, count;
   char *pointer;
 
   count = 0;
   pointer = (char*)malloc(32+1);
 
   if ( pointer == NULL )
      exit(EXIT_FAILURE);
 
   for ( c = 31 ; c >= 0 ; c-- )
   {
      d = n >> c;
 
      if ( d & 1 )
         *(pointer+count) = 1 + '0';
      else
         *(pointer+count) = 0 + '0';
 
      count++;
   }
   *(pointer+count) = '\0';
 
   return  pointer;
}

Conversion Decimal to binary in C program

#include <stdio.h>
 
int main()
{
  int n, c, k;
 
  printf("Enter an integer in decimal number system\n");
  scanf("%d", &n);
 
  printf("%d in binary number system is:\n", n);
 
  for (c = 31; c >= 0; c--)
  {
    k = n >> c;
 
    if (k & 1)
      printf("1");
    else
      printf("0");
  }
 
  printf("\n");
 
  return 0;
}
 
Output of program:


Enter an integer in decimal number system
100
100 
in binary number system is: 
000000000000000000001100100 
 
 

convert the decimal to binary numbers using bitwise operator in C programming

// bitwise operators
#include <stdio.h>

// function prototype
void DisplayBits(unsigned);

int main(void)
{
unsigned p;

// prompt user for input
printf("Enter an unsigned integer: ");

// read and store the data
// scanf("%u", &p);
scanf_s("%u", &p, 1);

// function call
DisplayBits(p);

return 0;
}

// function definition
void DisplayBits(unsigned number)
{
unsigned q;

// 2 byte, 16 bits position operated bit by bit and hide/mask other bits
// using left shift operator, start with 10000000 00000000
unsigned DisplayMask = 1<<15;
printf("%7u = ", number);

for(q = 1; q < 16; q++)
{
// combining variable number with variable DisplayMask
putchar(number & DisplayMask ? '1':'0');
// number variable is left shifted one bit
number<<= 1;
// separate by 8 bits position (1 byte)
if(q % 8 == 0)
putchar(' ');
}
putchar('\n');
}


Output example:

Enter an unsigned integer: 200
200 = 00000000 1100100
Press any key to continue . . .

Program to Print All ASCII Values in C Programming

//Program to Print All ASCII Values in C Programming

#include<stdio.h>
void main() {
   int i = 0;
   char ch;
   for (i = 0; i < 256; i++) {
      printf("%c ", ch);
      ch = ch + 1;
   }
}

Output :

Å É æ Æ ô ö ò û ù ÿ Ö Ü ¢ £ ¥ ₧ ƒ á í ó ú ñ Ñ ª º ¿ ⌐ ¬ ½ ¼ ¡ « » ░ ▒ ▓ │ ┤ ╡ ╢
╖ ╕ ╣ ║ ╗ ╝ ╜ ╛ ┐ └ ┴ ┬ ├ ─ ┼ ╞ ╟ ╚ ╔ ╩ ╦ ╠ ═ ╬ ╧ ╨ ╤ ╥ ╙ ╘ ╒ ╓ ╫ ╪ ┘ ┌ █ ▄ ▌ ▐
▀ α ß Γ π Σ σ µ τ Φ Θ Ω δ ∞ φ ε ∩ ≡ ± ≥ ≤ ⌠ ⌡ ÷ ≈ ° ∙ · √ ⁿ ² ■     ☺ ☻ ♥ ♦ ♣ ♠
♫ ☼ ► ◄ ↕ ‼ ¶ § ▬ ↨ ↑ ↓ ← ∟ ↔ ▲ ▼ ! " # $ % &amp; ' ( ) * + , - . / 0 1 2 3 4 5 6
7 8 9 : ; &lt; = &gt; ? @ A B C D E F G H I J K L M N O P Q R S T U V W X Y Z [ ] ^
_ ` a b c d e f g h i j k l m n o p q r s t u v w x y z { | } ~ ⌂ Ç ü é â ä à å
ç ê ë è ï î ì Ä

C Program to find greatest in 3 numbers

C Program to find greatest in 3 numbers

#include<stdio.h>
int main() {
   int a, b, c;
   printf("\nEnter value of a, b & c : ");
   scanf("%d %d %d", &a, &b, &c);
   if ((a > b) && (a > c))
      printf("\na is greatest");
   if ((b > c) && (b > a))
      printf("\nb is greatest");
   if ((c > a) && (c > b))
      printf("\nc is greatest");
   return(0);
}

Output :
Enter value for a,b & c : 15 17 21
c is greatest

C Program to find exponent Power Series

//C Program to find exponent Power Series

#include<stdio.h>
#define ACCURACY 0.0001
 int main() {
   int n, count;
   float x, term, sum;
   printf("\nEnter value of x :");
   scanf("%f", &x);
    n = term = sum = count = 1;
    while (n <= 100) {
      term = term * x / n;
      sum = sum + term;
      count = count + 1;
       if (term < ACCURACY)
         n = 999;
      else
         n = n + 1;
   }
    printf("\nTerms = %d Sum = %f", count, sum);
   return 0;
}
 
Output :
Enter value of x:0
Terms = 2 Sum = 1.000000
Enter value of x:0.1
Terms = 5 Sum = 1.105171
Enter value of x:0.5
Terms = 7 Sum = 1.648720
Enter value of x:0.75
Terms = 8 Sum = 2.116997
Enter value of x:0.99
Terms = 9 Sum = 2.691232
Enter value of x:1
Terms = 9 Sum = 2.718279
 
Explanation
A program to evaluate the power series
           x2      x3            xn
ex  =  1 + x + ---  +  --- + ..... + ---- , 0 &lt; x &lt; 1
                2!      3!            n!
It uses if……else to test the accuracy.
The power series contains the recurrence relationship of the type
        Tn   =  Tn-1  (---)   for n &gt; 1
         T1   =  x             for n = 1
         T0   =  1
If Tn-1 (usually known as previous term) is known, then Tn (known as present term) can be easily found by
multiplying the previous term by x/n. Then
  ex   =  T0 +  T1  +  T2 + ...... +  Tn  =  sum

C Program to calculate sum of 5 subjects and find percentage

#include<stdio.h>
int main()
 {
   int s1, s2, s3, s4, s5, sum, total = 500;
   float per;
   printf("\nEnter marks of 5 subjects : ");
   scanf("%d %d %d %d %d", &s1, &s2, &s3, &s4, &s5);
   sum = s1 + s2 + s3 + s4 + s5;
   printf("\nSum : %d", sum);
   per = (sum * 100) / total;
   printf("\nPercentage : %f", per);
   return (0);
}

Output :
Enter marks of 5 subjects : 80 70 90 80 80
Sum : 400
Percentage : 80.00

C Program to calculate gross salary of a person.

#include<stdio.h>
 
int main() {
   int gross_salary, basic, da, ta;
 
   printf("Enter basic salary : ");
   scanf("%d", &basic);
 
   da = (10 * basic) / 100;
   ta = (12 * basic) / 100;
 
   gross_salary = basic + da + ta;
 
   printf("\nGross salary : %d", gross_salary);
   return (0);
}
Output :
 Enter basic Salary : 1000
Gross Salart : 1220