0% found this document useful (0 votes)
3 views7 pages

Clock Angle and Student Record Programs

Uploaded by

ak9587646
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views7 pages

Clock Angle and Student Record Programs

Uploaded by

ak9587646
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Problem-1: Angel between hands of the clock.

You are asked to determine the smaller angle between the hour hand and the minute hand of an
analog clock at a given time.

You are given the time in hours and minutes (and optionally seconds).

Hours can be given in 12-hour format (1–12) or 24-hour format (0–23).

Minutes range from 0–59.

Print the smallest angle (in degrees) between the hour hand and the minute hand.

The answer should always be between 0° and 180°.

Solution:

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

int main() {
int h, m;
printf("Enter time in hours and minutes (e.g., 3
15): ");
scanf("%d %d", &h, &m);

h = h % 12;
float hour_angle = (h * 30.0) + (m * 0.5);

float minute_angle = m * 6.0;


float diff = fabs(hour_angle - minute_angle);

if (diff > 180.0)


diff = 360.0 - diff;

printf("The angle between the hands is: %.2f


degrees\n", diff);

return 0;
}
Problem-2: Digital record of students.

Suppose you are the class representative and it is the start of a new academic year. You need to
keep a digital record of the students in your class.

Write a C program that will:

Take the number of students initially admitted and store their names.

Provide a menu with the following options:

Admit new students → If new students join later, add their names to the list (using dynamic
memory allocation).

View students → Display the names of all admitted students.

Exit → End the program.

Solution:

#include<stdio.h>
#include<string.h>
#include<stdlib.h>

int main(){
int total_student = 0;
char **name;
printf("Enter the number of present students:");
scanf("%d", &total_student);
name = (char **)malloc(total_student*sizeof(char
*));
if(name == NULL){
printf("Memory not allocated");
exit(0);
}
char input[100];
char c;
scanf("%c", &c);
for(int i = 0; i < total_student; i++){
printf("Enter the name of the student no.%d:",
i+1);
fgets(input, sizeof(input), stdin);
int length = strlen(input);
if(input[length - 1] == '\n') input[length - 1]
= '\0';
name[i] = (char *)malloc((strlen(input) + 1) *
sizeof(char));
if(name[i] == NULL){
printf("Memory not allocated");
exit(0);
}
strcpy(name[i], input);
}
int new_student = 0;
int answer = 0;
while(1){
printf("1)Admit new students\n2)View
students\n3)Exit\n");
scanf("%d", &answer);
if(answer == 1){
printf("Enter the number of new students:");
scanf("%d", &new_student);
name = realloc(name,
(total_student+new_student)*sizeof(char *));
if(name == NULL){
printf("Memory not allocated");
exit(0);
}
scanf("%c", &c);
for(int i = total_student; i <
total_student+new_student; i++){
printf("Enter the name of the student
no.%d:", i+1);
fgets(input, sizeof(input), stdin);
int length = strlen(input);
if(input[length - 1] == '\n')
input[length-1] = '\0';
name[i] = (char *)malloc((length+1) *
sizeof(char));
if(name[i] == NULL){
printf("Memory not allocated");
exit(0);
}
strcpy(name[i], input);
}
total_student += new_student;
}
else if( answer == 2){
for(int i = 0; i < total_student; i ++){
puts(name[i]);
}
}
else break;
}
for(int i = 0; i < total_student; i++)
free(name[i]);
free(name);
return 0;
}

You might also like