Find the largest number among an integer set without using -if condition, bit operations,<,>,>>,<<,? Operators and also without using any library function except scanf and printf.

#include<stdio.h>

int a[100];

int ABS(int n) { // This is the most important function, return abs value of n
    int up = 0, down = 0, count = 0;
    for(count = 0; up != n && down != n; count++) { // finding abs value
    down--;
    up++;
    }
    return count;
}
int getMax(int x, int y) { // this function returns largest value among x and y
    int res;
    int d=x-y;
    d=ABS(d);
    res=( x - y ==-d) * y +  ( x - y == d) * x; // calculating max between x and y
    return res;
}
void  main()
{
    int n, i, max = -1000000;
    clrscr();
    printf("\nEnter how many number you want to check : - ");
    scanf("%d",&n);
    printf("\n\nPlease Enter %d numbers : \n\n",n);
    for(i = 0; i<n; i++) {
    scanf("%d",&a[i]); // >= -1000 && <= 1000
    }
    for(i = 0; i<n; i++) {
    max = getMax(max, a[i]);
    }
    printf("\nMaximum number is : - %d\n",max);
    getch();
}

0 comments:

Post a Comment