Simple Tasks on numbers - C programming


Task 01
Write a program to read two integers with the following significance.
The first integer value represents a time of day on a 24 hour clock, so that 1245 represents quarter to
one mid-day, for example.
The second integer represents time duration in a similar way, so that 345 represents three hours and 45
minutes. This duration is to be added to the first time, and the result printed out in the same notation,
in this case 1630 which is the time 3 hours and 45 minutes after 12.45.
#include <stdio.h>

int main(){

int time,duration,end_time;

printf("Enter the start time: ");
scanf("%d",&time);
printf("Enter the duration: ");
scanf("%d",&duration);

int count=0;

if(duration>60){
while(duration>60){

duration=duration-60;
count++;
}
}
int hours,minutes;
hours=(time/100+count)*100; //(duration/60);
minutes=time%100+duration;

end_time=hours+minutes;
if(end_time>=2400){

end_time=end_time-2400;
}

printf("End time is %04d.\n",end_time);

Task 02
Your second task for this lab is to write a program named quad.c which prompts the user to input
three coefficients a, b, c for a quadratic equation
ax 2 + bx + c = 0
Your program should then compute the solution(s) to this equation, using the quadratic formula (see
http://en.wikipedia.org/wiki/Quadratic_equation), and print them. Use one or more "if" statements to
ensure that your program handles all cases correctly.

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

int main(){

float a,b,c,answer1,answer2,answer,complex_answer;;

printf("Enter the coefficients: ");
scanf("%f%f%f",&a,&b,&c);

if((b*b-4*a*c)>=0){

answer1=(-b+sqrt(b*b-4*a*c))/2*a;
answer2=(-b-1*sqrt(b*b-4*a*c))/2*a;

printf("The solutions are %.2f and %.2f.\n",answer1,answer2);

return 0;
}
if((b*b-4*a*c)<0){

answer=-b/2*a;

complex_answer=(sqrt(4*a*c-b*b))/2*a;
printf("The solutions are %.2f+%.2fi and %.2f-%.2fi.\n",answer,complex_answer,answer,complex_answer);
}
return 0;
}

The code is in github : https://github.com/DGunasekara/lab01

Comments

Popular posts from this blog

How to push a file into a docker container

Docker - Begginer 1

Project(on going) - IPv6 Fragmentation