Monday, November 24, 2014

Useful c program



Write a C program using pointers to read in an array of integers and print its elements in reverse order

#include
#include
#define max 30
 void main()
{
   int size, i, arr[max];
   int *ptr;
   clrscr();
   ptr = &arr[0]; 
   printf("\nEnter the size of array : ");
   scanf("%d", &size);
   printf("\nEnter %d integers into array: ", size);
   for (i = 0; i < size; i++)
   {
      scanf("%d", ptr);
      ptr++;
   }
   ptr = &arr[size - 1];
   printf("\nElements of array in reverse order are :");
   for (i = size - 1; i >= 0; i--)
   {
      printf("\nElement%d is %d : ", i, *ptr);
      ptr--;
   }

   getch();
}

Output

Enter the size of array : 5
Enter 5 integers into array : 11 22 33 44 55
Elements of array in reverse order are :
Element 4 is : 55
Element 4 is : 44
Element 4 is : 33
Element 4 is : 22
Element 4 is : 11

To delete duplicate elements in an array

#include
#include
int main()
{
   int arr[20], i, j, k, size;
   printf("\nEnter array size : ");
   scanf("%d", &size);
   printf("\nAccept Numbers : ");
   for (i = 0; i < size; i++)
   scanf("%d", &arr[i]);
   printf("\nArray with Unique list  : ");
   for (i = 0; i < size; i++)
   {
      for (j = i + 1; j < size;)
      {
         if (arr[j] == arr[i])
         {
            for (k = j; k < size; k++)
             {
               arr[k] = arr[k + 1];
            }
            size--;
         } else
            j++;
      }
   }
    for (i = 0; i < size; i++)
   {
      printf("%d ", arr[i]);
   }
    return (0);
}

Output

Enter array size : 5
Accept Numbers : 1 3 4 5 3
Array with Unique list  : 1 3 4 5

Addition of All Elements of the Array

#include
#include
int main()
{
   int i, arr[50], sum, num;
   printf("\nEnter no of elements :");
   scanf("%d", &num);
   printf("\nEnter the values :");
   for (i = 0; i < num; i++)
      scanf("%d", &arr[i]);
   sum = 0;
   for (i = 0; i < num; i++)
      sum = sum + arr[i];
   for (i = 0; i < num; i++)
      printf("\na[%d]=%d", i, arr[i]);
      printf("\nSum=%d", sum);
    return (0);
}

Output

Enter no of elements : 3
Enter the values : 11 22 33
a[0]=11
a[1]=22
a[2]=33
Sum=66
Find Smallest Element in Array in C Programming

#include
#include 
int main()
{
   int a[30], i, num, smallest;
   printf("\nEnter no of elements :");
   scanf("%d", &num);
             for (i = 0; i < num; i++)
      scanf("%d", &a[i]);
      smallest = a[0];
   for (i = 0; i < num; i++)
   {
      if (a[i] < smallest)
     {
         smallest = a[i];
      }
   }
   printf("\nSmallest Element : %d", smallest);
   return (0);
}

Output

Enter no of elements : 5
11 44 22 55 99
Smallest Element : 11

Find Largest Element in Array in C Programming

#include
 #include
int main()
 {
   int a[30], i, num, largest;
   printf("\nEnter no of elements :");
   scanf("%d", &num);
   for (i = 0; i < num; i++)
      scanf("%d", &a[i]);
   largest = a[0];
    for (i = 0; i < num; i++)
    {
      if (a[i] > largest)
     {
         largest = a[i];
      }
   }
   printf("\nLargest Element : %d", largest);
   return (0);
}

Output

Enter no of elements : 5
11 55 33 77 22
Largest Element : 77

C Program to Reversing an Array Elements in C Programming


#include
#include 
int main()
{
   int arr[30], i, j, num, temp;
   printf("\nEnter no of elements : ");
   scanf("%d", &num);
   for (i = 0; i < num; i++)
  {
      scanf("%d", &arr[i]);
   }
   j = i - 1;  
   i = 0;      
   while (i < j)
  {
      temp = arr[i];
      arr[i] = arr[j];
      arr[j] = temp;
      i++;            
      j--;          
   }
    printf("\nResult after reversal : ");
   for (i = 0; i < num; i++)
   {
      printf("%d \t", arr[i]);
   }
    return (0);
                }

Output

 Enter no of elements : 5
11 22 33 44 55
Result after reversal : 55 44 33 22 11

C Program to Merge Two arrays in C Programming


#include
#include 
int main()
{
    int arr1[30], arr2[30], res[60];
    int i, j, k, n1, n2;
     printf("\nEnter no of elements in 1st array :");
    scanf("%d", &n1);
    for (i = 0; i < n1; i++)
   {
        scanf("%d", &arr1[i]);
    }
    printf("\nEnter no of elements in 2nd array :");
    scanf("%d", &n2);
    for (i = 0; i < n2; i++)
    {
        scanf("%d", &arr2[i]);
    }
    i = 0;
    j = 0;
    k = 0;
    while (i < n1 && j < n2)
    {
        if (arr1[i] <= arr2[j])
        {
            res[k] = arr1[i];
            i++;
            k++;
        }
           else
           {
            res[k] = arr2[j];
            k++;
            j++;
        }
    }
     while (i < n1)
      {
        res[k] = arr1[i];
        i++;
        k++;
    }
    while (j < n2)
    {
        res[k] = arr2[j];
        k++;
        j++;
    }
    printf("\nMerged array is :");
    for (i = 0; i < n1 + n2; i++)
        printf("%d ", res[i]);
        return (0);
 }

Output

Enter no of elements in 1st array : 4
11 22 33 44
Enter no of elements in 2nd array : 3
10 40 80
Merged array is : 10 11 22 33 40 44 80

No comments:

Post a Comment