Go to Top
Showing posts with label Maths. Show all posts
Showing posts with label Maths. Show all posts

Tuesday, 17 January 2012

To convert decimal number into binary notation

Question 73 : Write a program in Java to convert a decimal number into binary notation.

Java Program : 

class dectobin
{
 static void main(int m)
 {
     String s="";
     int n=m;
     while(n>0)
     {
         s=String.valueOf(n%2)+s;
         n=n/2;
        }
        System.out.print("The binary value of "+m+" is : "+s);
    }
}

Wednesday, 14 December 2011

To calculate the Compound Interest (C.I.)

Question 71 : Write a program in Java to find out Compound Interest (C. I.) on the basis of inputs accepted from the user.

Java Program :

import java.io.*;
class compound_interest
{
 static void main()throws IOException
 {
     BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
     double p,r,t,a=0,n;
     System.out.println("\t\t*****Input Data*****\n");
     System.out.print("Enter the principal amount : Rs. ");
     p=Double.parseDouble(br.readLine());
     System.out.print("\nEnter the rate of interest (w/o % sign) : ");
     r=Double.parseDouble(br.readLine());
     System.out.print("\nEnter the time in yrs. : ");
     t=Double.parseDouble(br.readLine());
     System.out.print("\nHow many times a year is the interest calculated (Ex.: Half-yearly : 2 times) ? : ");
     n=Double.parseDouble(br.readLine());
     a=p*(Math.pow((1+(r/(n*100))),(t*n)));
     System.out.println("\n\t\t*****Results*****\n");
     System.out.println("Compound interest : Rs. "+(a-p));
     System.out.println("\nAmount : Rs. "+a);
    }
} 

Saturday, 19 November 2011

To find the sum of even and odd numbers

Question 68 : Write a program in Java to find the sum of odd and even numbers between a range.

Java Program : 

public class sum_range
{
 int e,o,l,u;
 public sum_range(int lower_limit,int upper_limit)
 {
     l=lower_limit;
     u=upper_limit;
     e=0;
     o=0;
     sum();
     display();
    }
    public void sum()
    {
        for(int i=l;i<=u;i++)
        {
            if(i%2==0)
            even(i);
            else
            odd(i);
        }
    }
    public void odd(int a)
    {
        o=o+a;
    }
    public void even(int a)
    {
        e=e+a;
    }
    public void display()
    {
        System.out.print("The sum of even numbers : "+e);
        System.out.print("\nThe sum of odd numbers : "+o);
    }
}
    

Wednesday, 9 November 2011

To find the area of a rectangle

Question 67 : Write a program in Java to find the area and perimeter of a rectangle. Length and breadth to be taken as input from the user.

Java Program :

public class rectangle
{
 public static void main(int l,int b)
 {
     int a=0,p=0;
     a=l*b;
     p=2*(l+b);
     System.out.println("Area of the rectangle = "+a+" units");
     System.out.println("Perimeter of the rectangle = "+p+" units");
    }
}

Friday, 4 November 2011

To print all the factors of a number

Question 67 : Write a program in Java to print all the factors of a number.

Java Program :

import java.io.*;
class factors
{
 static void main()throws IOException
 {
     BufferedReader buf=new BufferedReader(new InputStreamReader(System.in));
     int a,i;
     System.out.print("Enter the number : ");
     a=Integer.parseInt(buf.readLine());
     System.out.print("\n");
     System.out.print("The factors are : ");
     for(i=1;i<=a/2;i++)
     {
         if(a%i==0)
         System.out.print(i+",");
        }
        System.out.print(a);
    }
}

To find the solution of a quadratic equation

Question 67 : Write a program in Java to find the solution of a quadratic equation by taking the inputs of a,b,c from the user.

Java Program :

import java.io.*;
class quadratic
{
 static void main()throws IOException
 {
     BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
     int a,b,c;
     double d,e;
     System.out.println("The format of a quadratic equation is given as follows : ");
     System.out.println("\nax2 + bx + c = 0");
     System.out.println("\nEnter values for a,b,c as follows : ");
     System.out.print("\na = ");
     a=Integer.parseInt(br.readLine());
     System.out.print("\nb = ");
     b=Integer.parseInt(br.readLine());
     System.out.print("\nc = ");
     c=Integer.parseInt(br.readLine());
     d=((-1*b)+java.lang.Math.sqrt((b*b)-(4*a*c)))/(2*a);
     e=((-1*b)-java.lang.Math.sqrt((b*b)-(4*a*c)))/(2*a);
     System.out.print("\nEquation : "+a+"x2");
     if(b<0)
     System.out.print(b+"x");
     else
     System.out.print("+"+b+"x");
     if(c<0)
     System.out.print(c);
     else
     System.out.print("+"+c);
     System.out.print(" = 0");
     System.out.println("\n\nSolution : x = ( "+d+" , "+e+" )");
    }
}
   

ShareThis