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++;
}

}

}

}

Sum of Product

Sum or Product
Send Feedback

Write a program that asks the user for a number N and a choice C. And then give him the possibility to choose between computing the sum and computing the product of 1 ,..., N.

If user enters C is equal to -

 1 : Print sum of 1 to N numbers
 2 : Print product of 1 to N numbers
 Any other number : print -1
Input format :
Line 1 : Integer N
Line 2 : Choice C (1 or 2)
Output Format :
 Sum or product according to user's choice
Sample Input 1 :
10
1
Sample Output 1 :
55
Sample Input 2 :
10
2
Sample Output 2 :
3628800
Sample Input 3 :
10
4
Sample Output 3 :
-1

import java.util.Scanner;
public class Main
{
    public static void main(String[] args)
    {
        int i,sum=0,pro=1;
Scanner s = new Scanner(System.in);
     
      int n = s.nextInt();
     
     
      int n1 = s.nextInt();
     
    if(n1==1)
    {
   
    for(i=1;i<=n;i++)
{
sum=sum+i;
}
    System.out.println(sum);
    }
    else if(n1==2)
    {
    for(i=1;i<=n;i++)
{
pro=pro*i;


}
   
    System.out.println(pro);

    }
    else
 
    System.out.println(-1);


}
   
}

Number Pattern 4

Number Pattern 4
Send Feedback

Print the following pattern for the given N number of rows.

Pattern for N = 4
1234
123
12
1
Input format :
Integer N (Total no. of rows)
Output format :
Pattern in N lines
Sample Input :
5
Sample Output :
12345
1234
123
12
1

import java.util.Scanner;
public class Main
{

public static void main(String[] args)
{
    Scanner s = new Scanner(System.in);
int n = s.nextInt();
for(int i=1,r=n;i<=n;i++,r--)
{
for(int j=1;j<=r;j++)
{
System.out.print(j);
}
System.out.println();
}
}
}

Number Pattern 3

Number Pattern 3
Send Feedback

Print the following pattern for the given N number of rows.

Pattern for N = 4
1
11
121
1221
Input format :
Integer N (Total no. of rows)
Output format :
Pattern in N lines
Sample Input :
5
Sample Output :
1
11
121
1221
12221

import java.util.Scanner;

public class Main
{

public static void main(String[] args)

{
Scanner s = new Scanner(System.in);
int row = s.nextInt();

if(row>=1)
{
for(int i =0; i<row;i++)
{
for(int j=0;j<=i;j++)
{
if(i==0)
{
System.out.print("1");

}
else if(j==0 || j==i)
{
System.out.print(1);
}
else
{
System.out.print("2");

}

}
System.out.println();

}


}

}

}

Number Pattern 2

Number Pattern 2
Send Feedback

Print the following pattern for the given N number of rows.

Pattern for N = 4
1
11
202
3003
Input format :
Integer N (Total no. of rows)
Output format :
Pattern in N lines
Sample Input :
5
Sample Output :
1
11
202
3003
40004

import java.util.Scanner;

public class Main
{

public static void main(String[] args)

{
Scanner s = new Scanner(System.in);
int row = s.nextInt();

if(row>=1)
{
for(int i =0; i<row;i++)
{
for(int j=0
; j<=i;j++)
{
if(i==0)
{
System.out.print(1);

}
else if(j==0 || j==i)
{
System.out.print(i);
}
else
{
System.out.print(0);

}


}
System.out.println();

}
/*else
{


}*/

}

}

}

Number Pattern 1

Number Pattern 1
Send Feedback

Print the following pattern for the given N number of rows.

Pattern for N = 4
1
11
111
1111
Input format :
Integer N (Total no. of rows)
Output format :
Pattern in N lines
Sample Input :
5
Sample Output :
1
11
111
1111
11111

import java.util.Scanner;

public class Main
{

public static void main(String[] args)
{
   int i,j;
Scanner s=new Scanner(System.in);
int n = s.nextInt();
    for(i=1;i<=n;i++)
    {
    for(j=1;j<=i;j++)
    {
        System.out.print("1");
    }
    System.out.println();
       
    }
}
}

Find power of a number

Find power of a number
Send Feedback

Write a program to find x to the power n (i.e. x^n). Take x and n from the user. You need to print the answer.

Input format :
Two integers x and n (separated by space)
Output Format :
x^n (i.e. x raise to the power n)
Sample Input 1 :
 3 4
Sample Output 1 :
81
Sample Input 2 :
 2 5
Sample Output 2 :
32

import java.util.Scanner;

public class Main
{

public static void main(String[] args)
{
// TODO Auto-generated method stub
int pro=1;

//System.out.print("Enter the base number:");
Scanner s = new Scanner(System.in);
int b = s.nextInt();
// ask for exponent
//System.out.print("Enter the exponent:");
int e = s.nextInt();
//String exponent = s.nextLine();

for (int i=1;i<=e;i++)
{
pro = pro * b;

}
//System.out.println("the power of given number is"+pro);
System.out.println(pro);



}

}

Sum of even & odd

Sum of even & odd

Write a program to input an integer N and print the sum of all its even digits and sum of all its odd digits separately.

Digits means numbers not the places. That is, if the given integer is "13245", even digits are 2 & 4 and odd digits are 1, 3 & 5.
Input format :
 Integer N
Output format :
Sum_of_Even_Digits Sum_of_Odd_Digits
(Print first even sum and then odd sum separated by space)
Sample Input :
1234
Sample Output :

6 4
  1. import java.util.Scanner;
  2. public class Main 
  3. {
  4. public static void main(String[] args) 
  5. {
  6. // Write your code here
  7. int rem,odd=0,even=0,digit,input;
  8. Scanner s = new Scanner (System.in);
  9. int num = s.nextInt();
  10. while(num>0)
  11. {
  12.              digit = num % 10;
  13.              
  14.              num = num / 10;
  15.              
  16.              rem = digit % 2;
  17.              
  18.              if(rem != 0)
  19.              
  20.              odd=odd+digit;
  21.              
  22.              else
  23.              
  24.              even=even+digit;
  25.          }
  26. System.out.print(even);
  27. System.out.println(" "+odd);

  28. }
  29. }

Roots of quadratic equation

Roots of quadratic equation
Send Feedback

Write a program to calculate the roots of a given quadratic equation -

      a(x^2) + bx + c = 0 

Print roots specifying their nature. If roots are imaginary, no need to print the roots.

Print the nature of roots in the form of an integer -
 0 : if roots are real & same
 1 : if roots are real & different
-1 : if roots are imaginary
Round off the roots and then print the integral part only i.e. without any decimal places.
You can assume that, input will always be a quadratic equation.
Input format :
a b c (separated by space)
Output format :
Line 1 : Nature of roots (0 or 1 or -1)
Line 2 : Root 1 and Root 2 (separated by space)
Sample Input 1 :
1 4 2
Sample Output 1 :
1
-1 -3
Sample Input 2 :
1 2 3
Sample Output 2 :

-1
  1. import java.util.Scanner;
  2. public class Main
  3. {
  4. public static void main(String[] args) 
  5. {
  6. Scanner s = new Scanner (System.in);
  7. // System.out.println("enter the values a,b and c");
  8. int a = s.nextInt();
  9. int b = s.nextInt();
  10. int c = s.nextInt();
  11. double r1=0;
  12. double r2=0;
  13. int temp = b*b-4*a*c;
  14. r1 = (-b + Math.sqrt(temp)) / (2 * a);
  15. r2 = (-b - Math.sqrt(temp)) / (2 * a);
  16. if (temp<0)
  17. {
  18. System.out.println(-1);
  19. }
  20. else if(temp>0)
  21. {
  22. System.out.println(1);
  23. }
  24. else
  25. {
  26. System.out.println(0);
  27. }

  28. System.out.println();
  29. System.out.print(Math.round (r1)+" ");
  30. System.out.print(Math.round(r2));

  31. }
  32. }