C language: Input 10 integers and find the number with the smallest absolute value

1
Enter 10 integers and find the number with the smallest absolute value (10 points)

Topic description

Enter 10 integers and find the number with the smallest absolute value

enter

ten integers

output

The number with the smallest absolute value

sample input

-10 -2 30 40 50 60 70 80 -90 100

Sample output

-2
absolute value function integer abs()
          

It is not necessary to consider the case where two absolute values ​​are the smallest at the same time

coding:

#include<stdio.h>
#include<math.h>
#define MAX 10
int main(void)
{
	int num[MAX];
	int i = 0, absmin = 0, min = 0;
 
 	for(; i < MAX; i++)
	{
  		scanf("%d",&num[i]);
 	}
 
 	absmin = abs(num[0]);
 	for(i = 1;i < MAX; i++)
	{
  		if(abs(num[i]) <= absmin)
		{
   			absmin = abs (num [i]);
   			min = i;
   		}
 	}
 
 	printf("%d\n",num[min]);
 	return 0;
}

  

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324838218&siteId=291194637