Program 1. A person is travelling from place A to place B. He will take 24 hours to reach the destination by road. As the person starts his journey, he has to note the current time in hours H and minutes M (non-negative integer input). The task is to find the time left for him to reach the destination (output). If the current time (hours or minutes) is a value, representing the total exceeded hours and minutes (See example 3).
The Input format for testing:
The candidate has to write the code to accept two inputs separated by a new line.
First Input- Accept value for hours which is H.
Second Input- Accept value for minutes which is M.
The Output format for testing:
The output should be a time in 24-hour format (Check the output in Examples 1 and 2 above). The hours and minutes should be separated by “::” without any
additional space(See the output in examples).
Additional messages in the output will cause the failure of the test case.
Constraints:
0<H<=100
0<M<=60
Instructions:
The System doesn’t allow any kind of hard-coded input value/values.
Written program code by the candidate will be verified against the inputs supplied from the system.
Example 1
Input:
14 -> Value of H i.e. Hours
20 -> Value of M i.e. Minutes
Output:
9::40 -> Time left to reach the destination
Example 2
Input:
1 -> Value of H i.e. Hours
15 -> Value of M i.e. Minutes
Output:
22::45 -> Time left to reach the destination
Example 3
Input:
30 -> Value of H i.e. Hours
5 -> Value of M i.e. Minutes
Output:
6::5 -> Time exceeded to reach the destination
Solution: In Java
import java.util.*;
import java.lang.*;
import java.io.*;
class CodeWindow
{
public static void main (String[] args) throws Exception
{
// your code goes here
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int hr = Integer.parseInt(br.readLine());
int mn = Integer.parseInt(br.readLine());
if (hr < 24) {
int m = 60 - mn;
int h = 24 - hr - 1;
System.out.println(h + "::" + m);
} else {
int h = hr - 24;
System.out.println(h + "::" + mn);
}
}
}
Program 2. Numbers are considered as lucky and unlucky by people based on their experiences. The upper bound(U) and lower bound(L) of a span of numbers is given.
Also, an unlucky number(D) is provided. The task here is to find all the numbers between the upper and lower bound such that:
The Sum of the digits on the right side of the number should be lesser than the digit on its left.
Eg: 9520 is a lucky number as 2>0, 5>2+0, 9>5+2+0
The lucky number should not consist of the digit ‘D’.
Example 1
Input:
510 -> Value of U
550 -> Value of L
3 -> Value of D
Output:
510 520 521 540 -> Lucky numbers
Explanation:
From the inputs given above:
Upper bound = 510
Lower bound = 550
Unlucky digit D = 3
Solution: In Java
import java.util.*;
import java.lang.*;
import java.io.*;
class CodeWindow
{
public static void main (String[] args) throws Exception
{
// your code goes here
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int ub = Integer.parseInt(br.readLine());
int lb = Integer.parseInt(br.readLine());
int d = Integer.parseInt(br.readLine());
for(int num=ub; num<=lb; num++) {
char[] str = Integer.toString(num).toCharArray();
int len = str.length;
boolean flag = true;
for(int j=0; j<len; j++)
if(str[j] - 48 == d)
flag = false;
if(flag == false)
continue;
for(int j=len-2; j>=0; j--) {
int tot = 0;
for(int k=j+1; k<len; k++) {
if(str[k] < str[j])
tot += str[k] - 48;
else flag = false;
}
if(flag != false && str[j] - 48 <= tot) {
flag = false;
break;
}
}
if(flag == true)
System.out.print(num + " ");
}
}
}
Program 3. Print below pyramid:
0
3 4 3
2 3 4 3 2
5 6 7 8 7 6 5
4 5 6 7 8 7 6 5 4
Solution: In Java
import java.util.*;
import java.lang.*;
import java.io.*;
class CodeWindow
{
public static void main (String[] args) throws Exception
{
// your code goes here
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int n = 7;
int inc = 0;
int val = 1;
for(int i=0; val<=n; i++) {
if(i % 2 == 0) {
for(int j=0; j<=n/2 - i; j++)
System.out.print(" ");
for(int j=0; j<=i; j++)
System.out.print(val-inc+j-1 + " ");
for(int j=val-inc+i-1; j>i; j--)
System.out.print(j-1 + " ");
val += 3;
inc += 1;
} else {
for(int j=0; j<=n/2 - i; j++)
System.out.print(" ");
for(int j=0; j<=i; j++)
System.out.print(val-inc+j + " ");
for(int j=val-inc+i-1; j>i+1; j--)
System.out.print(j + " ");
}
System.out.println();
}
}
}
Program 4. Write a program to print the sum of numbers in alternate positions.
Example 1
Input:
5 8 2 6 10 15 14 69 3
Output:
7 14 12 21 24 84 17
Solution: In Java
import java.util.*;
import java.lang.*;
import java.io.*;
class CodeWindow
{
public static void main (String[] args) throws Exception
{
// your code goes here
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String[] str = br.readLine().split(" ");
int arr[] = new int[str.length];
for(int i=0; i<str.length; i++)
arr[i] = Integer.parseInt(str[i]);
for(int i=0; i<arr.length-2; i++)
System.out.print(arr[i] + arr[i+2] + " ");
}
}
Also Checkout