Sunday, September 6, 2020

To develop a java program to print's that call by address

import java.util.Scanner;

public class reference
{
   public static void main(String args[])
   {
     int number;
     Scanner sayed=new Scanner(System.in);
     //printing before update
     System.out.println("Number:");
     number=sayed.nextInt();
     System.out.println("\nBefore update number:"+number);
     // update funcation return
     number=update (number);
     System.out.println("\nAfter update number:"+number);
   }
   public static int update (int number)
   {
     number=number+2;
     return number;
   }
}
   

Saturday, September 5, 2020

To develop a java program to solve an quadratic equation.

 import java.util.Scanner;

 class Quadratic

 {

   public static void main(String args[])

   { 

     double a,b,c,root1,root2;

     Scanner sc=new Scanner (System.in);     

    System.out.println("Enter values are a and b and c:");

   a=sc.nextDouble();

   b=sc.nextDouble();

   c=sc.nextDouble();

   

      double determinant=b*b-4*a*c;

     double sqrt=Math.sqrt(determinant);

   

   if(determinant>0)

   {

      root1=(-b+sqrt)/(2*a);

      root2=(-b-sqrt)/(2.0*a); 

      System.out.format("\nRoot1=%.2f\nRoot2=%.2f",root1,root2);

   }

   else if(determinant==0)

   {

      root1=(-b+sqrt)/(2*a);

      System.out.format("\nRoot1:%.2f",root1);

   }

   else{

     System.out.println("\nThe equation is not real roots.");

   }

   }

 }

    

To develop a java program to swap the content of two variables.

 import java.util.Scanner;

  class swap_with{

  public static void main(String[] args){

  int a,b,c;

  Scanner in= new Scanner(System.in);

  System.out.println("Enter the values a and b:");

  a=in.nextInt();

  b=in.nextInt();

  System.out.println("\nBefore swapping:\n"+a+" and "+b);

  c=a;

  a=b;

  b=c;

  System.out.println("\nAfter swapping:\n"+a+" and "+c);

  }

}

To write a java program to find the value of a number rose to its power that demonstrates a method using call by value.

 import java.util.Scanner;

 public class power_number

 {

   public static Scanner sayed;

  public static void main(String[] args)

   {

    int base,exp;

    sayed=new Scanner (System.in);

   System.out.println("Enter the number and power:");

    base=sayed.nextInt();

    exp=sayed.nextInt();

    System.out.print(result(base,exp));

    }

   public static double result(int base, int exp)

    {

      System.out.print(""+base+" to the power "+exp+" = ");

      return Math.pow(base,exp); 

    } 

  }

To write a java program to find the sum for the given variables using method with default arguments.

import java.util.Scanner;
 public class Method
 {
   private static Scanner sayed;
  public static void main(String[] args)
   {
    int a,b,c;
    
    sayed=new Scanner (System.in);
    System.out.println("Enter a:");
    a=sayed.nextInt();
    System.out.println("Enter b:");
    b=sayed.nextInt();
    System.out.println("Enter c:");
    c=sayed.nextInt();
    
    System.out.println(sum (a,b,c));
    }
    public static double sum (int a, int b, int c)
    { 
      System.out.println("\nThe sum of a+b+c:");
      return a+b+c;
    }
   }