Skip to main content

Posts

Showing posts with the label oracle

Java 8 coding challenge: Find Product

Problem: You have been given an array   A of size   N consisting of positive integers. You need to find and print the product of all the number in this array  Modulo     10 9 +7 . Input Format : The first line contains a single integer   N N  denoting the size of the array. The next line contains   N N  space separated integers denoting the elements of the array Output Format : Print a single integer denoting the product of all the elements of the array Modulo    10 9 +7 . Constraints :  1≤N≤10 3 1≤A[i]≤10 3 SAMPLE INPUT 5 1 2 3 4 5 SAMPLE OUTPUT   120 Code: import java.lang.Math; import java.util.Scanner; class TestClass { public static void main(String args[] ) throws Exception { Scanner sc=new Scanner(System.in); int []A=new int[1000]; int a=sc.nextInt(); int i; double answer=1.0; for(i=0;i<a;i++) { A[i]=sc.nextInt(); } for(i=0;i<a;i++...

Java 8 coding challenge: Find Product

Problem: You have been given an array A of size N consisting of positive integers. You need to find and print the product of all the number in this array Modulo   10 9 +7 . Input Format : The first line contains a single integer N N denoting the size of the array. The next line contains N N space separated integers denoting the elements of the array Output Format : Print a single integer denoting the product of all the elements of the array Modulo 10 9 +7 . Constraints : 1≤N≤10 3 1≤A[i]≤10 3   SAMPLE INPUT 5 1 2 3 4 5 SAMPLE OUTPUT 120 Code: import java.lang.Math; import java.util.Scanner; class TestClass { public static void main(String args[] ) throws Exception { Scanner sc=new Scanner(System.in); int []A=new int[1000]; int a=sc.nextInt(); int i; double answer=1.0; for(i=0;i<a;i++) { A[i]=sc.nextInt(); } for(i=0;i<a;i++) { answer=(A[i]*answer)%((Math.pow(10,9))+7); } System.out.println((int)answer); } }

Java 8 coding challenge: Palindromic String

Problem: You have been given a String  S S . You need to find and print whether this string is a palindrome or not. If yes, print "YES" (without quotes), else print "NO" (without quotes). Input Format The first and only line of input contains the String  S S . The String shall consist of lowercase English alphabets only. Output Format Print the required answer on a single line. Constraints   1 ≤ | S | ≤ 100 1≤|S|≤100 Note String  S S  consists of lowercase English Alphabets only. SAMPLE INPUT     aba SAMPLE OUTPUT     YES Code: /* IMPORTANT: Multiple classes and nested static classes are supported */ /* * uncomment this if you want to read input. //imports for BufferedReader import java.io.BufferedReader; import java.io.InputStreamReader; */ //import for Scanner and other utility classes import java.util.*; class TestClass { public static void main(String args[] ) throws Exception { /* * Read ...

Java 8 coding challenge: Palindromic String

Problem: You have been given a String S S . You need to find and print whether this string is a palindrome or not. If yes, print "YES" (without quotes), else print "NO" (without quotes). Input Format The first and only line of input contains the String S S . The String shall consist of lowercase English alphabets only. Output Format Print the required answer on a single line. Constraints 1 ≤ | S | ≤ 100 1≤|S|≤100 Note String S S consists of lowercase English Alphabets only. SAMPLE INPUT aba SAMPLE OUTPUT YES Code: /* IMPORTANT: Multiple classes and nested static classes are supported */ /* * uncomment this if you want to read input. //imports for BufferedReader import java.io.BufferedReader; import java.io.InputStreamReader; */ //import for Scanner and other utility classes import java.util.*; class TestClass { public static void main(String args[] ) throws Exception { /* * Read input from stdin and provide input before running * Use either of these method...

Java 8 coding challenge: Toggle String

Problem: You have been given a String S S consisting of uppercase and lowercase English alphabets. You need to change the case of each alphabet in this String. That is, all the uppercase letters should be converted to lowercase and all the lowercase letters should be converted to uppercase. You need to then print the resultant String to output. Input Format The first and only line of input contains the String S S Output Format Print the resultant String on a single line. Constraints 1 ≤ | S | ≤ 100 1≤|S|≤100 where | S | |S| denotes the length of string S S .   SAMPLE INPUT abcdE SAMPLE OUTPUT ABCDe   Code: import java.util.Scanner; import java.lang.StringBuilder; class TestClass { public static void main(String args[] ) throws Exception { Scanner sc= new Scanner(System.in); String s,s1,sx; char[] temp=new char[100]; s=sc.nextLine(); int x=s.length(); for(int i=0;i<x;i++){ char c = s.charAt(i); s1=Character.toString(c); if(Character.isLowerCase(c)){ sx=s1.toUp...

Java 8 coding challenge: Toggle String

Problem: You have been given a String  S S  consisting of uppercase and lowercase English alphabets. You need to change the case of each alphabet in this String. That is, all the uppercase letters should be converted to lowercase and all the lowercase letters should be converted to uppercase. You need to then print the resultant String to output. Input Format The first and only line of input contains the String  S S Output Format Print the resultant String on a single line. Constraints 1 ≤ | S | ≤ 100 1≤|S|≤100   where  | S | |S|  denotes the length of string  S S . SAMPLE INPUT abcdE SAMPLE OUTPUT ABCDe Code: import java.util.Scanner; import java.lang.StringBuilder; class TestClass { public static void main(String args[] ) throws Exception { Scanner sc= new Scanner(System.in); String s,s1,sx; char[] temp=new char[100]; s=sc.nextLine(); int x=s.length(); for(int i=0;i<x;i++){ char c = s.charAt(i); s1=C...

Java 8 coding challenge: Magical Word

Problem: Dhananjay has recently learned about  ASCII  values.He is very fond of experimenting. With his knowledge of  ASCII  values and character he has developed a special word and named it Dhananjay's Magical word. A word which consist of alphabets whose  ASCII  values is a prime number is an Dhananjay's Magical word. An alphabet is Dhananjay's Magical alphabet if its  ASCII  value is prime. Dhananjay's nature is to boast about the things he know or have learnt about. So just to defame his friends he gives few string to his friends and ask them to convert it to Dhananjay's Magical word. None of his friends would like to get insulted. Help them to convert the given strings to Dhananjay's Magical Word. Rules for converting: 1.Each character should be replaced by the nearest Dhananjay's Magical alphabet. 2.If the character is equidistant with 2 Magical alphabets. The one with lower  ASCII  value will be considered as its re...

Java 8 coding challenge: Magical Word

Problem: Dhananjay has recently learned about ASCII values.He is very fond of experimenting. With his knowledge of ASCII values and character he has developed a special word and named it Dhananjay's Magical word. A word which consist of alphabets whose ASCII values is a prime number is an Dhananjay's Magical word. An alphabet is Dhananjay's Magical alphabet if its ASCII value is prime. Dhananjay's nature is to boast about the things he know or have learnt about. So just to defame his friends he gives few string to his friends and ask them to convert it to Dhananjay's Magical word. None of his friends would like to get insulted. Help them to convert the given strings to Dhananjay's Magical Word. Rules for converting: 1.Each character should be replaced by the nearest Dhananjay's Magical alphabet. 2.If the character is equidistant with 2 Magical alphabets. The one with lower ASCII value will be considered as its replacement. Input format: First line of in...

Java 8 Coding challenge: Factorial!

Problem: You have been given a positive integer N. You need to find and print the Factorial of this number. The Factorial of a positive integer N refers to the product of all number in the range from 1 to N.  Input Format: The first and only line of the input contains a single integer N denoting the number whose factorial you need to find. Output Format Output a single line denoting the factorial of the number N. Constraints 1≤N≤101≤N≤10 SAMPLE INPUT  2 SAMPLE OUTPUT  2 Code: import java.util.*; class TestClass { public static void main(String args[] ) throws Exception { try (Scanner input = new Scanner(System.in)) { int fatorial = input.nextInt(); int total=fatorial; for(int i=fatorial-1;i>=1;i--){ total = total * i; } System.out.println(total); } catch (Throwable e) { e.printStackTrace(); } } }

Java 8 Coding challenge: Factorial!

Problem: You have been given a positive integer N. You need to find and print the Factorial of this number. The Factorial of a positive integer N refers to the product of all number in the range from 1 to N. Input Format: The first and only line of the input contains a single integer N denoting the number whose factorial you need to find. Output Format Output a single line denoting the factorial of the number N. Constraints 1≤N≤101≤N≤10 SAMPLE INPUT 2 SAMPLE OUTPUT 2 Code: import java.util.*; class TestClass { public static void main(String args[] ) throws Exception { try (Scanner input = new Scanner(System.in)) { int fatorial = input.nextInt(); int total=fatorial; for(int i=fatorial-1;i>=1;i--){ total = total * i; } System.out.println(total); } catch (Throwable e) { e.printStackTrace(); } } }  

Java 8 coding challenge: Roy and Profile Picture

Problem:  Roy wants to change his profile picture on Facebook. Now Facebook has some restriction over the dimension of picture that we can upload. Minimum dimension of the picture can be  L x L , where  L  is the length of the side of square. Now Roy has  N  photos of various dimensions. Dimension of a photo is denoted as  W x H where  W  - width of the photo and  H  - Height of the photo When any photo is uploaded following events may occur: [1] If any of the width or height is less than L, user is prompted to upload another one. Print " UPLOAD ANOTHER " in this case. [2] If width and height, both are large enough and (a) if the photo is already square then it is accepted. Print " ACCEPTED " in this case. (b) else user is prompted to crop it. Print " CROP IT " in this case. (quotes are only for clarification) Given L, N, W and H as input, print appropriate text as output. Input: First line contains  L . Second...

Java 8 coding challenge: Roy and Profile Picture

Problem: Roy wants to change his profile picture on Facebook. Now Facebook has some restriction over the dimension of picture that we can upload. Minimum dimension of the picture can be L x L , where L is the length of the side of square. Now Roy has N photos of various dimensions. Dimension of a photo is denoted as W x H where W - width of the photo and H - Height of the photo When any photo is uploaded following events may occur: [1] If any of the width or height is less than L, user is prompted to upload another one. Print " UPLOAD ANOTHER " in this case. [2] If width and height, both are large enough and (a) if the photo is already square then it is accepted. Print " ACCEPTED " in this case. (b) else user is prompted to crop it. Print " CROP IT " in this case. (quotes are only for clarification) Given L, N, W and H as input, print appropriate text as output. Input: First line contains L . Second line contains N , number of photos. Following N lines...