Monday, October 4, 2021

HANDS ON JAVA(OOP - based uses) (Part-I)

    Practice JAVA like Never Before!

                                                                                                    courtesy: Google Images

Day 1 🔥


Objective

Learning to code some Simple JAVA Programs:-

1. Write a program to find the sum of digits and reverse of a given integer number (take input using command-line argument).

  • Program:

public class A1a{
    public static void main(String args[]){
        int n=Integer.parseInt(args[0]);
int a, rev = 0, sum = 0;
        while(n>0){
            a = n % 10;
            rev = rev * 10 + a; //reversing
            sum = sum + a; //summing
            n = n / 10;
        }
        System.out.println("Sum of digits:" + sum);
System.out.println("Reverse:" + rev);   
    }
}


  • Output:



---------------------------------------------------

2. Write a program to determine the sum of the following series for a given value of n (take input using command-line argument): 1 + 1/2 + 1/3 + ...... + 1/n. Print the result up to two decimal places.

  • Program:

import java.io.*;
public class A1b{
    static double sum(int n){
      double i, s = 0.0;
      for (i=1; i<=n; i++)
          s = s + 1/i;
      return s;
    }
    public static void main(String args[]){
        int n = Integer.parseInt(args[0]);
        System.out.printf("Sum is %.2f", sum(n));
    }
}


  • Output:



---------------------------------------------------


3. Write a program to find the factorial of a given integer number using recursion (take input using command-line argument).

  • Program:

import java.io.*;
public class A1c{
    public static long fact(long n){
if(n<=0)
return 1;
return n*fact(n-1);
}
    public static void main(String args[]){
        long n = Long.parseLong(args[0]);
        System.out.printf("Factorial is: "+fact(n));
    }
}


  • Output:



--------------------------------------------------

4. Write a program to find the real roots of the quadratic equation ax2 + bx + c = 0 where a, b and c are constants (take input using command-line argument).

  • Program:

public class A1d{
public static void main(String args[]){
int a = Integer.parseInt(args[0]);
int b = Integer.parseInt(args[1]);
int c = Integer.parseInt(args[2]);
System.out.println("The Equation is "+a+"x^2 + "+b+"x+ "+c+" =0");
int discriminant = b*b - 4*a*c;
if(discriminant<0)
System.out.println("No real roots");
else if(discriminant==0){
double x = -b/(2.0*a);
System.out.println("Root is: "+x);
}
else{
double d = Math.sqrt(discriminant);
double x1 = (-b+d)/(2.0*a);
double x2 = (-b-d)/(2.0*a);
System.out.println("The Roots are: "+x1+" and "+x2);
}
}
}


  • Output:



--------------------------------------------------

5. Write a program to calculate GCD of two integer numbers (take input using command-line argument).

  • Program:

public class A1e{
public static int gcd(int x, int y){
if(x%y==0)
return y;
return gcd(y, x%y);
}
public static void main(String args[]){
int a = Integer.parseInt(args[0]);
int b = Integer.parseInt(args[1]);
System.out.println("The GCD of "+a+" and "+b+" is: "+gcd(a, b));
}
}


  • Output:




<---------- End of Part - I ---------->😋


No comments:

Post a Comment

You may like these:

HANDS ON JAVA(OOP - based uses) (Part-I)

     Practice JAVA like Never Before!                                                                                                     ...