Code:
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
int main()
{
int array[100], num1, c, n, num2;
printf("Enter number of elements in array\n");
scanf("%d", &n);
printf("Enter %d elements\n", n);
for ( c = 0 ; c < n ; c++ )
scanf("%d", &array[c]);
printf("Enter the number to swap\n");
scanf("%d", &num1);
printf("Enter the number to swap with\n");
scanf("%d", &num2);
printf("%d is swap place with %d.\n", num1, num2);
for (c = 0; c < n; c++)
{
if (array[c] == num1)
{
array[c] = num2;
}
else if (array[c] == num2)
{
array[c] = num1;
}
}
printf("The new output will be\n");
for (c = 0; c < n; c++)
printf("%d\n", array[c]);
getch();
return 0;
}
This is my coding for swapping element. so how should i input the -1 inside this code? i tried to use: if ( -1 == n) printf("end program"); but don't works.
can anyone help me out?