﻿ C Program: Find the larger of two specified integers

# C Exercises: Find the larger from two given integers of specified pattern

## C-programming basic algorithm: Exercise-27 with Solution

Write a C program to find the larger of two given integers. However if the two integers have the same remainder when divided by 5, choose the smaller integer. If the two integers are the same, return 0.

C Code:

``````#include <stdio.h> // Include standard input/output library
#include <stdlib.h> // Include standard library for additional functions

// Function declaration for 'test' with two integer parameters 'x' and 'y'
int test(int x, int y);

int main(void){
// Print the result of calling 'test' with different integer values and format the output
printf("%d",test(11, 21));
printf("\n%d",test(11, 20));
printf("\n%d",test(10, 10));
}

// Function definition for 'test'
int test(int x, int y)
{
if (x == y) // If 'x' is equal to 'y'
{
return 0; // Return 0
}
else if ((x % 5 == y % 5 && x < y) || x > y) // If the conditions inside the parentheses are met
{
return x; // Return 'x'
}
else
{
return y; // Return 'y'
}
}
``````

Sample Output:

```11
20
0
```

Explanation:

```int test(int x, int y, int z) {
if (x == y) {
return 0;
} else if ((x % 5 == y % 5 && x < y) || x > y) {
return x;
} else {
return y;
}
}
```

The above function takes three integer inputs and returns an integer value. It first checks if the first and second input integers are equal. If they are equal, the function returns 0. Otherwise, it checks if the remainder of the first integer divided by 5 is equal to the remainder of the second integer divided by 5 and the first integer is less than the second integer. If this condition is true, then the function returns the value of the first integer. Otherwise, it returns the value of the second integer.

Time complexity and space complexity:

The time complexity of this function is O(1) because the execution time is not dependent on the input size.

The space complexity of this function is O(1) because the function uses only a constant amount of memory space irrespective of the input size.

Pictorial Presentation:

Flowchart:

C Programming Code Editor:

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.

﻿