suppose there are five integers write a program to find
larger among them without using if- else

Answers were Sorted based on User's Feedback



suppose there are five integers write a program to find larger among them without using if- else..

Answer / icywind

#include <stdio.h>
#define SIZE 5
int main(void)
{

int array[] = {100, 32, 22, 500, 21};
int max=0,ii;

for( ii = 0; ii<SIZE; ii++)
{
max = (array[ii]>max)?array[ii]:max;
}
printf("max = %d\n", max);
return 0;
}

Is This Answer Correct ?    7 Yes 1 No

suppose there are five integers write a program to find larger among them without using if- else..

Answer / brindha

#define<stdio.h>
void main()
{
int num, max = 0;
for (i = 0;i < 5; i ++)
{
scanf("Enter next number: %d", &num);
max = findMax(max, num);
}
printf(" MAX is %d", max);
}

int findMax(int x, int y)
{
int mask = 0, result;
mask = (x - y) >> 31;
result = (~mask & x) | (mask & y);
return result;
}

Is This Answer Correct ?    0 Yes 2 No

Post New Answer

More C Interview Questions

Do you know what is the purpose of 'extern' keyword in a function declaration?

0 Answers  


You have an int array with n elements and a structure with three int members. ie struct No { unsigned int no1; unsigned int no2; unsigned int no3; }; Point1.Lets say 1 byte in the array element is represented like this - 1st 3 bits from LSB is one number, next 2 bits are 2nd no and last 3 bits are 3rd no. Now write a function, struct No* ExtractNos(unsigned int *, int count) which extracts each byte from array and converts LSByte in the order mentioned in point1.and save it the structure no1, no2, no3. in the function struct No* ExtractNos(unsigned int *, int count), first parameter points to the base address of array and second parameter says the no of elements in the array. For example: if your array LSB is Hex F7 then result no1 = 7, no2 = 2, no3 = 7. In the same way convert all the elements from the array and save the result in array of structure.

2 Answers   Qualcomm,


#define DCHAR char* typedef char* TCHAR; if using these following variables will be declared like DCHAR ch1, ch2; TCHAR ch3, ch4; then what will be types of ch1, ch2, ch3 and ch4?

6 Answers   NDS,


What are types of structure?

0 Answers  


Explain a pre-processor and its advantages.

0 Answers  






find the size of structure without using the size of function

1 Answers   Bosch,


Please provide question papers of NATIONAL INFORMATICS CENTRE for Scientific officer

0 Answers  


When is a null pointer used?

0 Answers  


Differentiate between new and malloc(), delete and free() ?

0 Answers   iNautix,


Is using exit() the same as using return?

0 Answers  


There are 21 people in a room. They have to form groups of 3 people each. How many combinations are possible? Write a C program to print the same.

1 Answers   HCL, iGate,


When should the volatile modifier be used?

0 Answers  


Categories