you have been an integer array A of size N.you need to pfint the number with the value closest to zero.if there are multiple elements print the number with greater value.
#include <iostream>
using namespace std;
int main()
{
int num;
printf("Enter the size of the array: ");
scanf("%d",&num);
int *A = new int[num];
for(int i =0;i<num;i++)
{
printf("Enter %d" ,i);
printf(" Element of the array: ");
scanf("%d",&A[i]);
}
int E_index=0;
for(int i =0;i<num;i++)
{
if ((A[i] >= 0 && A[i]<=A[E_index]) || (A[i]< 0 && A[i]>A[E_index]))
{
E_index = i;
}
}
printf("element closest to zero A[%d",E_index);
printf("]=%d",A[E_index]);
delete[] A;
A= nullptr;
return 0;
}
Comments
Leave a comment