C Program to Replace Array Elements by Given Value

C Program to Replace Array Elements by Given Value

Write C Program to Replace Array Elements by Given Value

// C Program to Replace Array Elements by Given Value

#include <stdio.h>

int main()
{
	int A[] = {1, 6, 5, 8, 6}, n, k, i, j, flag=0;

	n = sizeof(A) / sizeof(A[0]);

	printf("Array A :--> ");
	for(i=0; i<n; i++)
        printf("%d  ", A[i]);


	printf("\nEnter the Element to replace :--> ");
	scanf("%d", &k);


	for(i = 0;i < n; i++)
	{
		if(A[i] == k)
		{
			int r;
			flag = 1;
			printf("Enter Element Replaced with %d:--> ",i);
			scanf("%d", &r);
			A[i] = r;
		}
	}
	if(flag == 0)
	{
		printf("\nThe element not found in the array");
	}
	else
	{
	    printf("\nArray After Replacement :--> ");
		for(j = 0; j < n; j++)
		{
            printf("%d ",A[j]);
		}
	}
	return 0;
}

Output:

Run 1:
Array A :--> 1  6  5  8  6
Enter the Element to replace :--> 3

The element not found in the array

Run 2:
Array A :--> 1  6  5  8  6
Enter the Element to replace :--> 6
Enter Element Replaced with 1:--> 11
Enter Element Replaced with 4:--> 22

Array After Replacement :--> 1 11 5 8 22