In: Computer Science
Code is in C
Write a program that reads integers from stdin. Once it reaches the
* end of the input, it prints the smallest absolute value among those
* of the numbers it read.
*
* For example, if
* 4, 6 -3, 3, -2, 13, -4
* are read from stdin, the program should print 2.
*
* If the end of file is reached before any integer is seen, the
* number printed should be INT_MAX (defined in limits.h).
*
* The program should return 0 if successful and -1 if there are
* errors in the input.
*
* You may assume that the absolute value of all negative integers
* successfully read by your program may be stored in an int.
#include <stdio.h>
#include <limits.h>
#include <stdlib.h>
int solve(){
int num ; // the variable where input is going to be taken upto End Of File
int smallestAbsValue = INT_MAX; // the variable declared as the smallestAbsVal which will store the smallest Absolute vlaue till the EOF
int error = 0;
while(scanf("%d", &num)!=EOF){ // iterating till the EOF (end of file)
if(abs(num) < smallestAbsValue){ // checking the codition for the final Ans
smallestAbsValue = abs(num); // upadte the answer variable
}
}
printf("Smallest Absolute value is %d ", smallestAbsValue);
return error;
// if there is an error it should retunr -1
}
int main() {
int flag = solve();
if(flag ==0 ) printf("\nProgramme executed successfully");
else if(flag == -1) printf("There is Error input");
return 0;
}