Tuesday, 5 February 2019

12. Swapping of two Numbers in java without third variable.


Description:
Write a sample code to Swapping of two Numbers in java without third variable.


Code:
?
1
2
3
4
5
6
7
8
9
10
import java.util.Scanner; public class Swapping_Number { public static void main(String[] args) { int x,y; System.out.println("Enter x and y no: "); Scanner s=new Scanner(System.in); x=s.nextInt(); y=s.nextInt(); System.out.println("Before Swaping \n x="+x+"\n y="+y); x=x+y; y=x-y; x=x-y; System.out.println("After Swaping\n x="+x+"\n y="+y); } }


Output:
Enter x and y no: 
10
20
Before Swaping 
 x=10
 y=20
After Swaping
 x=20
 y=10

11. Palindrome program in java.


Description:
Write a sample code to Palindrome in java.


Code:
?
1
2
3
4
5
6
7
8
9
import java.util.Scanner; class Palindrome { public static void main(String args[]) { String original, reverse = ""; Scanner in = new Scanner(System.in); System.out.println("Enter a string to check :"); original = in.nextLine(); int length = original.length(); for (int i = length - 1; i >= 0; i--) reverse = reverse + original.charAt(i); if (original.equals(reverse)) System.out.println("The string is a palindrome."); else System.out.println("The string isn't a palindrome."); } }

Output:
Enter a string to check :
madam
The string is a palindrome.

Enter a string to check :
godblessgovind
The string isn't a palindrome.

10. FizzBuzz Program in Java - Interview Question


Description:
Write a sample code to FizzBuzz Program in Java.


Code:
?
1
2
3
4
5
6
7
8
9

import java.util.Scanner; public class FizzBuzz { public static void main(String[] args) { for(int i=1; i<=15; i++) { Scanner s=new Scanner(System.in); if(i%(3*5)==0)System.out.println("fizzBuzz"); else if (i% 5 ==0) System.out.println("fizz"); else if (i% 3==0)System.out.println("Buzz"); else System.out.println(i); } } }


Output:
1
2
Buzz
4
fizz
Buzz
7
8
Buzz
fizz
11
Buzz
13
14
fizzBuzz

9.Program to print Fibonacci Series using for loop in java.


Description:
Write a sample code to print Fibonacci Series using for loop in java.


Code:
?
1
2
3
4
5
6
7
8

public class Fibonacci_Searies { public static void main(String args[]) { int febCount = 15; int[] feb = new int[febCount]; feb[0] = 0; feb[1] = 1; for (int i = 2; i < febCount; i++) { feb[i] = feb[i - 1] + feb[i - 2]; } for (int i = 0; i < febCount; i++) { System.out.println(feb[i] + ""); } } }


Output:
0
1
1
2
3
5
8
13
21
34
55
89
144
233
377

8.Factorial program in java.


Description:
Write a sample code for Factorial in java..


Code:
?
1
2
3
4
5
6

public class FactorialNumber { public static void main(String[] args) { int i,fact=1; int number=5; for(i=1;i<=number;i++) { fact=fact*i; } System.out.println("Factorial of : "+number+"\n"+"Factorial is : "+fact); } }


Output:
Factorial of : 5
Factorial is : 120

7. How to check Even OR Odd using scanner in java.


Description:
Write a sample code to check Even OR Odd  using scanner in java.


Code:
?
1
2
3
4
5
6
7
8
9
10
import java.util.Scanner; public class Even_Odd_Number { public static void main(String[] args) { int x; System.out.println("Enter the Number:"); Scanner s=new Scanner(System.in); x=s.nextInt(); if (x%2==0) System.out.println("Number is Even"); else System.out.println("Number is Odd"); } }


Output:
Enter the Number:
12
Number is Even