GoogleTag

Google Search

[Programming] Question 28: Write a C Program to find the Maximum and minimum of two numbers without using any loop or condition

#include <stdio.h>

int main() {
	int num1, num2, max, min;

	printf("Enter two numbers: ");
	scanf("%d %d", &num1, &num2);

	max = num1 - ((num1 - num2) & ((num1 - num2) >> (sizeof(int) * 8 - 1)));
	min = num2 + ((num1 - num2) & ((num1 - num2) >> (sizeof(int) * 8 - 1)));

	printf("Maximum: %d\n", max);
	printf("Minimum: %d\n", min);

	return 0;
} 

 

Inside the main() function:

- Integer variables `num1`, `num2`, `max`, and `min` are declared to store the input numbers and the maximum and minimum of the two numbers respectively.

- The user is prompted to enter two numbers.

- The maximum of the two numbers is calculated using bitwise operations without using any loops or conditions.

- Similarly, the minimum of the two numbers is calculated.

- Finally, the program prints the maximum and minimum values.




Featured Posts

SQL Interview Questions Topics

 SQL Topics to prepare for interviews,   SQL Basics: Introduction to SQL SQL Data Types DDL (Data Definition Language): C...

Popular Posts