codeparrot/xlcost-text-to-code huggingface dataset:
Divisibility by 3 where each digit is the sum of all prefix digits modulo 10 | Java code to check divisibility by 3 ; Function to check the divisibility ; Cycle ; no of residual terms ; sum of residual terms ; if no of residue term = 0 ; if no of residue term = 1 ; if no of residue term = 2 ; if no of residue term = 3 ; sum of all digits ; divisibility check ; Code driven
import java . util . * ; import java . io . * ; class GFG { static String check ( int k , int d0 , int d1 ) { int s = ( 2 * ( d0 + d1 ) ) % 10 + ( 4 * ( d0 + d1 ) ) % 10 + ( 8 * ( d0 + d1 ) ) % 10 + ( 6 * ( d0 + d1 ) ) % 10 ; int a = ( k - 3 ) % 4 ; int x = 0 ; switch ( a ) { case 0 : x = 0 ; break ; case 1 : x = ( 2 * ( d0 + d1 ) ) % 10 ; break ; case 2 : x = ( 2 * ( d0 + d1 ) ) % 10 + ( 4 * ( d0 + d1 ) ) % 10 ; break ; case 3 : x = ( 2 * ( d0 + d1 ) ) % 10 + ( 4 * ( d0 + d1 ) ) % 10 + ( 8 * ( d0 + d1 ) ) % 10 ; break ; } int sum = d0 + d1 + ( ( ( k - 3 ) / 4 ) * s + x ) ; if ( sum % 3 == 0 ) return " YES " ; return " NO " ; } public static void main ( String [ ] args ) { int k , d0 , d1 ; k = 13 ; d0 = 8 ; d1 = 1 ; System . out . println ( check ( k , d0 , d1 ) ) ; k = 5 ; d0 = 3 ; d1 = 4 ; System . out . println ( check ( k , d0 , d1 ) ) ; } }
Find ceil of a / b without using ceil ( ) function | Java program to find ceil ( a / b ) without using ceil ( ) function ; Driver Code ; taking input 1 ; example of perfect division taking input 2
import java . io . * ; class GFG { public static void main ( String args [ ] ) { int a = 4 ; int b = 3 , val = 0 ; if ( ( a % b ) != 0 ) val = ( a / b ) + ( a % b ) ; else val = ( a / b ) ; System . out . println ( " The ▁ ceiling ▁ " + " value ▁ of ▁ 4/3 ▁ is ▁ " + val ) ; a = 6 ; b = 3 ; if ( ( a % b ) != 0 ) val = ( a / b ) + ( a % b ) ; else val = ( a / b ) ; System . out . println ( " The ▁ ceiling ▁ " + " value ▁ of ▁ 6/3 ▁ is ▁ " + val ) ; } }
Program to print Collatz Sequence | Java program to print Collatz sequence ; We simply follow steps while we do not reach 1 ; If n is odd ; If even ; Print 1 at the end ; Driver code
import java . io . * ; class GFG { static void printCollatz ( int n ) { while ( n != 1 ) { System . out . print ( n + " ▁ " ) ; if ( ( n & 1 ) == 1 ) n = 3 * n + 1 ; else n = n / 2 ; } System . out . print ( n ) ; } public static void main ( String [ ] args ) { printCollatz ( 6 ) ; } }
Powers of 2 to required sum | Java program to find the blocks for given number . ; Convert decimal number to its binary equivalent ; Displaying the output when the bit is '1' in binary equivalent of number . ; Driver Code
import java . util . * ; class GFG { static void block ( long x ) { ArrayList < Integer > v = new ArrayList < Integer > ( ) ; System . out . print ( " Blocks ▁ for ▁ " + x + " ▁ : ▁ " ) ; while ( x > 0 ) { v . add ( ( int ) x % 2 ) ; x = x / 2 ; } for ( int i = 0 ; i < v . size ( ) ; i ++ ) { if ( v . get ( i ) == 1 ) { System . out . print ( i ) ; if ( i != v . size ( ) - 1 ) System . out . print ( " , ▁ " ) ; } } System . out . println ( ) ; } public static void main ( String args [ ] ) { block ( 71307 ) ; block ( 1213 ) ; block ( 29 ) ; block ( 100 ) ; } }
Given a number N in decimal base , find number of its digits in any base ( base b ) | Java program to Find Number of digits in base b . ; function to print number of digits ; Calculating log using base changing property and then taking it floor and then adding 1. ; printing output ; Driver method ; taking inputs ; calling the method
class GFG { static void findNumberOfDigits ( long n , int base ) { int dig = ( int ) ( Math . floor ( Math . log ( n ) / Math . log ( base ) ) + 1 ) ; System . out . println ( " The ▁ Number ▁ of ▁ digits ▁ of ▁ Number ▁ " + n + " ▁ in ▁ base ▁ " + base + " ▁ is ▁ " + dig ) ; } public static void main ( String [ ] args ) { long n = 1446 ; int base = 7 ; findNumberOfDigits ( n , base ) ; } }
Nesbitt 's Inequality | Java code to verify Nesbitt 's Inequality ; 3 parts of the inequality sum ; Driver code
class GFG { static boolean isValidNesbitt ( double a , double b , double c ) { double A = a / ( b + c ) ; double B = b / ( a + c ) ; double C = c / ( a + b ) ; double inequality = A + B + C ; return ( inequality >= 1.5 ) ; } public static void main ( String args [ ] ) { double a = 1.0 , b = 2.0 , c = 3.0 ; if ( isValidNesbitt ( a , b , c ) == true ) { System . out . print ( " Nesbitt ' s ▁ inequality " + " ▁ satisfied . " ) ; System . out . println ( " for ▁ real ▁ numbers ▁ " + a + " , ▁ " + b + " , ▁ " + c ) ; } else System . out . println ( " Nesbitts ▁ inequality " + " ▁ not ▁ satisfied " ) ; } }
Ordered Prime Signature | JAVA to find total number of divisors of a number , using ordered prime signature ; Finding primes upto entered number ; Finding primes by Sieve of Eratosthenes method ; If prime [ i ] is not changed , then it is prime ; Update all multiples of p ; Forming array of the prime numbers found ; Finding ordered prime signature of the number ; Map to store prime factors and the related exponents ; Declaring an iterator for map ; Finding prime factorization of the number ; Storing the prime factor and its exponent in map ; Storing the exponent in a vector ; Sorting the stored exponents ; Printing the prime signature ; Finding total number of divisors of the number ; Adding one to each element present ; Multiplying the elements ; Driver code
import java . util . * ; class GFG { static class pair { int first , second ; public pair ( int first , int second ) { this . first = first ; this . second = second ; } } static Vector < Integer > primes ( int n ) { boolean [ ] prime = new boolean [ n + 1 ] ; Arrays . fill ( prime , true ) ; for ( int i = 2 ; i * i <= n ; i ++ ) { if ( prime [ i ] == true ) { for ( int j = i * 2 ; j <= n ; j += i ) prime [ j ] = false ; } } Vector < Integer > arr = new Vector < > ( ) ; for ( int i = 2 ; i <= n ; i ++ ) { if ( prime [ i ] ) arr . add ( i ) ; } return arr ; } static Vector < Integer > signature ( int n ) { Vector < Integer > r = primes ( n ) ; HashMap < Integer , Integer > factor = new HashMap < > ( ) ; HashMap < Integer , Integer > :: iterator it ; Vector < Integer > sort_exp = new Vector < > ( ) ; int k , t = n ; int it = 0 ; for ( int i = 0 ; i < r . size ( ) ; i ++ ) { if ( n % r . get ( i ) == 0 ) { k = 0 ; while ( n % r . get ( i ) == 0 ) { n = n / r . get ( i ) ; k ++ ; } factor . put ( r . get ( i ) , k ) ; sort_exp . add ( k ) ; } } Collections . sort ( sort_exp ) ; System . out . print ( " ▁ The ▁ Ordered ▁ Prime ▁ Signature ▁ of ▁ " + t + " is :  { "); for ( int i = 0 ; i < sort_exp . size ( ) ; i ++ ) { if ( i != sort_exp . size ( ) - 1 ) System . out . print ( sort_exp . get ( i ) + " , ▁ " ) ; else System . out . print ( sort_exp . get ( i ) + " }NEW_LINE"); } return sort_exp ; } static void divisors ( int n ) { int f = 1 , l ; Vector < Integer > div = signature ( n ) ; l = div . size ( ) ; for ( int i = 0 ; i < l ; i ++ ) { f *= ( div . get ( i ) + 1 ) ; } System . out . print ( " The ▁ total ▁ number ▁ of ▁ divisors ▁ of ▁ " + n + " ▁ is ▁ " + f + "NEW_LINE"); } public static void main ( String [ ] args ) { int n = 13 ; divisors ( n ) ; } }
Cube Free Numbers smaller than n | Efficient Java Program to print all cube free numbers smaller than or equal to n . ; Initialize all numbers as not cube free ; Traverse through all possible cube roots ; If i itself is cube free ; Mark all multiples of i as not cube free ; Print all cube free numbers ; Driver code
class GFG { public static void printCubeFree ( int n ) { boolean [ ] cubFree = new boolean [ n + 1 ] ; for ( int i = 0 ; i <= n ; i ++ ) cubFree [ i ] = true ; for ( int i = 2 ; i * i * i <= n ; i ++ ) { if ( cubFree [ i ] ) { for ( int multiple = 1 ; i * i * i * multiple <= n ; multiple ++ ) { cubFree [ i * i * i * multiple ] = false ; } } } for ( int i = 2 ; i <= n ; i ++ ) { if ( cubFree [ i ] == true ) System . out . print ( i + " ▁ " ) ; } } public static void main ( String [ ] args ) { printCubeFree ( 20 ) ; } }
Heap Sort for decreasing order using min heap | Java program for implementation of Heap Sort ; To heapify a subtree rooted with node i which is an index in arr [ ] . n is size of heap ; Initialize smalles as root ; left = 2 * i + 1 ; right = 2 * i + 2 ; If left child is smaller than root ; If right child is smaller than smallest so far ; If smallest is not root ; Recursively heapify the affected sub - tree ; main function to do heap sort ; Build heap ( rearrange array ) ; One by one extract an element from heap ; Move current root to end ; call max heapify on the reduced heap ; A utility function to print array of size n ; Driver program
import java . io . * ; class GFG { static void heapify ( int arr [ ] , int n , int i ) { int smallest = i ; int l = 2 * i + 1 ; int r = 2 * i + 2 ; if ( l < n && arr [ l ] < arr [ smallest ] ) smallest = l ; if ( r < n && arr [ r ] < arr [ smallest ] ) smallest = r ; if ( smallest != i ) { int temp = arr [ i ] ; arr [ i ] = arr [ smallest ] ; arr [ smallest ] = temp ; heapify ( arr , n , smallest ) ; } } static void heapSort ( int arr [ ] , int n ) { for ( int i = n / 2 - 1 ; i >= 0 ; i -- ) heapify ( arr , n , i ) ; for ( int i = n - 1 ; i >= 0 ; i -- ) { int temp = arr [ 0 ] ; arr [ 0 ] = arr [ i ] ; arr [ i ] = temp ; heapify ( arr , i , 0 ) ; } } static void printArray ( int arr [ ] , int n ) { for ( int i = 0 ; i < n ; ++ i ) System . out . print ( arr [ i ] + " ▁ " ) ; System . out . println ( ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 4 , 6 , 3 , 2 , 9 } ; int n = arr . length ; heapSort ( arr , n ) ; System . out . println ( " Sorted ▁ array ▁ is ▁ " ) ; printArray ( arr , n ) ; } }
Squared triangular number ( Sum of cubes ) | Java program to check if a given number is sum of cubes of natural numbers . import java . Math . * ; ; Returns root of n ( n + 1 ) / 2 = num if num is triangular ( orinteger root exists ) . Else returns - 1. ; Considering the equation n * ( n + 1 ) / 2 = num . The equation is : a ( n ^ 2 ) + bn + c = 0 "; ; Find roots of equation ; checking if root1 is natural ; checking if root2 is natural ; Returns square root of x if it is perfect square . Else returns - 1. ; Find floating point value of square root of x . ; If square root is an integer ; Function to find if the given number is sum of the cubes of first n natural numbers ; Driver code
class GFG { public static int isTriangular ( int num ) { if ( num < 0 ) return 0 ; int c = ( - 2 * num ) ; int b = 1 , a = 1 ; int d = ( b * b ) - ( 4 * a * c ) ; if ( d < 0 ) return - 1 ; double root1 = ( - b + Math . sqrt ( d ) ) / ( 2 * a ) ; double root2 = ( - b - Math . sqrt ( d ) ) / ( 2 * a ) ; if ( ( int ) ( root1 ) > 0 && ( int ) ( Math . floor ( root1 ) ) == ( int ) ( root1 ) ) return ( int ) ( root1 ) ; if ( ( int ) ( root2 ) > 0 && ( int ) ( Math . floor ( root2 ) ) == ( int ) ( root2 ) ) return ( int ) ( root2 ) ; return - 1 ; } static int isPerfectSquare ( double x ) { double sr = Math . sqrt ( x ) ; if ( ( sr - Math . floor ( sr ) ) == 0 ) return ( int ) ( Math . floor ( sr ) ) ; else return - 1 ; } static int findS ( int s ) { int sr = isPerfectSquare ( s ) ; if ( sr == - 1 ) return - 1 ; return isTriangular ( sr ) ; } public static void main ( String [ ] args ) { int s = 9 ; int n = findS ( s ) ; if ( n == - 1 ) System . out . println ( " - 1" ) ; else System . out . println ( n ) ; } }
Smallest even digits number not less than N | Java program to print the smallest integer not less than N with all even digits ; function to return the answer when the first odd digit is 9 ; traverse towwars the left to find the non - 8 digit ; index digit ; if digit is not 8 , then break ; if on the left side of the '9' , no 8 is found then we return by adding a 2 and 0 's ; till non - 8 digit add all numbers ; if non - 8 is even or odd than add the next even . ; add 0 to right of 9 ; function to return the smallest number with all digits even ; convert the number to string to perform operations ; find out the first odd number ; if no odd numbers are there , than n is the answer ; if the odd number is 9 , than tricky case handles it ; add all digits till first odd ; increase the odd digit by 1 ; add 0 to the right of the odd number ; Driver Code
import java . io . * ; import java . util . * ; import java . lang . * ; class GFG { static int trickyCase ( String s , int index ) { int index1 = - 1 ; for ( int i = index - 1 ; i >= 0 ; i -- ) { int digit = s . charAt ( i ) - '0' ; if ( digit != 8 ) { index1 = i ; break ; } } if ( index1 == - 1 ) return 2 * ( int ) Math . pow ( 10 , s . length ( ) ) ; int num = 0 ; for ( int i = 0 ; i < index1 ; i ++ ) num = num * 10 + ( s . charAt ( i ) - '0' ) ; if ( s . charAt ( index1 ) % 2 == 0 ) num = num * 10 + ( s . charAt ( index1 ) - '0' + 2 ) ; else num = num * 10 + ( s . charAt ( index1 ) - '0' + 1 ) ; for ( int i = index1 + 1 ; i < s . length ( ) ; i ++ ) num = num * 10 ; return num ; } static int smallestNumber ( int n ) { int num = 0 ; String s = " " ; int duplicate = n ; while ( n > 0 ) { s = ( char ) ( n % 10 + 48 ) + s ; n /= 10 ; } int index = - 1 ; for ( int i = 0 ; i < s . length ( ) ; i ++ ) { int digit = s . charAt ( i ) - '0' ; int val = digit & 1 ; if ( val == 1 ) { index = i ; break ; } } if ( index == - 1 ) return duplicate ; if ( s . charAt ( index ) == '9' ) { num = trickyCase ( s , index ) ; return num ; } for ( int i = 0 ; i < index ; i ++ ) num = num * 10 + ( s . charAt ( i ) - '0' ) ; num = num * 10 + ( s . charAt ( index ) - '0' + 1 ) ; for ( int i = index + 1 ; i < s . length ( ) ; i ++ ) num = num * 10 ; return num ; } public static void main ( String args [ ] ) { int N = 2397 ; System . out . print ( smallestNumber ( N ) ) ; } }
n | Java program to find n - th number with sum of digits as 10. ; Find sum of digits in current no . ; If sum is 10 , we increment count ; If count becomes n , we return current number . ; Driver Code
import java . util . * ; import java . lang . * ; public class GFG { public static int findNth ( int n ) { int count = 0 ; for ( int curr = 19 ; ; curr += 9 ) { int sum = 0 ; for ( int x = curr ; x > 0 ; x = x / 10 ) sum = sum + x % 10 ; if ( sum == 10 ) count ++ ; if ( count == n ) return curr ; } } public static void main ( String [ ] args ) { System . out . print ( findNth ( 5 ) ) ; } }
Sum of pairwise products | Simple Java program to find sum of given series . ; Driver code
class GFG { static int findSum ( int n ) { int sum = 0 ; for ( int i = 1 ; i <= n ; i ++ ) for ( int j = i ; j <= n ; j ++ ) sum = sum + i * j ; return sum ; } public static void main ( String [ ] args ) { int n = 5 ; System . out . println ( findSum ( n ) ) ; } }
Sum of pairwise products | Efficient Java program to find sum of given series . ; Sum of multiples of 1 is 1 * ( 1 + 2 + . . ) ; Adding sum of multiples of numbers other than 1 , starting from 2. ; Subtract previous number from current multiple . ; For example , for 2 , we get sum as ( 2 + 3 + 4 + ... . ) * 2 ; Driver code
class GFG { static int findSum ( int n ) { int multiTerms = n * ( n + 1 ) / 2 ; int sum = multiTerms ; for ( int i = 2 ; i <= n ; i ++ ) { multiTerms = multiTerms - ( i - 1 ) ; sum = sum + multiTerms * i ; } return sum ; } public static void main ( String [ ] args ) { int n = 5 ; System . out . println ( findSum ( n ) ) ; } }
Lemoine 's Conjecture | Java code to verify Lemoine 's Conjecture for any odd number >= 7 ; Function to check if a number is prime or not ; Representing n as p + ( 2 * q ) to satisfy lemoine 's conjecture ; Declaring a map to hold pairs ( p , q ) ; Declaring an iterator for map HashMap < Integer , Integer > :: iterator it ; it = pr . begin ( ) ; ; Finding various values of p for each q to satisfy n = p + ( 2 * q ) ; After finding a pair that satisfies the equation , check if both p and q are prime or not ; If both p and q are prime , store them in the map ; Displaying all pairs ( p , q ) that satisfy lemoine ' s ▁ conjecture ▁ for ▁ the ▁ number ▁ ' n ' ; Driver Function ; Function calling
import java . util . * ; class GFG { static class pair { int first , second ; public pair ( int first , int second ) { this . first = first ; this . second = second ; } } static boolean isPrime ( int n ) { if ( n < 2 ) return false ; for ( int i = 2 ; i <= Math . sqrt ( n ) ; i ++ ) { if ( n % i == 0 ) return false ; } return true ; } static void lemoine ( int n ) { HashMap < Integer , pair > pr = new HashMap < > ( ) ; int i = 0 ; for ( int q = 1 ; q <= n / 2 ; q ++ ) { int p = n - 2 * q ; if ( isPrime ( p ) && isPrime ( q ) ) pr . put ( i , new pair ( p , q ) ) ; i ++ ; } for ( Map . Entry < Integer , pair > it : pr . entrySet ( ) ) System . out . print ( n + " ▁ = ▁ " + it . getValue ( ) . first + " ▁ + ▁ ( 2 ▁ * ▁ " + it . getValue ( ) . second + ")NEW_LINE"); } public static void main ( String [ ] args ) { int n = 39 ; System . out . print ( n + " ▁ can ▁ be ▁ expressed ▁ as ▁ " + "NEW_LINE"); lemoine ( n ) ; } }
Sum of n digit numbers divisible by a given number | Efficient Java program to find the sum divisible numbers . ; find the Sum of having n digit and divisible by the number ; compute the first and last term ; first number which is divisible by given number ; last number which is divisible by given number ; total divisible number ; return the total sum ; Driver code
import java . io . * ; class GFG { static int totalSumDivisibleByNum ( int digit , int number ) { int firstnum = ( int ) Math . pow ( 10 , digit - 1 ) ; int lastnum = ( int ) Math . pow ( 10 , digit ) ; firstnum = ( firstnum - firstnum % number ) + number ; lastnum = ( lastnum - lastnum % number ) ; int count = ( ( lastnum - firstnum ) / number + 1 ) ; return ( ( lastnum + firstnum ) * count ) / 2 ; } public static void main ( String [ ] args ) { int n = 3 , number = 7 ; System . out . println ( totalSumDivisibleByNum ( n , number ) ) ; } }
Program for N | Java program to find nth term of Arithmetic progression ; using formula to find the Nth term t ( n ) = a ( 1 ) + ( n - 1 ) * d ; Driver code ; starting number ; Common difference ; N th term to be find ; Display the output
import java . io . * ; import java . lang . * ; class GFG { public static int Nth_of_AP ( int a , int d , int N ) { return ( a + ( N - 1 ) * d ) ; } public static void main ( String [ ] args ) { int a = 2 ; int d = 1 ; int N = 5 ; System . out . print ( " The ▁ " + N + " th ▁ term ▁ of ▁ the ▁ series ▁ is ▁ : ▁ " + Nth_of_AP ( a , d , N ) ) ; } }
Fibbinary Numbers ( No consecutive 1 s in binary ) | Java program to check if a number is fibinnary number or not ; function to check if binary representation of an integer has consecutive 1 s ; stores the previous last bit initially as 0 ; if current last bit and previous last bit is 1 ; stores the last bit ; right shift the number ; Driver code to check above function
class GFG { static boolean checkFibinnary ( int n ) { int prev_last = 0 ; while ( n != 0 ) { if ( ( n & 1 ) != 0 && prev_last != 0 ) return false ; prev_last = n & 1 ; n >>= 1 ; } return true ; } public static void main ( String [ ] args ) { int n = 10 ; if ( checkFibinnary ( n ) == true ) System . out . println ( " YES " ) ; else System . out . println ( " NO " ) ; } }
Sum of the series 5 + 55 + 555 + . . up to n terms | Java program for sum of the series 5 + 55 + 555. . ... n ; function which return the the sum of series ; Driver code
class GFG { static int sumOfSeries ( int n ) { return ( int ) ( 0.6172 * ( Math . pow ( 10 , n ) - 1 ) - 0.55 * n ) ; } public static void main ( String [ ] args ) { int n = 2 ; System . out . println ( sumOfSeries ( n ) ) ; } }
Nonagonal number | Java Program to find nth nonagonal number . ; Function to find nth nonagonal number . ; Formula to find nth nonagonal number . ; Driver function .
import java . io . * ; class GFG { static int Nonagonal ( int n ) { return n * ( 7 * n - 5 ) / 2 ; } public static void main ( String args [ ] ) { int n = 10 ; System . out . println ( Nonagonal ( n ) ) ; } }
Check if a large number is divisible by 20 | Java program to check if a large n number is divisible by 20. ; Get number with last two digits ; Check if the number formed by last two digits is divisible by 5 and 4. ; Driver Program
import java . io . * ; class GFG { static Boolean divisibleBy20 ( String num ) { int lastTwoDigits = Integer . parseInt ( num . substring ( num . length ( ) - 2 , num . length ( ) ) ) ; return ( ( lastTwoDigits % 5 == 0 ) && ( lastTwoDigits % 4 == 0 ) ) ; } public static void main ( String [ ] args ) { String num = "63284689320" ; if ( divisibleBy20 ( num ) == true ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }
Divisibility by 12 for a large number | Java Program to check if number is divisible by 12 ; if number greater then 3 ; find last digit ; no is odd ; find second last digit ; find sum of all digits ; if number is less then or equal to 100 ; driver function
import java . io . * ; class GFG { static boolean isDvisibleBy12 ( String num ) { if ( num . length ( ) >= 3 ) { int d1 = ( int ) num . charAt ( num . length ( ) - 1 ) ; if ( d1 % 2 != 0 ) return false ; int d2 = ( int ) num . charAt ( num . length ( ) - 2 ) ; int sum = 0 ; for ( int i = 0 ; i < num . length ( ) ; i ++ ) sum += num . charAt ( i ) ; return ( sum % 3 == 0 && ( d2 * 10 + d1 ) % 4 == 0 ) ; } else { int number = Integer . parseInt ( num ) ; return ( number % 12 == 0 ) ; } } public static void main ( String [ ] args ) { String num = "12244824607284961224" ; if ( isDvisibleBy12 ( num ) ) System . out . print ( " Yes " ) ; else System . out . print ( " No " ) ; } }
Largest number that is not a perfect square | Java program to find the largest non perfect square number among n numbers ; takes the sqrt of the number ; checks if it is a perfect square number ; function to find the largest non perfect square number ; stores the maximum of all non perfect square numbers ; traverse for all elements in the array ; store the maximum if not a perfect square ; driver code to check the above functions ; function call
import java . io . * ; class GfG { static Boolean check ( int n ) { int d = ( int ) Math . sqrt ( n ) ; if ( d * d == n ) return true ; return false ; } static int largestNonPerfectSquareNumber ( int a [ ] , int n ) { int maxi = - 1 ; for ( int i = 0 ; i < n ; i ++ ) { if ( ! check ( a [ i ] ) ) maxi = Math . max ( a [ i ] , maxi ) ; } return maxi ; } public static void main ( String [ ] args ) { int a [ ] = { 16 , 20 , 25 , 2 , 3 , 10 } ; int n = a . length ; System . out . println ( largestNonPerfectSquareNumber ( a , n ) ) ; } }
Program to print Arithmetic Progression series | Java Program to print an arithmetic progression series ; Printing AP by simply adding d to previous term . ; Driver code ; starting number ; Common difference ; N th term to be find
class GFG { static void printAP ( int a , int d , int n ) { int curr_term ; curr_term = a ; for ( int i = 1 ; i <= n ; i ++ ) { System . out . print ( curr_term + " ▁ " ) ; curr_term = curr_term + d ; } } public static void main ( String [ ] args ) { int a = 2 ; int d = 1 ; int n = 5 ; printAP ( a , d , n ) ; } }
Program to print non square numbers | Java program to print first n non - square numbers . ; Print curr_count numbers . curr_count is current gap between two square numbers . ; skip a square number . ; Count of next non - square numbers is next even number . ; Driver code
import java . io . * ; import java . math . * ; class GFG { static void printNonSquare ( int n ) { int curr_count = 2 , num = 2 , count = 0 ; while ( count < n ) { for ( int i = 0 ; i < curr_count && count < n ; i ++ ) { System . out . print ( num + " ▁ " ) ; count ++ ; num ++ ; } num ++ ; curr_count += 2 ; } } public static void main ( String args [ ] ) { int n = 10 ; printNonSquare ( n ) ; } }