20_10

Write a C program to sort an array using Insertion sort.

 Source code:

/*Insertion sort is a sorting algorithm in which the elements are transferred one at a time to the right position. Here the first element in the array is considered as sorted, even if it is an unsorted array. Then each element in the array is checked with the previous elements, resulting in a growing sorted output list. With each iteration, the sorting algorithm removes one element at a time and finds the appropriate location within the sorted array and inserts it there. The iteration continues until the whole list is sorted. First an array of size 10 will be taken. We will fill it by reading 10 integers. You need to write the logic of Selection Sort. The final output will be sorted output in Ascending Order.*/


#include <stdio.h>

int main()

{

  int n=10, array[10], i, j, t;


  /* Read Array Elements*/

printf("Enter the array: ");

  for (i = 0; i < n; i++) {

    scanf("%d", &array[i]);

  }

for (i = 1 ; i <= n - 1; i++) {

    j = i;


    while ( j > 0 && array[j-1] > array[j]) {

      t = array[j];

      array[j] = array[j-1];

      array[j-1] = t;

    j--;

    }

}

printf("The sorted output in ascending order:\n");

  for (i = 0; i <= n - 1; i++) {

    printf("%d\n", array[i]);

  }

  return 0;

}

Output:
C program to sort an array using Insertion sort


Write a C program to sort an array using Selection Sort.

Source code:

 /*The Selection Sort works by finding the smallest unsorted item in the list and swapping it with the item in the current position. The algorithm works as follows:

1. Set first position as current position.

2. Find the minimum value in the list

3. Swap it with the value in the current position

4. Set next position as current position

5. Repeat Steps 2-4 until you reach end of list 


First an array of size 10 will be taken. We will fill it by reading 10 integers. The final output will be sorted output in Ascending Order.*/


#include <stdio.h>

int main()

{

    int array[10], n=10, i, j, index, swap;


   /* Read Array Elements */

printf("Enter the array:");

    for ( i = 0 ; i < n ; i++ )

    scanf("%d", &array[i]);

    for ( i = 0 ; i < ( n - 1 ) ; i++ )

    {

    index = i;

    for ( j = i + 1 ; j < n ; j++ )

        {

        if ( array[index] > array[j] )

            index = j;

        }

      if ( index != i )

      {

        swap = array[i];

        array[i] = array[index];

        array[index] = swap;

        }

    }

printf("The sorted output in ascending order:\n");

    for ( i = 0 ; i < n ; i++ )

    printf("%d\n", array[i]);

    return 0;

}

Output:
C program to sort an array using Selection Sort


Write a C program to search a number in an array using Linear search algorithm.

Source code:

 /*A Linear search is a searching algorithm which is used to find whether a given number is present in an array. It is also known as sequential search. It is straightforward and keep on comparing each element with the element to search until it is found or the list ends. First an array of size 10 will be taken. We will fill it by reading 10 integers. Then the key integer to be searched will be given as input.   The test case total number of values are 11. The first 10 values are to fill up the array. If it is found then output would return it and it's position.  */


#include <stdio.h>

int main()

{

    int array[10], search, i, n=10;


  /* Read array */

printf("Enter the array:");

    for (i = 0; i < n; i++)

    scanf("%d", &array[i]);


  /* Read the interger to be searched */

printf("Enter the search element:");

    scanf("%d", &search);

for (i = 0; i < n; i++)

    {

    if (array[i] == search)     /* if required element found */

        {

        printf("%d is found at position %d.", search, i+1);

          break;

      }

    }

    if (i == n)

    printf("%d is not there in the array.", search);

return 0;

}

Output:

C program to search a number in an array using Linear search algorithm




Write a C program to search a number in an array using Binary search algorithm.

 Source code:

/*A Binary search is an efficient algorithm for finding an item from an ordered list of items. It works by repeatedly dividing in half the portion of the list that could contain the item, until it is narrowed down the possible locations to just one. First an array of size 10 will be taken. We will fill it by reading 10 integers. Then the key integer to be searched will be given as input.  */


#include <stdio.h>

int main()

{

int i, n=10, first, last, middle, search, array[10];


   /* Fill Array */

    printf("Enter the array: ");

    for (i = 0; i < n; i++)

    scanf("%d",&array[i]);


   /* Read the value of the integer to be searched */

printf("Enter the number which to be search:");

    scanf("%d", &search);

    first = 0;

    last = n - 1;

    middle = (first+last)/2;


    while (first <= last) {

        if (array[middle] < search)

        first = middle + 1;

        else if (array[middle] == search) {

        printf("%d found at position %d.", search, middle+1);

        break;

      }

      else

          last = middle - 1;


      middle = (first + last)/2;

    }

    if (first > last)

    printf("%d is not there in the list.", search);

return 0;

}

Output:



Write C code to check the number is a Perfect Number. A function isPerfect is used here.

 Source code:

/*A perfect number is a positive integer which is equal to the sum of it's positive divisors excluding the number itself e.g 6 is first perfect number because it's proper divisors are 1,2 and 3 so sum 1+2+3=6.  Using a function, write C code to check the number is a Perfect Number. A function isPerfect is used here.  */


#include <stdio.h>

int isPerfect(int num);

int isPerfect(int num)

{

    int i, sum, n;

    sum = 0;

    n = num;


    for(i=1; i<n; i++)

    {

        /* If i is a divisor of num */

        if(n%i == 0)

        {

            sum += i;

        }

    }

    return (num == sum);

}


int main()

{

int num;

printf("Enter a number:");

    scanf("%d", &num);

    

    if(isPerfect(num))

    {

        printf("%d is Perfect Number", num);

    }

    else

    {

        printf("%d is not Perfect Number", num);

    }

    return 0;

}

Output:


Write a C code to read a number and check whether it is prime. A function isPrime() is used here.

 Source code:

/A Prime number is a natural number greater than 1 which has no positive divisors other than 1 and itself. Write a C code to read a number and check whether it is prime. A function isPrime() is used here.*/

#include <stdio.h>

int isPrime(int num);

int isPrime(int num)

{

    int i;

    for(i=2; i<=num/2; i++)

    {

      /* If the number is divisible by any number

         other than 1 and self then it is not prime */


        if(num%i == 0)

        {

            return 0;

}

    }

    return 1;

}


int main()

{

    int num;

printf("Enter a number:");

    scanf("%d", &num);

    

    if(isPrime(num))

    {

        printf("%d is Prime Number", num);

    }

    else

    {

        printf("%d is not Prime Number", num);

    }

return 0;

}

Output:

C code to read a number and check whether it is prime


Write a program in C to convert a decimal number to binary using recursion.

 Source code: //Write a program in C to convert a decimal number to binary using recursion. #include<stdio.h> long convertB_to_D(int d...