Tuesday, 5 February 2019

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

6.Write a Program to find ASCII values in java.


Description:
Write a sample code to find ASCII values in java.


Code:
?
1
2
3
4
5
6

public class ASCII_VALUE { public static void main(String[] args) { char ch='A'; int i=ch; System.out.println("Ascii Character---" + ch+"=="+i); } }

Output:
 Ascii Character---A==65

5.How to find armstrong number in java.


Description:
Write a sample code  to find Armstrong number in java

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

public class Armstrong_NO {
public static void main(String[] args) { int n=159; int temp=n; int r,sum=0; while(n>0) { r=n%10; n=n/10; sum =sum+ r* r*r; } if(temp==sum) System.out.println(" Armstrong : "); else System.out.println("This is not Armstrong"); } }


Output:
This  is not  Armstrong