Monday, November 24, 2014

C using array pro



C Program to Copy all elements of an array into another array


#include
#include 
int main()
{
   int arr1[30], arr2[30], i, num;
   printf("\nEnter no of elements :");
   scanf("%d", &num);
              printf("\nEnter the values :");
   for (i = 0; i < num; i++)
   {
      scanf("%d", &arr1[i]);
   }
   for (i = 0; i < num; i++)
  {
      arr2[i] = arr1[i];
   }
   printf("The copied array is :");
   for (i = 0; i < num; i++)
      printf("\narr2[%d] = %d", i, arr2[i]);

   return (0);
}

Output

Enter no of elements : 5
Enter the values : 11 22 33 44 55
The copied array is : 11 22 33 44 55

C Program to Insert an element in an Array


#include
#include 
int main()
{
   int arr[30], element, num, i, location;
   printf("\nEnter no of elements :");
   scanf("%d", &num);
   for (i = 0; i < num; i++)
   {
      scanf("%d", &arr[i]);
   }
   printf("\nEnter the element to be inserted :");
   scanf("%d", &element);
   printf("\nEnter the location");
   scanf("%d", &location);
   for (i = num; i >= location; i--)
  {
      arr[i] = arr[i - 1];
   }
   num++;
   arr[location - 1] = element;
   for (i = 0; i < num; i++)
      printf("n %d", arr[i]);
   return (0);
}

Output

Enter no of elements : 5
1 2 3 4 5
Enter the element to be inserted : 6
Enter the location : 2
1 6 2 3 4 5

C Program to Delete an element from the specified location from Array

#include
#include
int main()
{
   int arr[30], num, i, loc;
   printf("\nEnter no of elements :");
   scanf("%d", &num);
   printf("\nEnter %d elements :", num);
   for (i = 0; i < num; i++)
  {
      scanf("%d", &arr[i]);
   }
   printf("\n location of the element to be deleted :");
   scanf("%d", &loc);
   while (loc < num)
  {
      arr[loc - 1] = arr[loc];
      loc++;
   }
   num--;  
   for (i = 0; i < num; i++)
      printf("\n %d", arr[i]);
    return (0);
}

C Program to Print Array Elements


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

C Program to Read Array Elements


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

No comments:

Post a Comment