Java Programs

jmks stands for java me kya seekha 

 print Fibonacci series in Java:

Method 1 – Iterative:

jmks : Integer input lena aur void ke phle static lagana
            Scanner sc=new Scanner(System.in);  //System.in is a standard input stream
int n= sc.nextInt();
  static void PrintFibonacci(int N)
Code:
import java.util.Scanner;

public class Tmp {

// Function to print N Fibonacci Number
static void PrintFibonacci(int N)
{
int num1 = 0, num2 = 1;

int counter = 0;

// Iterate till counter is N
while (counter < N) {

// Print the number
System.out.print(num1 + " ");

// Swap
int num3 = num2 + num1;
num1 = num2;
num2 = num3;
counter = counter + 1;
}
}

public static void main(String args[]){
Scanner sc=new Scanner(System.in); //System.in is a standard input stream
int n= sc.nextInt();

PrintFibonacci(n);
}
}

Method 2 – Using Recursion:
jmks :function bnate waqt int ke phle static lagana
 static int fib(int n)

Code:
import java.util.Scanner;

public class Tmp {
static int fib(int n)
{
// Base Case
if (n <= 1)
return n;

// Recursive call
return fib(n - 1) + fib(n - 2);
}


public static void main(String args[]){
Scanner sc=new Scanner(System.in); //System.in is a standard input stream
int n= sc.nextInt();
// Print the first n numbers
for (int i = 0; i < n; i++) {

System.out.print(fib(i) + " ");
}
}
}

 Prime Numbers:

jmks :Math.sqrt() ko use krne ke liye 
import java.lang.Math; use nhi kiya fir bhi kaam ho gya
code:
import java.util.Scanner;

public class Tmp {
// Check for number prime or not
static boolean isPrime(int n)
{

// Check if number is less than
// equal to 1
if (n <= 1)
return false;

// Check if number is 2
else if (n == 2)
return true;

// Check if n is a multiple of 2
else if (n % 2 == 0)
return false;

// If not, then just check the odds
for (int i = 3; i <= Math.sqrt(n); i += 2)
{
if (n % i == 0)
return false;
}
return true;
}

public static void main(String args[]){
Scanner sc=new Scanner(System.in); //System.in is a standard input stream
int n= sc.nextInt();

if(isPrime(n)){
System.out.println(n +" is prime");
}else{
System.out.println(n +" is not prime");
}
}
}
Time Complexity: O(\sqrt{n}

Using Recursion :
jmks : static method ke liye global variable bhi static
 static int i = 2;
import java.util.Scanner;

public class Tmp {
static int i = 2;

// Function check whether a number
// is prime or not
public static boolean isPrime(int n)
{

// Corner cases
if (n == 0 || n == 1)
{
return false;
}

// Checking Prime
if (n == i)
return true;

// Base cases
if (n % i == 0)
{
return false;
}
i++;
return isPrime(n);
}
public static void main(String args[]){
Scanner sc=new Scanner(System.in); //System.in is a standard input stream
int n= sc.nextInt();

if(isPrime(n)){
System.out.println(n +" is prime");
}else{
System.out.println(n +" is not prime");
}
}
}

Quick ways to check for Prime:

jmks: 

        // Converting int ya long to BigInteger
BigInteger b = new BigInteger(String.valueOf(n));
BigInteger ke pass hi jadu hau hai Quick Prime Check krne ka:
b.isProbablePrime(1)
code:
import java.util.Scanner;

import java.math.BigInteger;
public class Tmp {

public static void main(String args[]){
Scanner sc=new Scanner(System.in); //System.in is a standard input stream
int n= sc.nextInt();
// Converting int ya long to BigInteger
BigInteger b = new BigInteger(String.valueOf(n));
if( b.isProbablePrime(1)){
System.out.println(n +" is prime");
}else{
System.out.println(n +" is not prime");
}
}
}

next Prime in Java:


code:
 BigInteger b = new BigInteger(String.valueOf(n));
long ans= Long.parseLong(b.nextProbablePrime().toString());
System.out.println(ans);

Java program to check whether a string is a Palindrome:


jmks :

 string input lena (sentence ke liye bhi work)
            Scanner sc=new Scanner(System.in);  //System.in is a standard input stream
String s= sc.nextLine();
 string ki length aur index se ele access krna
 str.length()
str.charAt(i)
code:
import java.util.Scanner;

import java.math.BigInteger;
public class Tmp {
// Method
// Returning true if string is palindrome
static boolean isPalindrome(String str)
{

// Pointers pointing to the beginning
// and the end of the string
int i = 0, j = str.length() - 1;

// While there are characters to compare
while (i < j) {

// If there is a mismatch
if (str.charAt(i) != str.charAt(j))
return false;

// Increment first pointer and
// decrement the other
i++;
j--;
}

// Given string is a palindrome
return true;
}
public static void main(String args[]){
Scanner sc=new Scanner(System.in); //System.in is a standard input stream
String s= sc.nextLine();

if(isPalindrome(s)){
System.out.println("Yes");
}else{
System.out.println("No");
}
}
}

Reverse String(or sentence) using inBuilt Function:

Method 1:( using StringBuilder) jmks
code:
    Scanner sc=new Scanner(System.in);  //System.in is a standard input stream
String s= sc.nextLine();
StringBuilder ans = new StringBuilder();
// append a string into StringBuilder input1
ans.append(s);
// reverse StringBuilder input1
ans.reverse();
System.out.println(ans);
Method 2:(using StringBuffer)  jkms
code:
    Scanner sc=new Scanner(System.in);  //System.in is a standard input stream
String s= sc.nextLine();
StringBuffer ans = new StringBuffer(s);
// To reverse the string
ans.reverse();
System.out.println(ans);

Program to Count total digit in Number:

Method 1:
code:
int digit_count=0;
while(n>0){
n=n/10;
digit_count++;
}
System.out.println(digit_count);
Method 2:(using inBuilt) jkms
code:
    int digit_count=0;
// Find total digits in number
digit_count = (int) Math.log10(n) + 1;

Function to calculate x raised to the power y:

Method 1:(using recursion)
code:
static int power(int x, long y)
{
if( y == 0)
return 1;
//even power
if (y%2 == 0)
return power(x, y/2)*power(x, y/2);
//odd power
return x*power(x, y/2)*power(x, y/2);
}
Method 2:(using  inBuilt) jkms
code:
int ans=(int)Math.pow(x, y);

Find sum of digits Number:


Method 1:
code:

static int getSum(int n)
{
int sum = 0;

while (n != 0)
{
sum = sum + n % 10;
n = n/10;
}

return sum;
}

Armstrong number :

A positive integer of n digits is called an Armstrong number of order n (order is number of digits) if. 

abcd... = pow(a,n) + pow(b,n) + pow(c,n) + pow(d,n) + ....
code:
static boolean isArmstrong(int n){
int num=n;
int digits_count=(int)Math.log10(n) + 1;
int sum=0;
while (n>0){
int last_digit=n%10;
sum+=(int)Math.pow(last_digit,digits_count);
n=n/10;
}
// System.out.println(sum);
return num==sum;
}

Armstrong Number upto number n:

code:
for(int i=0;i<n;i++){
if(isArmstrong(i)){
System.out.print(i+" ");
}
400
0 1 2 3 4 5 6 7 8 9 153 370 371 
conclusion: There is no two digit Armstrong Number

First n Armstrong Number:

code:
        int count=0;
for(int i=0;i<Integer.MAX_VALUE;i++){
if(isArmstrong(i)){
System.out.print(i+" ");
count++;
}
if(count==n){
break;
}
}
15
0 1 2 3 4 5 6 7 8 9 153 370 371 407 1634 

Reverse a Number:

code:
   int rev=0;
while (n>0){
int last_digit=n%10;
rev=rev*10+last_digit;
n=n/10;
}
System.out.println(rev);

What is an automorphic number?

A number is called an automorphic number if and only if the square of the given number ends with the same number itself. For example, 25, 76 are automorphic numbers because their square is 625 and 5776, respectively and the last two digits of the square represent the number itself. Some other automorphic numbers are 5, 6, 36, 890625, etc.

Peterson Number

A number is said to be Peterson if the sum of factorials of each digit is equal to the sum of the number itself.

e.g. 

145 = !1 + !4 + !5

=1+4*3*2*1+5*4*3*2*1

=1+24+120

145=145

Sunny Number

A number is called a sunny number if the number next to the given number is a perfect square. In other words, a number N will be a sunny number if N+1 is a perfect square.

 N=80 then N+1 will be 80+1=81, which is a perfect square of the number 9. Hence 80 is a sunny number.

Swap Two Numbers:

Method 1 :(using Xor)

code:
int a=sc.nextInt();
int b= sc.nextInt();
a=a^b;
b=a^b;
a=a^b;
Method 2 :(using Multiplication and Division)
a=a*b;
b=a/b;
a=a/b;

gcd of Two Numbers:

Method 1:(using Euclidean algorithm)

code:
static int gcd (int a, int b) {
if (b == 0)
return a;
else
return gcd (b, a % b);
}

Method 2:(using BigInteger)

code:
  String s1=sc.nextLine();
String s2=sc.nextLine();
BigInteger a=new BigInteger(s1);
BigInteger b=new BigInteger(s2);
System.out.println(a.gcd(b));







-----

Comments

Popular posts from this blog

c++ oops

Takeoff (hackerearth.3, datastructure, array 1-D)

Aptitude tricks