Adjust Time - Hours, Minutes, Seconds
Adjust Time - Hours, Minutes, Seconds
PROBLEM STATEMENT :
The program must accept a time T in 24-hr format (HH:MM: SS) and three integers X, Y, Z as the input. The integer X represents the number of hours to be added to the time T. The integer Y represents the number of minutes to be added to the time T. The integer Z represents the number of seconds to be added to the time T. After each adjustment of the time (add X hours, add Y minutes, and add Z seconds), the program must print the revised time as the output.
Boundary Condition(s):
1 <= X, Y, Z <= 10^6
Input Format:
The first line contains the time T in 24-hr format (HH:MM: SS).
The second line contains X, Y, and Z separated by a space.
Output Format:
The first three lines, each contains the revised time after each adjustment of the given time.
Example Input/Output 1:
Input: ()
10:05:45
4 65 100
Output:
14:05:45
15:10:45
15:12:25
Explanation:
Here T = 10:05:45, X = 4, Y = 65 and Z = 100.
After adding 4 hours, the time becomes 14:05:45.
After adding 65 minutes, the time becomes 15:10:45.
After adding 100 seconds, the time becomes 15:12:25.
Example Input/Output 2:
Input: ()
23:50:00
3 15 45
Output:
02:50:00
03:05:00
03:05:45
1) LEARN THRICE
👇
2) THINK TWICE
👇
3) APPLY ONCE
SOLUTION :
C (Programming Language)
#include<stdio.h>
#include<stdlib.h>
int main()
{
int HH,MM,SS,X,Y,Z;
scanf("%d:%d:%d",&HH,&MM,&SS);
scanf("%d %d %d",&X,&Y,&Z);
int hour=HH,min=MM,sec=SS;
hour=(HH+X)%24;
printf("%02d:%02d:%02d\n",hour%24,min%60,sec%60);
min=(MM+Y)%60;
hour=hour+((MM+Y)/60);
printf("%02d:%02d:%02d\n",hour%24,min%60,sec%60);
sec=(SS+Z)%60;
min=min+((SS+Z)/60);
hour=hour+(min/60);
printf("%02d:%02d:%02d",hour%24,min%60,sec%60);
return 0;
}
C++ (CPP)
#include <bits/stdc++.h>
using namespace std;
int main(int argc, char** argv)
{
int h,m,s;
char c;
cin>>h>>c>>m>>c>>s;
int x,y,z;
cin>>x>>y>>z;
h+=x;
h%=24;
printf("%02d:%02d:%02d\n",h,m,s);
m+=y;
h+=m/60;
m%=60;
h%=24;
printf("%02d:%02d:%02d\n",h,m,s);
s+=z;
m+=s/60;
h+=m/60;
printf("%02d:%02d:%02d\n",h%24,m%60,s%60);
}
JAVA
import java.util.*;
public class Hello {
public static void main(String[] args) {
Scanner s=new Scanner(System.in);
String time=s.nextLine();
String[] a=time.split(":");
int hh=Integer.parseInt(a[0]);
int mm=Integer.parseInt(a[1]);
int ss=Integer.parseInt(a[2]);
int x=s.nextInt(),y=s.nextInt(),z=s.nextInt();
int h=(hh+x)%24;
System.out.printf("%02d:%02d:%02d\n",h%24,mm%60,ss%60);
int m=(mm+y)%60;
h=h+(mm+y)/60;
System.out.printf("%02d:%02d:%02d\n",h%24,m%60,ss%60);
int sec=(ss+z)%60;
m=m+(ss+z)/60;
h=h+m/60;
System.out.printf("%02d:%02d:%02d",h%24,m%60,sec%60);
}
}
PYTHON
hr,min,sec=map(int,input().split(':'))
hr1,min1,sec1=map(int,input().split())
hr+=hr1
hr%=24
print("%02d:%02d:%02d" % (hr,min,sec))
min+=min1
hr+=min//60
hr%=24
min%=60
print("%02d:%02d:%02d" % (hr,min,sec))
sec+=sec1
min+=sec//60
hr+=min//60
hr%=24
min%=60
sec%=60
print("%02d:%02d:%02d" % (hr,min,sec))

Upload the some question on tree & graph
ReplyDeleteSure. Mostly advanced questions will be posted on Competitive Coding Blog. Follow up for more....
Delete