Java Code

Java programming language was originally developed by Sun Microsystems which was initiated by James Gosling and released in 1995 as core component of Sun Microsystems.


Software- IntelliJ IDEA Community Edition 2017.2.6 x64, Java JDK 9.




1. Java Program to print fibonacci series upto nth term.


import java.util.Scanner;
public class Main {

    public static void main(String[] args) {
        int a = 0, b = 1, sum = 1;
        Scanner reader = new Scanner(System.in);  // Reading from System.in      
        System.out.println("Enter a number: ");
        int n = reader.nextInt();
        reader.close();
        for (int i = 0; i < n; i++) {
            System.out.print(" "+sum);
            sum = a + b;
            a = b;
            b = sum;
        }
    }
}



Output-


Enter a number: 
6
 1 1 2 3 5 8
Process finished with exit code 0



2. Java Program to print ASCII value of Character entered by user.



import java.util.Scanner;
public class Main {

    public static void main(String[] args) {
        int x;
        System.out.println("Enter a character: ");
        Scanner reader = new Scanner(System.in);
        char ch = reader.next().charAt(0);
        x=ch;
        System.out.println("Ascii value of "+ch+" is "+x);

    }
}



Output-

Enter a character: 
g
Ascii value of g is 103



3. Java program to find all the roots of quadratic equation.

ax2 + bx + c = 0, where
a, b and c are real numbers and
a ≠ 0
package com.company;
import java.util.Scanner;
public class Main {
  public static void main(String[] args) {
    double a,b,c,root1,root2;
    System.out.println("ax2 + bx + c = 0, where\n"+
            "a, b and c are real numbers and\n"+
            "a ≠ 0\n ");
    System.out.println("Enter the value a b and c");
    Scanner reader = new Scanner(System.in);
     a = reader.nextInt();
     b = reader.nextInt();
     c = reader.nextInt();
    double d = (b*b-4*a*c);
    if(d==0){
        System.out.println("Roots are real and equal");
        root1=root2=(-b/2*a);
        System.out.println("root1=root2="+root2);
    }
    else if(d>0){
        System.out.println("Roots are real");
        root1=(-b + Math.sqrt(d)) / (2 * a);
        root2=(-b - Math.sqrt(d)) / (2 * a);
        System.out.println("root1="+root1+"root2="+root2);
    }
    else if(d<0){
        double r = -b / (2 *a);
        double i = Math.sqrt(d) / (2 * a);
        System.out.format("root1 = %.2f+%.2fi and root2 = %.2f-%.2fi", r, i, r, i);
    }


}



Output-
ax2 + bx + c = 0, where
a, b and c are real numbers and
a ≠ 0
 
Enter the value a b and c
1 2 1
Roots are real and equal
root1=root2=-1.0





4. Java program to create a method fact to find to find factorial of entered number.

import java.util.Scanner;
public class Main {
    public static long fact(int x){
        long f=1;
        for(int i=1;i<=x;i++){
            f=f*i;
        }
        return f;
    }

    public static void main(String[] args) {
   int x;
   System.out.println("Enter any number");
        Scanner reader = new Scanner(System.in);
        x = reader.nextInt();
        System.out.println("Factorial="+fact(x));

    }

}

Output-
Enter any number
8
Factorial=40320
Process finished with exit code 0

5. Java Program to make simple calculator using switch case.


import java.util.Scanner;
public class Main {

    public static void main(String[] args) {
        Scanner reader = new Scanner(System.in);
        System.out.print("Enter two numbers: ");
        double x = reader.nextDouble();
        double y = reader.nextDouble();
        System.out.print("Enter an operator (+, -, *, /, %, ^): ");
        char operator = reader.next().charAt(0);
        double result;
        switch(operator)
        {
            case '+':
                result = x + y;
                break;

            case '-':
                result = x - y;
                break;

            case '*':
                result = x * y;
                break;

            case '/':
                result = x / y;
                break;
            case '%':
                result = x%y;
                break;
            case '^':
                result=1;
                for(int i=1;i<=y;i++)
                    result = result * x;
                break;
            default:
                System.out.printf("Error! operator is not correct \n Enter correct Operator");
                return;
        }
        System.out.printf("%.1f %c %.1f = %.1f", x, operator, y, result);
    }


}

Output1-

Enter two numbers:
12 4
Enter an operator (+, -, *, /, %, ^): %
12.0 % 4.0 = 0.0
Process finished with exit code 0

Output2-

Enter two numbers: 4 2
Enter an operator (+, -, *, /, %, ^): ^
4.0 ^ 2.0 = 16.0
Process finished with exit code 0



6. Java program to calculate the power using recursion.


import java.util.Scanner;
public class Main {
    public static int power(int base1,int base2){
        if (base2 != 0)
            return (base1 * power(base1,base2-1));
        else            return 1;
    }

    public static void main(String[] args) {
        int x,y;
        System.out.println("Enter two number to find power");
        Scanner reader = new Scanner(System.in);
        x = reader.nextInt();
        y = reader.nextInt();
        System.out.printf("%d^%d = %d", x, y, power(x,y));
    }
}


Output-

Enter two number to find power
7 3
7^3 = 343
Process finished with exit code 0

Comments

Post a Comment

Popular posts from this blog

Python Code

Perl

C++ code