PDF Google Drive Downloader v1.1


Report a problem

Content text Java Interview Specific Codes.pdf

Java Interview Specific Codes.md 2024-05-28 1 / 57 Java Important Programs 1. Find a number is positive or negative ? import java.util.Scanner; class Main { public static void main (String[]args) { Scanner sc = new Scanner(System.in); int num = sc.nextInt(); if (num > 0) System.out.println ("The number is positive"); else if (num < 0) System.out.println ("The number is negative"); else System.out.println ("Zero"); } } // 0 is a positive number because if we represent 0 in terms of binary : 0000 0000 // in the above representation the Most Significant Bit (MSD - First bit) is 0 which // indicate a positive number 2. Find a number is Even or Odd? // Using Modulus Operator public class Main { public static void main(String[] args) { int number = 29; //checking whether the number is even or odd if (number % 2 == 0) System.out.println(number + " is Even"); else System.out.println(number + " is odd"); } } // Using Bitwise Operator public class Main {
Java Interview Specific Codes.md 2024-05-28 2 / 57 public static void main (String[]args) { int number = 29; if (isEven (number)) System.out.println ("Even"); else System.out.println ("Odd"); } // Returns true if n is even, else odd static bool isEven (int number) { // n & 1 is 1, then odd, else even return (!(number & 1)); } } 3. Find the Sum of First N Natural Numbers in Java //using loops public class Main { public static void main (String[]args) { int n = 10; int sum = 0; for (int i = 1; i <= n; i++){ sum += i;//sum=sum+i; } System.out.println (sum); } } //using formula public class Main { public static void main (String[]args) { int n = 10; System.out.println (n*(n+1)/2); } }
Java Interview Specific Codes.md 2024-05-28 3 / 57 4. Find the Sum of the Numbers in a Given Interval in Java //Using Brute Force public class Main { public static void main (String[]args) { int a = 5; int b = 10; int sum = 0; for (int i = a; i <= b; i++) sum = sum + i; System.out.println ("The sum is " + sum); } } //using formula public class Main { public static void main(String[] args) { int num1 = 2; int num2 = 5; int sum = num2*(num2+1)/2 - num1*(num1+1)/2 + num1; System.out.println("The Sum is "+ sum); } } 5. Find the Greatest of the Three Numbers in Java //using conditional statements public class Main { public static void main (String[]args) { int num1 = 10, num2 = 20, num3 = 30; //checking if num1 is greatest if (num1 >= num2 && num1 >= num3) System.out.println (num1 + " is the greatest"); //checking if num2 is greatest else if (num2 >= num1 && num2 >= num3) System.out.println (num2 + " is the greatest"); //checking if num2 is greatest else if (num3 >= num1 && num3 >= num2) System.out.println (num3 + " is the greatest");
Java Interview Specific Codes.md 2024-05-28 4 / 57 } } public class Main { public static void main (String[]args) { int num1 = 10, num2 = 20, num3 = 30; int temp, result; // find the largest b/w num1 and num2 & store in temp temp = num1>num2 ? num1:num2; // find the largest b/w temp and num3 & finally printing it result = temp>num3 ? temp:num3; System.out.println (result + " is the greatest"); } } 6. Check Whether or Not the Year is a Leap Year in Java import java.util.Scanner; public class LeapYear { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("Enter the year :"); int n = sc.nextInt();// boolean result = leapYear(n); System.out.println(result); } public static boolean leapYear(int n) { if(n%4 == 0) { if(n%100 == 0) { if(n%400 == 0) { return true; } else { return false; } } else { return false; } } else { return false;

Related document

x
Report download errors
Report content



Download file quality is faulty:
Full name:
Email:
Comment
If you encounter an error, problem, .. or have any questions during the download process, please leave a comment below. Thank you.