Showing posts with label java programs. Show all posts
Showing posts with label java programs. Show all posts

Sunday, 27 March 2016

JAVA Questions

Some question of Java programs are given below as a link. Click on that link which one you want to know more. The given links will display the program code and its output. Here I displayed the output as an Image. Check its working by copying that codes to a Notpad or any text editors and then save as a JAVA file.

Questions are given below. Click on that to get answers,
  1. Write a java program to find area and circumference of a circle?
  2. Write a java program to calculate electricity bill by reading the consumer no and no of units consumed. The charges for different slabs are given below
    up to 50 unitsRs 1.5 per unit
    50-100 unitsRs 2 per unit
    100-200 unitsRs 2.8 per unit
    200-300 unitsRs 3.5 per unit
    above 300 unitsRs 4.50 per unit
  3. Write a java program to  display n prime numbers?
  4. Write a java program to display Fibonacci series up to n?
  5. Write a java program to check whether a given year is leap or not?
  6. Write a java program to display the Armstrong within range?
  7. Write a java program to check whether a given number is perfect, abundant, or deficient?
  8. Write a java program to check whether the given sides can form a triangle. If yes,find area of the triangle?
  9. Write a java program to find the roots of a quadratic equation?
  10. Write a java program to compute the mean and SD of 3 numbers. Mean=(a+b+c)/3, SD=√((a-m)²+(b-m)²+(c-m)²)/3
  11. Write a java program to read an array of 10 numbers and to find the following,
    a. Sum of the elements
    b. Average of the elements
    c. Maximum of the elements
    d. Minimum of the elements
     
  12. Write a java program to sort an array?
  13. Write a java program to count the number of even numbers, odd numbers, positive numbers, negative numbers and zeros in an array?
  14. Write a java program to appending two arrays?
  15. Write a java program to find the sum of the digits and reverse of a given number using class and objects?
  16. Write a java program to find the volume of cube, rectangular box, cylinder using function overloading?
  17. Write a java program to create a class complex. Create two objects find the sum of the complex numbers, Read the real and imaginary part using constructor?
  18. Find the area of a square and a rectangle using overloaded constructor?
  19. Write a java program to enable arithmetic exceptions?
  20. Write a java program for generating two threads. One for odd numbers and one for even numbers?

Saturday, 26 March 2016

Mean & S.D

Code:


import java.util.Scanner;
class meansd
{
    public static void main(String[] args)
    {
        int a,b,c;
        double m,sd;
        Scanner in=new Scanner(System.in);
        System.out.println("Enter 3 numbers: ");
        a=in.nextInt();
        b=in.nextInt();
        c=in.nextInt();

        m=(a+b+c)/3;
        sd=Math.sqrt(((a-m)*(a-m))+((b-m)*(b-m))+((c-m)*(c-m)))/3;

        System.out.println("Mean= "+m);
        System.out.println("SD= "+sd);
    }
}

Output:


Roots of Quadratic Equation

Code:


import java.util.Scanner;
class Quadratic{
    public static void main(String[] args) {
        int a,b,c;
        System.out.println("Enter the three values of the equation");
        Scanner in=new Scanner(System.in);
        System.out.print("\nEnter a: ");
        a=in.nextInt();
        System.out.print("Enter b: ");
        b=in.nextInt();
        System.out.print("Enter c: ");
        c=in.nextInt();

        double temp=Math.sqrt(b*b-4*a*c);
        double root1=(-b+temp)/(2*a);
        double root2=(-b-temp)/(2*a);
        System.out.println("Roots of the Quadratic Equation are: "+root1 +" and "+root2+"\n");
    }
}

Output:


To Check is Triangle or Not

Code:


import java.util.*;
class triangle
{
    public static void main(String[] args) {
        int a,b,c;
        double area;
        System.out.println("Let a,b,c are the three sides of triangle");
        Scanner in=new Scanner(System.in);
        System.out.print("\nEnter a: ");
        a=in.nextInt();
        System.out.print("Enter b: ");
        b=in.nextInt();
        System.out.print("Enter c: ");
        c=in.nextInt();
        if ((a>b)||(b>c))
        {
            System.out.print("Unacceptable values");
        }
        else if (a+b<=c)
        {
            System.out.print("Length do not represents triangle sides");
        }
        else
        {
            System.out.print("Length represents triangle sides\n");
            area=0.5*a*b;
            System.out.print("Then the area of the triangle is: "+area);
        }
    }
}

Output:


Perfect, Abundant or Deficient

Code:


import java.util.Scanner;
class perfect
{
    public static void main(String args[])
    {
      int a,b,c,d,i,sum=0,dsum=0;
      Scanner in=new Scanner(System.in);
      System.out.print("Enter a number : ");
      a=in.nextInt();
     
      for(i=1;i<=a;i++)
      {
          if(a%i==0)
          {
              sum=sum+i;
      }
      }
    dsum=2*a;
    if(sum<dsum)
    {
       System.out.println("it is a deficient number");
    }
    else if(sum==dsum)
    {
       System.out.println("it is a perfect number"); 
    }
    else
    {
        System.out.println("it is a abundant number");
    }
    }
    }

Output:


Amstrong in Range

Code: 

import java.util.Scanner;
class armstrong
{
    public static void main(String[] args) {
        Scanner s=new Scanner(System.in);
        System.out.print("Enter the Range: ");
        int range=s.nextInt();
        int a,b,c,sum,count=0;
        b=1;
        while(b<=range)
        {
            sum=0;
            c=b;
            while(c>0)
            {
                a=c%10;
                sum=sum+(a*a*a);
                c=c/10;
            }
            if(sum==b)
            {
                System.out.println(b+" is a Armstrong Number");
                count=count+1;
            }
            b++;
        }
        System.out.println("There are "+count +" Armstrong Number in that range");
    }
}

Output:

 

Leap or Not

Code:

import java.util.Scanner;
class leap
{
    public static void main(String[] args)
    {
        int a;
        Scanner in=new Scanner(System.in);
        System.out.print("Enter the year to check leap or not : ");
        a=in.nextInt();
        if ((a%4==0)&&(a%100!=0))
        {
            System.out.println(a+" is a leap year");   
        }
        else if ((a%4==0)&&(a%100==0)&&(a%400==0))
        {
            System.out.println(a+" is a leap year");   
        }
        else
        {
            System.out.println(a+" is not a leap year");
        }
    }   
}


Output:


Fibonacci Series up to N

Code:


import java.util.Scanner;
class fibonacci
{
    public static void main(String[] args)
    {
        int n,a=0,b=1;
        Scanner in=new Scanner(System.in);
        System.out.print("Enter limit: ");
        n=in.nextInt();
        if (n>1)
        {
            System.out.println("0"+"\n"+"1");           
        }           
        for (int i=3;i<=n;i++)
        {
            int c=a+b;
            if (c<=n)
            {
            System.out.println(c);
            a=b;
            b=c;   
            }
        }   
    }
}

Output:


N Prime Numbers

Code:

import java.util.Scanner;
class prime {
    public static void main(String[] args) {
        int n,num=3,status=1;
        Scanner in=new Scanner(System.in);
        System.out.print("Enter the limit : ");
        n=in.nextInt();
        if (n>1) {
            System.out.println(2);
        }
        for (int count = 2; count <= n;)
        {
        for (int j = 2; j <= Math.sqrt(num) ; j++)
            {
                if (num%j==0)
                    {
                        status=0;
                        break;
                    }
            }
        if (status!=0)
            {
                System.out.println(num);
                count++;
            }
        status=1;
        num++;
        }
    }
}


Output:


Electricity bill calculator

Code:

import java.util.*;
class ElectricityBill
{
    public static void main(String []args)
    {
        int consumer_no,unit;
        double amount;
        Scanner in=new Scanner(System.in);
        System.out.println("Enter your consumer no.: ");
        consumer_no=in.nextInt();
        System.out.println("Enter no. of units used: ");
        unit=in.nextInt();

        if (unit<=50)
        {
            amount=1.5*unit;
            System.out.println("Amount to be paid: "+amount);
        }

        else if (unit>50 && unit<100)
        {
            amount=2*unit;
            System.out.println("Amount to be paid: "+amount);   
        }

        else if (unit>=100 && unit<200)
        {
            amount=2.8*unit;
            System.out.println("Amount to be paid: "+amount);   
        }

        else if (unit>=200 && unit<300)
        {
            amount=3.5*unit;
            System.out.println("Amount to be paid: "+amount);   
        }

        else if (unit>=300)
        {
            amount=4.5*unit;
            System.out.println("Amount to be paid: "+amount);   
        }

        else
        {
            System.out.println("Cannot calculate the Electricity Bill");   
        }
    }
}


Output:


Area and circumference of a circle

Code:


import java.util.Scanner;
class Circle
{
    static Scanner sc=new Scanner(System.in);
    public static void main(String args[])
    {
        System.out.print("\n\nEnter the Radius: ");
        double radius=sc.nextDouble();
        double area=Math.PI*(radius*radius);
        System.out.print("\nThe area of the circle is:" + area);
        double circumference=Math.PI*2*radius;
        System.out.print("\nThe Circumference of the circle is:" + circumference +"\n\n");
    }
}

Output:


 

Friday, 25 March 2016

Sum, Average, Max., Min of Elements in an Array

Code:


import java.io.*;
import java.lang.*;
import java.util.Scanner;
class arra
{
    public static void main(String args[])throws IOException
    {
    int sum=0,avg,max,mini,n,i;
    Scanner in=new Scanner(System.in);
    System.out.println("enter three  number");
    n=in.nextInt();
    int[] h= new int[n];
     System.out.println("enter the value");
     for(i=0;i<n;i++)
     {
         h[i]=in.nextInt();
     }
     for(i=0;i<n;i++)
     {
         sum=sum+h[i];
     }
     System.out.println("sum of the array"+sum);
     avg=sum/n;
     System.out.println("sum of the average"+avg);
     max=h[0];
     mini=h[0];
     for(i=0;i<n;i++)
     {
         if(h[i]>max)
         {
             max=h[i];
         }
         else if(h[i]<mini)
             {
             mini=h[i];
         }
     }
     System.out.println("maximum value"+max);
     System.out.println("minimum  value"+mini);
    }
    }


Output:

Array sorting

Code:


import java.io.*;
import java.lang.*;
import java.util.Scanner;
class sorting
{
    public static void main(String args[])
    {
        int a,b,n,i,temp,j;
    Scanner in=new Scanner(System.in);
    System.out.print("How many numbers to sort : ");
    n=in.nextInt();
    int[] h= new int[n];
     System.out.println("Enter the "+n +" Elements");
     for(i=0;i<n;i++)
     {
         h[i]=in.nextInt();
        
     }
     for(i=0;i<n-1;i++)
     {
         for(j=0;j<(n-i-1);j++)
         {
             if(h[j]>h[j+1])
             {
                 temp=h[j];
                 h[j]=h[j+1];
                 h[j+1]=temp;
             }
         }
     }
      System.out.println("sorted list");
      for(i=0;i<n;i++)
      {
           System.out.println(h[i]);
      }
    }
    }


Output:

 

Count Even, Odd, Positive and Negative Numbers

Code:


import java.io.*;
import java.lang.*;
import java.util.Scanner;
class count
{
    public static void main(String args[])throws IOException
    {
      int a,i,p,j,k,count=0;
      int[] h= new int[50];
     
      Scanner in=new Scanner(System.in);
      System.out.println("insert numbers");
      p=in.nextInt();
     for( i=0;i<p;i++)
            {
                System.out.println("enter your value "+i);
            h[i]=in.nextInt();
            count++;
         
            }
    int odd=0,even=0,postive=0,negative=0;
  
    for(j=0;j<count;j++)
    {
        if(h[j]%2==0)
        {
        even++;
    }
        else
        {
            odd++;
        }
    }
        for(k=0;k<count;k++)
        {
            if(h[k]<0)
            {
                negative++;
            }
            else
            {
                postive++;
            }
        }
    System.out.println("There are " +odd +" odd numbers");
    System.out.println("There are " +even +" even numbers");
    System.out.println("There are " +postive +" postive numbers");
    System.out.println("There are " +negative +" negative numbers");
   
   
    }
   
    }


Output:

Array appending

Code:


class append {

     public static void main(String []args) {

        int[] a = new int[] { 1, 2, 3};
        int[] b = new int[] { 3, 4, 5};
        int[] r = new int[a.length + b.length];
        System.arraycopy(a, 0, r, 0, a.length);
        System.arraycopy(b, 0, r, a.length, b.length);
        for(int x : r) {
            System.out.println(x);
        }           
     }        
}


Output:

 

Sum of the digit and Reverse of Sum

Code:


import java.util.*;
class reverse
{
    public static void main(String []args)
    {
        int a,b,c,reverse=0;
        Scanner in=new Scanner (System.in);
        System.out.print("enter any integer number: ");
        a=in.nextInt();
        int sum=0;
        int n=a;
        while (n!=0)
        {
            int lastdigit=n%10;
            sum +=lastdigit;
            n /=10;   
        }
         System.out.println("sum of the digits is "+sum);
         while(sum!=0)
         {
             reverse=reverse*10;
             reverse=reverse+(sum%10);
             sum=sum/10;
         }
          System.out.println("Reverse of the sum is "+reverse);
    }
}


Output:


Volume of Cube, Rectangular box and Cylinder

Code:


import java.util.*;

class volume
{
   
   void all(int a)
  {
      int k;
        k=a*a*a;
    
       System.out.println("volume of cube\t"+k);
      }
    void all(int b,int c)
  {
      int k;
      double pi=3.14;
      k=(int)(pi*(c*c)*b);
    
       System.out.println("volume of cylinder\t"+k);
      }
      void all(int d,int e,int f)
  {
      int k;
     
      k=d*e*f;
    
       System.out.println("volume of rectangular box\t"+k);
      }
 
  
   
}
class reca
{
public static void main(String[] args)
{
    int a,b,c,d,e,f;
    Scanner in=new Scanner(System.in);
      System.out.println("sides of cube");
    a=in.nextInt();
    volume obj=new volume();
  obj.all(a);
    System.out.println("height of cylinder");
   b=in.nextInt();
     System.out.println("radius of cylinder");
      c=in.nextInt();
    obj.all(b,c);
    System.out.println("rectangle box length");
 d=in.nextInt(); 
 System.out.println("rectangle box height");
 e=in.nextInt();
 System.out.println("rectangle box width");
 f=in.nextInt();
 obj.all(d,e,f);

}
}


Output:


Sum of the complex number

Code:


import java.util.*;

class complex
{
    int a,b,c,d;
   
  complex(int a,int b,int c,int d){
    this.a=a;
      this.b=b;
      this.c=c;
      this.d=d;
     
  }
 void complex(complex r1)
 {
    int k,n;
    k=a+c;
    n=b+d;
    System.out.println("sum of complex numbers");
      System.out.println(k+"+"+n+"i");
 }
}
class complex1
{
public static void main(String args[])
{
    int a,b,c,d;
    Scanner in=new Scanner(System.in);
      System.out.println("enter the real number of first complex number");
    a=in.nextInt();
     System.out.println("enter the imaginary number of first complex number");
    b=in.nextInt();
     System.out.println("enter the real number of second complex number");
    c=in.nextInt();
     System.out.println("enter the imaginary number of second complex number");
    d=in.nextInt();
 complex r1=new complex(a,b,c,d);
 r1.complex(r1);
}
}


Output:


Area of a Square and Rectangle using overloaded constructor

Code:


import java.util.*;

class area
{
   
   void all(int a)
  {
      int k;
     
        k=a*a;
    
       System.out.println("The Area of the Square is "+k);
      }
    void all(int b,int c)
  {
      int k;
      k=b*c;
    
       System.out.println("The Area of the Rectangle is "+k);
      }
}
class reca
{
public static void main(String args[])
{
    int a,b,c;
    Scanner in=new Scanner(System.in);
      System.out.print("Sides of Square= ");
    a=in.nextInt();
    area obj=new area();
  obj.all(a);
    System.out.print("Height of rectangle= ");
   b=in.nextInt();
     System.out.print("Breadth of rectangle= ");
      c=in.nextInt();
    obj.all(b,c);
   
}
}


Output:


Arithmetic Exception

Code:


class exception
{
    public static void main(String[] args)
    {
        int a=10,b=5,c=5;
        int x,y=0;
        try
        {
            x=a/(b-c);
        }
        catch(ArithmeticException e)
        {
            System.out.print("Divisible by zero");
            y=a/(b+c);
            System.out.print("\ny= "+y);
        }
    }
}


Output: