Showing posts with label c program. Show all posts
Showing posts with label c program. Show all posts

Monday, September 12, 2011

insert element in any array

main()
{
    int n,count=0,i,j,k,m,temp;
    int a[10];
    clrscr();
    printf("\n Enter number of elements:");
    scanf("%d",&n);
    for(i=0;i<n;i++)
    {
      printf("\n Enter the element no. %d:",i+1);
      scanf("%d",&a[i]);
    }
    for(j=1;j<n;j++)
    {
      printf("\n Iteration number %d:-\n",j-1);
      for(m=0;m<n;m++)
        printf("%d\n",a[m]);

      for(i=0;i<j;i++)
        if(a[i]>a[j])
          {
        temp=a[j];
        for(k=j;k>i;k--)
          a[k]=a[k-1];

        a[k]=temp;
        break;
          }
       printf("\n");
       getch();
       }
      printf("last iteration is %d:-\n",j-1);
      for(i=0;i<n;i++)
     printf("%d\n",a[i]);
 getch();
 }

Insert any element into an array and then sort the array

Insert any element into an array  and then sort the array
#include<stdio.h>
#include<conio.h>
main()
{
    int a[15],n,m,i,j,temp;
    clrscr();
    printf("\n How many elements: ");
    scanf("%d",&n);
    for(i=0;i<n;i++)
      {
          printf("\n Enter element no. %d : ",i+1);
          scanf("%d",&a[i]);
      }
    printf("\n Original unsorted elements are:");
    for(i=0;i<n;i++)
         printf(" %d ",a[i]);
    printf("\n enter the element to be inserted : ");
    scanf("%d",&a[n]);

    m=n+1;  // n is increased b'coz of entry of new element
    for(i=0;i<m;i++)  // Bubble Sort
        for(j=0;j<m-1;j++)
            if(a[j]<=a[j+1])
            {
                temp=a[j];
                a[j]=a[j+1];
                a[j+1]=temp;
            }
    printf("\n Sorted elements of the array are:");
    printf("\n");
    for(i=0;i<m;i++)
         printf(" %d ",a[i]);
    getch();
}

Insert any element into an array at a desired position

Insert any element into an array at a desired position
#include<stdio.h>
#include<conio.h>
main()
{
    int a[15],n,i,item,pos;
    clrscr();
    printf("\n How many elements: ");
    scanf("%d",&n);
    for(i=0;i<n;i++)
      {
          printf("\n Enter element no. %d : ",i+1);
          scanf("%d",&a[i]);
      }
    printf("\n Array elements are:");
    for(i=0;i<n;i++)
         printf(" %d ",a[i]);
    printf("\n enter the element to be inserted : ");
    scanf("%d",&item);
    printf("\n enter the position of insertion : ");
    scanf("%d",&pos);
    for(i=n;i>=pos;i--)  // shifting the elements
         a[i]=a[i-1];

    a[--pos]=item;
      /* pos=pos-1; // pos. is decremented b'coz of Array storage process
    a[pos]=item; */

    printf("\n New array elements are:");
    printf("\n");
    for(i=0;i<=n;i++)
         printf(" %d ",a[i]);
    getch();
}

Reverse array

main()
{
    int a[10],n,i,j,t;
    clrscr();
    printf("\n Enter no. of terms:");
    scanf("%d",&n);
    for(i=0;i<n;i++)
     {
       printf("\n Enter ele. no. %d-:",i+1);
       scanf("%d",&a[i]);
     }

    for(i=0,j=n-1;i<=n/2;i++,j--)
       {
          t=a[i];
          a[i]=a[j];
          a[j]=t;
       }
    printf("\n Reversed Array\n");
    for(i=0;i<n;i++)
       printf("\n %d",a[i]);
    getch();
}

Array Bubble sort

-: Bubble Sort :-
#include<stdio.h>
#include<conio.h>
main()
{
    int i,j,n,a[10],temp;
    clrscr();
    printf("\nEnter the number of terms:");
    scanf("%d",&n);
    printf("\n Enter the elements:");
    for(i=0;i<n;i++)
    {
        printf("\n a[%d]=",i);
        scanf("%d",&a[i]);
    }
    for(i=0;i<n;i++)
        for(j=0;j<n-1;j++)
            if(a[j]<=a[j+1])
            {
                temp=a[j];
                a[j]=a[j+1];
                a[j+1]=temp;
            }
    for(i=0;i<n;i++)
        printf("\n %d",a[i]);
    getch();
}

Selection Sort


//                    -: Selection Sort :-
#include<stdio.h>
#include<conio.h>
main()
{
    int i,j,n,a[10],temp;
    clrscr();
    printf("\nEnter the number of terms:");
    scanf("%d",&n);
    printf("\n Enter the elements:");
    for(i=0;i<n;i++)
    {
        printf("\n a[%d]=",i);
        scanf("%d",&a[i]);
    }
    for(i=0;i<n;i++)
        for(j=i;j<n;j++)
            if(a[i]>a[j])
            {
temp=a[j];
                a[j]=a[i];
                a[i]=temp;
            }
    for(i=0;i<n;i++)
        printf("\n %d",a[i]);
    getch();
}

Insertions Short

main()
{
    int a[25], i, j, k, m, t ;

    for(i=0;i<=24;i++ )
        scanf("%d",&a[i] ) ;


    for(i=1;i<=24;i++)
    {
        t = a[i] ;
        for(j=0;j<i;j++)
        {
            if(t<a[j])
            {
                for(k=i;k>=j;k--)
                     a[k] = a[k-1] ;

                a[j] = t ;
                break ;
            }
        }
    }

    for(i=0;i<=24;i++ )
                     printf("%d",a[i] ) ;
}

Delete an element from the Array

// Delete an element from the Array
#include<stdio.h>
#include<conio.h>
main()
{
    int a[15],n,i,j,flag=0,item,num;
    clrscr();
    printf("\n How many elements : ");
    scanf("%d",&n);
    num=n;  //It wll be used to find the no. of duplicates
    for(i=0;i<n;i++)
        {
        printf("\n enter the element no. %d: ",i+1);
        scanf("%d",&a[i]);
        }
    printf("\n The elements are: \n");
    for(i=0;i<n;i++)
        printf(" %d ",a[i]);
    printf("\n enter the element to be deleted : ");
    scanf("%d",&item);



    for(i=0;i<n;i++)        // Main logic
       {
          if(a[i]==item)     // i.e. item is found in the array
        {
            flag=1;
            for(j=i;j<n;j++)
            a[j]=a[j+1]; //using shifting, item is deleted
            n--;
        }    // Looping to check whether more 'item' ;left or not
       }  //end of main logic

       if(flag==0)
        printf("\n element not found  ");
       else
       {
      printf("\n Array has %d duplicates",num-n);
      printf("\n Array after deletion \n");
      for(i=0;i<n;i++)
        printf(" %d ",a[i]);
    }
    getch();
 }

find the prime factor of the given number

find the prime factor of the given number */
#include<stdio.h>
#include<conio.h>
main()
{
    int i=2,j=0,k,n,arr[15];
    clrscr();
    printf("enter any integer greater than 1:");
    scanf("%d",&n);
    while(i<=n)
    {
        if(n%i==0)
        {
            do{
            arr[j]=i;
            n=n/i;
            j++;
            }while(n%i==0);
        }
        else
        i++;
    }
    printf("prime factors of the number are\n");
     /*    for(k=j-1;k>=0;k--)
    printf("%d\t",arr[k]);    */
    for(k=0;k<=j-1;k++)
    printf("%d\t",arr[k]);
    getch();
}


Popular 5 Posts Last 7 days