Array Program #4

7. Read n numbers in an array, read two different numbers, search the first number in an array and replace it with another number in an array and print its index and final array.

#include <stdio.h>
int main()
{
    int array[100], x,y, i, n, count = 0;
     
        printf("Enter the number of elements in array\n");
     scanf("%d", &n);
     
     printf("Enter %d numbers\n", n);
     
     for (i = 0; i < n; i++)
        scanf("%d", &array[i]);
       
    printf("Enter n number by which you want to replace?\n");
     scanf("%d",&x);
   
     printf(" By which number?\n");
     scanf("%d",&y);
     
     for (i = 0; i < n; i++)
     {
        if (array[i] == x)
        {
             array[i]=y;
           printf("%d is present at location %d.\n", y, i+1);
           count++;
        }
     }
     if (count == 0)
        printf("%d isn't present in the array.\n", x);
     else
        printf("%d is present %d times in the array.\n", y, count);
     printf("Resultant array is\n");
     for (i=0;i<n;i++)
     {
         printf("%d\n",array[i]);
     }
}

Output

  • Enter the number of elements in array
    10
    Enter 10 numbers
    12
    4
    2
    6
    4
    7
    4
    9
    13
    8
    Enter n number by which you want to replace?
    4
     By which number?
    0
    0 is present at location 2.
    0 is present at location 5.
    0 is present at location 7.
    0 is present 3 times in the array.
    Resultant array is
    12
    0
    2
    6
    0
    7
    0
    9
    13
    8
  • Enter the number of elements in array
    5
    Enter 5 numbers
    23
    12
    45
    65
    20
    Enter n number by which you want to replace?
    60
     By which number?
    23
    60 isn't present in the array.
    Resultant array is
    23
    12
    45
    65
    20




For download this program please check the link ➡➡ Download

Comments

Popular

Basic Program #3

Pointer Program #1

Singly Linked List program #1

Basic program #2