Sunday, March 11, 2018

Check Number Sequence

Check Number sequence
Send Feedback

You are given S a sequence of n integers i.e. S = s1, s2, ..., sn. Compute if it is possible to split S into two parts : s1, s2, ..., si and si+1, si+2, ….., sn (0 <= i <= n) in such a way that the first part is strictly decreasing while the second is strictly increasing one.

That is, in the sequence if numbers are decreasing, they can start increase at one point. And once number starts increasing, they cannot decrease at any point further.

Sequence made up of only increasing numbers or only decreasing numbers is a valid sequence. So in both the cases, print true.
You just need to print true/false. No need to split the sequence.
Input format :
Line 1 : Integer n
Line 2 : n integers (separated by space)
Output Format :
"true" or "false" (without quotes)
Sample Input 1 :
5
9 8 4 5 6
Sample Output 1 :
true
Sample Input 2 :
3
1 2 3
Sample Output 2 :
true

import java.util.Scanner;
public class Main {

public static void main(String[] args) {
// Write your code here
Scanner scan = new Scanner(System.in);
int n = scan.nextInt();
int[]num=new int [n];
int count = 0 , temp=0;
for (int i=0;i<n;i++)
{
num [i] = scan.nextInt();
    }
for(int i = 0 ; i<n-1;i++)
{
    if(count==0)
    {
if(num[i]<num[i+1])
{
count++;
}
    }
else if(count==1)
{
if(num[i]>num[i+1])
{
count++;
}
}
else if(count>1)
{
    break;
}
}
if(count==0||count==1)
{
System.out.println("true");
}
else
{
System.out.println("false");
}
}
}

Square root

Square Root (Decimal)
Send Feedback

Given a number n, find the square root with an accuracy of d decimal points. n & d is provided by the user.

If the number is perfect square, print integer only. For eg. if n = 16 & d = 2, output should be 4
Input format : n and d
Sample Input 1 :
10 4
Sample Output 1 :
3.1622
Sample Input 2 :
16 2
Sample Output 2 :
4
Sample Input 3 :
10 0
Sample Output 3 :
3


import java.util.Scanner;
public class Main {

public static void main(String[] args) {
// Write your code here
Scanner s=new Scanner(System.in);
int  n=s.nextInt();
int d=s.nextInt();
float sqrt = 0;
int temp = (int) Math.sqrt(n);
int a = (int) n;
sqrt=(float)Math.sqrt(n);
if (temp * temp == a) {
System.out.print(temp);
} else
{
int p = 1;
int j = 1;
while(j <= d)
{
p = p * 10;
j++;
}

sqrt = (int)(sqrt * p);

System.out.print(sqrt / p);
}
}
}

Square root integral

Square Root (Integral)
Send Feedback

Given a number N, find its square root. You need to find and print only the integral part of square root of N.

For eg. if number given is 18, answer is 4.

Input format :
Integer N
Output Format :
Square root of N (integer part only)
Sample Input 1 :
10
Sample Output 1 :
3
Sample Input 2 :
4
Sample Output 2 :
2

import java.util.Scanner;

public class Main
{

public static void main(String[] args)
{



Scanner scanner = new Scanner(System.in);
   
 
   
        int square = scanner.nextInt();
   
   
      int squareRoot = (int) Math.sqrt(square);
   
   
        System.out.print(squareRoot);
}
}

Decimal to binary

Decimal to Binary
Send Feedback

Given a decimal number (integer N), convert it into binary and print.

The binary number should be in the form of an integer.

Note : The given input number could be large, so the corresponding binary number can exceed the integer range. So take the answer as long.

Input format :
Integer N
Output format :
Corresponding Binary number (long)
Sample Input 1 :
12
Sample Output 1 :
1100
Sample Input 2 :
7
Sample Output 2 :
111

import java.util.Scanner;

public class Main
{
public static void main(String[] args)
{

Scanner s = new Scanner(System.in);
int n = s.nextInt();
System.out.println(Integer.toBinaryString(n));


}


}

Binary to Decimal

Binary to decimal
Send Feedback

Given a binary number as an integer N, convert it into decimal and print.

Input format :
An integer N
Output format :
Corresponding Decimal number (as integer)
Sample Input 1 :
1100
Sample Output 1 :
12
Sample Input 2 :
111
Sample Output 2 :
7

import java.util.Scanner;
public class Main
{


 public static void main(String[] args)

 {
       
 Scanner in = new Scanner( System.in );



  int  binarynum =in.nextInt();
 
  int binary=binarynum;
       
int decimal = 0;
int power = 0;

while(true)

{

 if(binary == 0)
 {

        break;

 }

 else
 {

   int tmp = binary%10;
   decimal += tmp*Math.pow(2, power);
   binary = binary/10;
   power++;

 }

}
        System.out.println(decimal);
}

}

Reverse of a number

Reverse of a number
Send Feedback

Write a program to generate the reverse of a given number N. Print the corresponding reverse number.

Input format :
Integer N
Output format :
Corresponding reverse
Sample Input 1 :
1234
Sample Output 1 :
4321
Sample Input 2 :
1980
Sample Output 2 :
891

import java.util.Scanner;
public class Main
{

public static void main(String[] args)
{
int num=0;
      int reversenum =0;
   
     
      Scanner in = new Scanner(System.in);
     
      num = in.nextInt();
   
      for( ;num != 0; )
      {
          reversenum = reversenum * 10;
          reversenum = reversenum + num%10;
          num = num/10;
      }

      System.out.println(reversenum);


}
}

Terms of AP

Terms of AP
Send Feedback

Write a program to print first x terms of the series 3N + 2 which are not multiples of 4.

N varies from 1 to 1000.
Input format :
Integer x
Output format :
Terms of series (separated by space)
Sample Input 1 :
10
Sample Output 1 :
5 11 14 17 23 26 29 35 38 41
Sample Input 2 :
4
Sample Output 2 :
5 11 14 17

import java.util.Scanner;

public class Main
{
public static void main(String[] args)
{
Scanner s = new Scanner(System.in);
int n = s.nextInt();
int count = 0;
for(int i=1; count<n;i++)
{
int ap = 3*i+2;
if(ap%4!=0)
{
System.out.print(ap+" ");
count++;
}

}

}

}