﻿ C Program: Find angle between given hour and minute hands - w3resource

# C Exercises: Find angle between given hour and minute hands

## C Programming Mathematics: Exercise-28 with Solution

Write a C program to find the angle between the hour and minute hands.

Example:
Input: int ha = 11
int ma = 30
Output: Angle between hour and minute hands 165

Sample Solution:

C Code:

``````#include <stdio.h>
#include <math.h>

int calcAngle(int ha, int ma)
{
if (ha == 12)
ha = 0;
if (ma == 60)
ma = 0;
int hour_angle = (int)(0.5 * (ha*60 + ma));
int minute_angle = (int)(6*ma);

int angle = abs(hour_angle - minute_angle);
int ang = 360-angle;
return (ang > angle) ? angle : ang;
}

int main(void)

{
int ha = 11;
printf("\nAngles move by hour hand: %d",ha);
int ma = 30;
printf("\nAngles move by minute hand: %d",ma);
if (ha <0 || ma < 0 || ha > 12 || ma > 60)
{
printf("\nWrong input..!");
}
else
{
printf("\nAngle between hour and minute hands %d",calcAngle(ha, ma));
}
}
```
```

Sample Output:

```Angles move by hour hand: 11
Angles move by minute hand: 30
Angle between hour and minute hands 165
```

Flowchart:

C Programming Code Editor:

Improve this sample solution and post your code through Disqus.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.

﻿

## C Programming: Tips of the Day

C Programming - How do you pass a function as a parameter in C?

Declaration

A prototype for a function which takes a function parameter looks like the following:

```void func ( void (*f)(int) );
```

This states that the parameter f will be a pointer to a function which has a void return type and which takes a single int parameter. The following function (print) is an example of a function which could be passed to func as a parameter because it is the proper type:

```void print ( int x ) {
printf("%d\n", x);
}
```

Function Call

When calling a function with a function parameter, the value passed must be a pointer to a function. Use the function's name (without parentheses) for this:

```func(print);
```

would call func, passing the print function to it.

Function Body

As with any parameter, func can now use the parameter's name in the function body to access the value of the parameter. Let's say that func will apply the function it is passed to the numbers 0-4. Consider, first, what the loop would look like to call print directly:

```for ( int ctr = 0 ; ctr < 5 ; ctr++ ) {
print(ctr);
}
```

Since func's parameter declaration says that f is the name for a pointer to the desired function, we recall first that if f is a pointer then *f is the thing that f points to (i.e. the function print in this case). As a result, just replace every occurrence of print in the loop above with *f:

```void func ( void (*f)(int) ) {
for ( int ctr = 0 ; ctr < 5 ; ctr++ ) {
(*f)(ctr);
}
}
```

Ref : https://bit.ly/3skw9Um