Program 1. Mathematics: Hatching of eggs
There is a fish pond in your garden.
On the first day, 1 fish egg hatches, and a fish is born.
On each consecutive day, the number of eggs that hatch is twice that of the previous day.
For a given Nth day, Print how many eggs will hatch on that day.
Input Format:
The first line contains a single integer N.
Sample input:
2 –denotes N
Constraints:
1<=N<=20
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 = Integer.parseInt(br.readLine());
long eggs = (long)Math.pow(2, n-1);
System.out.println(eggs);
}
}
Program 2. Special SSDs
You are having N special SSDs, each of them work for a[i] consecutive days and don’t work for next a[i] consecutive days. Every day you will mark the SSD of your choice which is in working condition.
How many days will it take to mark at least K SSDs or more?
Constraints:
. 1<=N<=18
. 1<=K<=10^5
. 1<=a[i]<=10^5
Example 1
Input:
3 2
1 2 3
Output:
10
Solution: In Java
// Solution will be available soon
// We are very sorry for the inconvenience caused
Program 3. Given an integer, convert all digits except the most significant digit to zeros.
You are given an integer ‘N’. Write a program that will convert the digits of the integer except the most significant digit to zeroes for example, if the input is 386, the output should be 300(This is because, 3 is the most significant digit in the number, 8 and 6 are not most significant digits. So, 8 and 6 are replaced with zeroes, 3 is retained as it is)
Note:
1) Do not convert the binary digits to zero, it will give wrong output. The decimal digits of the binary number have to be converted to zeroes.
2) Input values can range between: -65534<=N<=65535
Example 1
Input:
235
Output:
200
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 n = br.readLine();
String newNum = n.substring(0, 1);
for(int i=1; i<n.length(); i++)
newNum = newNum + '0';
System.out.println(newNum);
}
}
Program 4. Sort and Square Even numbers
Write a program that will sort a given list of numbers, remove the odd numbers from the list and square each of the remaining numbers in the list.
The input file will contain the following:
Line 1: Number of elements in the list
Line 2: The numbers separated by spaces
Example 1
Input:
5
5 4 3 2 1
Output:
4 16
Example 2
Input:
10
2 8 6 5 9 1 3 8 4 12
Output:
4 16 36 64 64 144
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 = Integer.parseInt(br.readLine());
String[] str = br.readLine().split(" ");
int num[] = new int[n];
for(int i=0; i<str.length; i++)
num[i] = Integer.parseInt(str[i]);
Arrays.sort(num);
for(int i=0; i<num.length; i++)
if(num[i]%2 == 0)
System.out.print(num[i]*num[i] + " ");
}
}
Also Checkout