Tuesday, December 27, 2011

Program to


import java.util.*;
public class Ch3Q9
{public static void main(String[] args)
  {Scanner kbd = new Scanner(System.in);
  System.out.print("Enter n: ");
  int n = kbd.nextInt();
  int [] x = new int [n];
  System.out.println("Enter elements: ");
  for(int i = 0; i < n; i++)
  x[i] = kbd.nextInt();
  for(int i = 0; i < n/2; i++)
     {int temp = x[i];
      x[i] = x[n-i-1];
      x[n-i-1] = temp;
     }
  System.out.println("Enter array is: ");
  for(int i = 0; i < n; i++)
  System.out.print(x[i]+"  ");
    }
}

Program to


import java.util.*;
public class Ch3Q8
{public static void main(String[] args)
  {int x [] = {30, 50, 20, 80, 100, 90, 60, 10, 40, 70};
  int y[];
  y = x;
  System.out.println("Arrays x and y equal is "+Arrays.equals(x, y));
  System.out.println("Array x is:");
  for(int i = 0; i < x.length; i++)
  System.out.print(x[i]+" ");
  System.out.println();
  Arrays.sort(y, 1, 5);
     System.out.println("Arrays x and y equal is "+Arrays.equals(x, y));
     System.out.println("Array y is:");
  for(int i = 0; i < y.length; i++)
  System.out.print(y[i]+" ");
  System.out.println();
  Arrays.sort(x);
  System.out.println("Array x is:");
  for(int i = 0; i < x.length; i++)
  System.out.print(x[i]+" ");
  System.out.println();
  int z[] = new int[10];
  Arrays.fill(z, 10);
  for(int i = 0; i < z.length; i++)
     {z[i] *= i; z[i]+=10;}
  System.out.println("Array z is:");
  for(int i = 0; i < z.length; i++)
  System.out.print(z[i]+" ");
  System.out.println();
  for(int no = 7; no <= 700; no*=10)
     {int pos = Arrays.binarySearch(x, no);
      System.out.println("position of "+no+" in x array is "+pos);
     }
  System.out.println("Arrays z and y equal is "+Arrays.equals(z, y));
  int z1[] = new int[5];
  System.arraycopy(z, 3, z1, 0, 5);
     System.out.println("Array z1 is:");
  for(int i = 0; i < z1.length; i++)
  System.out.print(z1[i]+" ");
  System.out.println();
    }
}

Friday, December 23, 2011

Program for factorial


import java.util.*;

public class Ch2Q40
{
    public static void main(String[] args)
    {Scanner kbd = new Scanner(System.in);
     System.out.print("Enter n and r: ");
     int n = kbd.nextInt();
     int r = kbd.nextInt();
     System.out.println("nCr = "+fact(n)/(fact(r)*fact(n-r)));
     System.out.println("nPr = "+fact(n)/fact(n-r));
    }
    static long fact(int n)
    {if(n==0)return 1;
     return n*fact(n-1);
    }
}

Program for


import java.util.*;
public class Ch2Q39a
{public static void main(String[] args)
    {int n;
     double x, sum = 0;
     Scanner kbd = new Scanner(System.in);
     System.out.print("Enter x: ");
     x = kbd.nextDouble();
     System.out.print("Enter no of terms: ");
     n = kbd.nextInt();
     for(int i = 0; i <= n; i++)
      sum += Math.pow(x, i)/fact(i);
     System.out.println("Sum = "+ sum);
    }
 static long fact(int n)
    {long f = 1;
     for(int i = 1; i <= n; i++)
      f *= i;
     return f;
    }
}

Program for


import java.util.*;
public class Ch2Q39
{
    public static void main(String[] args)
    {int n;
     double x, sum = 0, term = 1;
     Scanner kbd = new Scanner(System.in);
     System.out.print("Enter x: ");
     x = kbd.nextDouble();
     System.out.print("Enter no of terms: ");
     n = kbd.nextInt();
     for(int i = 1; i <= n; i++)
      {sum += term;
      term *= x/i;
      }
     System.out.println("Sum = "+ sum);
    }
}

Program for


import java.util.*;
public class Ch2Q38
{public static void main(String[] args)
    {int n;
     double sum = 0;
     Scanner kbd = new Scanner(System.in);
     System.out.print("Enter no of terms: ");
     n = kbd.nextInt();
     int sign = 1;
     for(int i = 1; i <= n; i++)
      {sum += sign*1.0/i;
      sign = -sign;
      }
     System.out.println("Sum = "+ sum);
    }
}

Program for


import java.util.*;
public class Ch2Q37
{public static void main(String[] args)
    {int n;
     double sum = 0;
     Scanner kbd = new Scanner(System.in);
     System.out.print("Enter number of terms: ");
     n = kbd.nextInt();
     for(int i = 1; i <= n; i++)
         sum += 1.0/i;
     System.out.println("Sum = "+ sum);
    }
}

Program for



import java.util.*;
public class Ch2Q35
{
    public static void main(String[] args)
    {int n, k = 1;
     Scanner kbd = new Scanner(System.in);
     System.out.print("Enter a integer: ");
     n = kbd.nextInt();
loop: for(int i = 1; ; i++)
         {for(int j = 1; j <= i; j++)
              {System.out.printf("%-3d\t", k++);
               if(k>n)break loop;
              }
          System.out.println();
         }
    }
}

Program for pattern


import java.util.*;

public class Ch2Q34
{
 public static void main(String[] args)
  {int n = 3;
  for(int i = 1; i <= n; i++)
        {for(int j = 1; j <= 4 - i; j++)
      System.out.print("  ");
         for(int j = 1; j <= 2*i-1; j++)
      System.out.print(" *");
      System.out.println();
        }
     for(int i = n-1; i >= 1; i--)
        {for(int j = 1; j <= 4 - i; j++)
      System.out.print("  ");
         for(int j = 1; j <= 2*i-1; j++)
      System.out.print(" *");
      System.out.println();
        }
    }
}

Program to


import java.util.*;

public class Ch2Q33
{
 public static void main(String[] args)
  {
     int n = 5;//n indicates number of lines
  for(int i = 1; i <=n; i++)
        {for(int j = 1; j <= i; j++)
      System.out.print("  ");
         for(int j = 1; j <= n-i+1; j++)
      System.out.print("* ");
      System.out.println();
        }
   }
}
/*       __* * * * *
 *       ____* * * *
         ______* * *                   */

Program to


import java.util.*;
public class Ch2Q32
{
    public static void main(String[] args)
    {int n;
     for(int i = 1; i <= 5; i++)
         {for(int j = 1; j <= i; j++)
              System.out.printf("%-3d\t", (i+j+1)%2);
          System.out.println();
         }
    }
}

Program to


import java.util.*;
public class Ch2Q31
{
    public static void main(String[] args)
    {int n, k = 1;
     Scanner kbd = new Scanner(System.in);
     System.out.print("Enter a integer: ");
     n = kbd.nextInt();
     System.out.print("Enter the pattern no(1 or 2 or 3 or 4 to Quit): ");
     int choice = kbd.nextInt();
     while(choice !=4)
     {
     switch(choice)
     {case 1: pattern1(n); break;
      case 2: pattern2(n); break;
      case 3: pattern3(n);
     }
      System.out.print("Enter the pattern no(1 or 2 or 3 or 4 to Quit): ");
   choice = kbd.nextInt();
     }
   
    }
    static void pattern1(int n)
    {for(int i = 1; i <= n; i++)
         {for(int j = 1; j <= i; j++)
              System.out.printf("%-3d\t", i);
          System.out.println();
         }
    }
   
    static void pattern2(int n)
    {for(int i = 1; i <= n; i++)
         {for(int j = 1; j <= i; j++)
              System.out.printf("%-3d\t", j);
          System.out.println();
         }
    }
   
    static void pattern3(int n)
    {for(int i = 1; i <= n; i++)
         {for(int j = 1; j <= n+1-i; j++)
              System.out.printf("    ");
          for(int j = 1; j <= i; j++)
              System.out.printf("%-4d\t", j);
          System.out.println();
         }
    }
}

Program to


import java.util.*;
public class Ch2Q30
{
    public static void main(String[] args)
    {double no, max, min, sum;
    int count;
    Scanner kbd = new Scanner(System.in);
    System.out.print("Enter a number: ");
    no = kbd.nextDouble();
    if(no < 0)
       System.out.println("No data");
    else {count = 1; max = min = sum = no;
          System.out.print("Enter a number: ");
          no = kbd.nextDouble();
          while(no >= 0)
               {if(no > max)
                max = no;
                else if(no < min)
                   min = no;
                sum += no; count++;
                System.out.print("Enter a number: ");
                no = kbd.nextDouble();
               }
          System.out.println("Maximum = "+max+"\nMinimum = "+min);
          System.out.println("Average = "+ sum / count);
            }
     }
}

Program to


import java.util.*;
public class Ch2Q29
{public static void main(String[] args)
      {int n, marks, counta = 0, countb = 0, countc= 0,countd = 0;
       Scanner kbd = new Scanner(System.in);
       System.out.print("How many students in the class: ");
       n = kbd.nextInt();
       for(int i = 1; i <= n ; i++)
       {System.out.print("Enter marks of student"+i+": ");
        marks = kbd.nextInt();
        switch((marks-1)/20)
        {case 4:counta++; break;
         case 3:countb++; break;
         case 2:countc++; break;
         case 1: case 0 :countd++; break;
         default: System.out.println("You are nuts\n");
        }
        }
        System.out.println("\nNo of students in the range of 81..100 are "+ counta);
        System.out.println("No of students in the range of 61..80 are "+ countb);
        System.out.println("No of students in the range of 41..60 are "+ countc);
        System.out.println("No of students in the range of 00..40 are "+ countd);
       
      }
}

Program to


import java.util.*;
public class Ch2Q28
{
    public static void main(String[] args)
    {Scanner kbd = new Scanner(System.in);
    System.out.print("Enter an integer: ");
    int n = kbd.nextInt();
    int no = 1, count = 0;
    while (count < n)    
          {if(armstrong(no))
          {System.out.println(no);
          count++;
          }
       no++;
               }
    }
        static boolean armstrong(int no)
        {int no1 = no, sum = 0;
    while(no != 0)
         { int dig = no % 10;
           sum += dig*dig*dig;
           no /= 10;
         }
    return sum == no1;
        }
}

Program to


import java.util.*;
public class Ch2Q27
{
    public static void main(String[] args)
    {Scanner kbd = new Scanner(System.in);
    System.out.print("Enter an integer: ");
    int n = kbd.nextInt();
    for(int no = 1; no < n; no++)
    if(armstrong(no))
      System.out.println(no);
        }
        static boolean armstrong(int no)
        {int no1 = no, sum = 0;
    while(no != 0)
         { int dig = no % 10;
           no /= 10;
           sum += Math.pow(dig, 3);
         }
    if(sum == no1)return true;
    return false;  
        }
}

Program to check whether a number is armstong or not


import java.util.*;
public class Ch2Q26
{
    public static void main(String[] args)
    {Scanner kbd = new Scanner(System.in);
    System.out.print("Enter an integer: ");
    int no = kbd.nextInt();
    if(amg(no))
       System.out.println("Armstrong No");
    else System.out.println("Not an Armstrong No");
         }
       
        static boolean amg(int no)
        {int no1 = no;
    int sum = 0;
    while(no != 0)
         { int dig = no % 10;
           no /= 10;
           sum += Math.pow(dig, 3);
         }
    return (sum == no1);
    }
}






Program to


public class Ch2Q25
{
    public static void main(String[] args)
    {double mohan = 1000, vidya = 750;
    int yr = 0;
    while(vidya <= mohan)
         { mohan += 100;
           vidya += 0.075 * vidya;
           yr++;
         }
    System.out.println("\nNo of years= "+yr);
        }
}

Program to


import java.util.*;
public class Ch2Q24
{
    public static void main(String[] args)
    {Scanner kbd = new Scanner(System.in);
    int no, count = 0;
    System.out.print("Enter the integer: ");
    no = kbd.nextInt();
    while(no != 1)
         { if(no%2 != 0)
         no = 3*no + 1;
           else no/=2;
           System.out.print(no+ "  ");
           count++;
         }
    System.out.println("\nNo of iterations = "+count);
        }
}

Program to


import java.util.*;
public class Ch2Q23
{
    public static void main(String[] args)
    {Scanner kbd = new Scanner(System.in);
    int no;
    System.out.print("Enter the integer: ");
    no = kbd.nextInt();
    int dig, rev = 0, count = 0;
    while(no != 0)
         {dig = no % 10;
          rev = rev* 10 + dig;
          no = no/10;
         }
    no = rev;
    for(int i = 1; i<= 4; i++)
       {dig = no % 10;
        System.out.print(dig+"  ");
        no = no/10;
       }
        }
}

program to


import java.util.*;
public class Ch2Q22
{
    public static void main(String[] args)
    {Scanner kbd = new Scanner(System.in);
    int no;
    System.out.print("Enter the integer: ");
    no = kbd.nextInt();
    int i = 2;
    while(no != 1)
    if(no%i == 0)
      {System.out.print(i+" "); no/=i;}
    else i++;
        }
}

program to


import java.util.*;
public class Ch2Q21
{
    public static void main(String[] args)
    {Scanner kbd = new Scanner(System.in);
    int n1, n2, no, sum = 0, count = 0;
    System.out.print("Enter two integers: (n1 <n2): ");
    n1 = kbd.nextInt(); n2 = kbd.nextInt();
    for (no = n1+1; no < n2; no++)
    if(no%7 == 0)
      {sum += no; count++;}
    System.out.println("Count = "+count+"\tSum = "+sum);
        }
}

Program to calcute speed


import java.util.*;

public class Ch2Q20
{
 public static void main(String[] args)
  {Scanner kbd = new Scanner(System.in);
  final double g = 9.81;
  System.out.print("Enter initial distance: ");
  double s0 = kbd.nextDouble();
  System.out.print("Enter initial velocity: ");
  double v0 = kbd.nextDouble();
     double s = s0+v0+g/2;
     System.out.printf("%d\t\t%6.2f\n", 1, s);
     for(int t=5; t<=100; t+=5)
        {s = s0+v0*t+g*t*t/2;
        System.out.printf("%d\t\t%6.2f\n", t, s);
        }
    }
}

Program to calculate simple and compound interest


import java.util.*;
public class ch2q19
{
    public static void main(String[] args)
    {double rate, sum = 0;
     int n;
     Scanner kbd = new Scanner(System.in);
     System.out.print("Enter interest rate and no of years: ");
     rate = kbd.nextDouble(); n = kbd.nextInt();
     for(int i = 1; i <= n; i++)
      sum += Math.pow(1+rate/100, i);
     System.out.print("What do you want? Enter 1 for Final amount 2 for Annual deposit: ");
     int choice = kbd.nextInt();
     if(choice == 1)
       {System.out.print("Enter annual deposit: ");
        double amt = kbd.nextDouble();
        System.out.printf("Final amount is %.2f\n", amt*sum);
       }
     else {System.out.print("Enter final amount: ");
        double famt = kbd.nextDouble();
        System.out.printf("Annual Deposit is %.2f\n", famt/sum);
       }
     
    }
}

Program to



public class output2
{    public static void main(String[] args)
{ int sum = 0, i = 2, j = 0, k = 0;
      for (sum = 0, i = 2; i <= 8; i += 2)
          {j = 1;
           while ( j < 4)
                 { k = j;
                   do {sum ++;
                       k += 2;
                      } while (k <= 3);
                   j++;
                 }
          }
     System.out.printf ("%d %d %d %d\n", sum, i, j, k);

     }
}

Program to



class output1
{    public static void main(String[] args)
{ int i = 0, x = 0 ;
 for (i = 1;  i < 10; ++i)
          {if (i%2  == 1)
               x += i ;
           else x--;
           System.out.println(x);
          }
      System.out.printf ("\n x = % d", x);
   
    }
}

Program to check whether how many days does the month have


import java.util.*;
class Ch2Q12
{
    public static void main(String[] args)
    {
        Scanner kbd = new Scanner (System.in);
        System.out.print("Enter the month no: ");
        int mno = kbd.nextInt();
        switch(mno)
        {case 1:case 3: case 5: case 7: case 8 : case 10 : case 12:
        System.out.println("No of days are 31"); break;
         case 4: case 6: case 9: case 11:
          System.out.println("No of days are 30"); break;
         case 2: System.out.print("Enter the year: ");
                 int yr = kbd.nextInt();
                 if(leapyr(yr))
                  System.out.println("No of days are 29");
                 else System.out.println("No of days are 28");break;
         default: System.out.println("Are you Crazy\n");
        }
    }
   static boolean leapyr(int yr)
    {if(yr%4 == 0)
    if(yr < 1582)
    return true;
    else if(yr%100 == 0)
       if(yr % 400 == 0)
        return true;
       else return false;
    else return true;
     else return false;
    }  
}

program to


import java.util.*;

public class Ch2Q10c
{
    public static void main(String[] args)
    {Scanner kbd = new Scanner(System.in);
     System.out.print("Enter x: ");
     double x = kbd.nextDouble();
     double y;
     y = x>0?1:(x<0?-1:0);
     System.out.println("x = "+x+"\ny= "+y);
    }
}

Program to


import java.util.*;

public class Ch2Q10b
{
    public static void main(String[] args)
    {Scanner kbd = new Scanner(System.in);
     System.out.print("Enter x: ");
     double x = kbd.nextDouble();
     double y = 0;
     if(x>0)y = 1;
     else if(x<0)y = -1;
     System.out.println("x = "+x+"\ny= "+y);
    }
}

Program to


import java.util.*;

public class Ch2Q10a
{
    public static void main(String[] args)
    {Scanner kbd = new Scanner(System.in);
     System.out.print("Enter x: ");
     double x = kbd.nextDouble();
     double y = 0;
     if(x>0)y = 1;
     if(x<0)y = -1;
     System.out.println("x = "+x+"\ny= "+y);
    }
}

Program to check whether the triangle in Equilateral, isosceles or scalene


import java.util.*;
public class Ch2Q7
{
    public static void main(String[] args)
    {Scanner kbd = new Scanner(System.in);
     double a, b, c;
     System.out.print("Enter 3 sides: ");
     a = kbd.nextDouble();
     b = kbd.nextDouble();
     c = kbd.nextDouble();
     if(a+b>c&&b+c>a&&a+c>b)
      if(a== b && b== c)
       System.out.println("Equilateral");
      else if(a==b||b==c||a==c)
         System.out.println("Isoscales");
      else System.out.println("Scalene");
     else System.out.println("Not Possible");
    }
}

Program to check if a year is leap year or not


import java.util.*;
public class Ch2Q4
{    public static void main(String[] args)
    {Scanner kbd = new Scanner(System.in);
     int yr;
     System.out.print("enter year: ");
     yr = kbd.nextInt();
     if(yr < 1582)
      if(yr%4==0)
       System.out.println("Leap year");
      else System.out.println("not a Leap year");
     else if(yr%400 == 0||yr%4 == 0 && yr % 100 != 0)
         System.out.println("Leap year");
       else System.out.println("Not aLeap year");
     }
}

Program in java for to find coordinates of a circle


import java.util.*;
public class Ch2Q3
{    public static void main(String[] args)
     {double y, x1, y1, rad;
      Scanner kbd = new Scanner(System.in);
      System.out.print("Enter co-ordinates of the centre: ");
      double x = kbd.nextDouble();
      y = kbd.nextDouble();
      System.out.print("Enter radius of the circle: ");
      rad = kbd.nextDouble();
      System.out.print("Enter co-ordinates of the point: ");
      x1 = kbd.nextDouble();
      y1 = kbd.nextDouble();
      double dist = Math.sqrt((x-x1)*(x-x1)+(y-y1)*(y-y1));
      if(dist < rad)
      System.out.println("Point is inside the circle");
      else if(dist == rad)
              System.out.println("Point is on the circle");
           else System.out.println("Point is outside the circle");
     }
}

Wednesday, December 21, 2011

Java program for side of a square


import java.util.*;

public class Sideofsquare
{
 public static void main(String[] args)
  {Scanner kbd = new Scanner(System.in);
   System.out.print("Enter Co-ordinates of first vertex: ");
   double x1 = kbd.nextDouble();
   double y1 = kbd.nextDouble();
   System.out.print("Enter Co-ordinates of second vertex: ");
   double x2 = kbd.nextDouble();
   double y2 = kbd.nextDouble();
   System.out.print("Enter Co-ordinates of third vertex: ");
   double x3 = kbd.nextDouble();
   double y3 = kbd.nextDouble();
   double a = Math.sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
   double b = Math.sqrt((x1-x3)*(x1-x3)+(y1-y3)*(y1-y3));
   double c = Math.sqrt((x3-x2)*(x3-x2)+(y3-y2)*(y3-y2));
   double s = (a+b+c)/2;
   double area = Math.sqrt(s*(s-a)*(s-b)*(s-c));
   double sarea = area;
   System.out.println("Side of square = " + Math.sqrt(sarea));
  }
}

Calculate Frequency Using java



import java.util.*;

public class Ch1Q38
{
 public static void main(String[] args)
  {Scanner kbd = new Scanner(System.in);
  System.out.print("Enter inductance, capacitance and resistance: ");
  double l = kbd.nextDouble();
  double c = kbd.nextDouble();
  double r = kbd.nextDouble();
  double f = Math.sqrt(1/l/c - r*r/4/c/c);
  System.out.println("Freq = " + f);
    }
}


Java program for finding area and volume of a circle


import java.util.*;

public class Ch1Q37
{
 public static void main(String[] args)
  {
  double sarea, vol;
  System.out.print("Enter radius of a sphere: ");
  Scanner kbd = new Scanner(System.in);
  double rad = kbd.nextDouble();
  sarea = 4*Math.PI*rad*rad;
     vol = Math.PI*rad*rad*rad*4/3;
  System.out.println("Surface area = " + sarea);
     System.out.println("Volume = " + vol);  
    }
}

Java program to convert weight in kg to lb oz and drams


import java.util.*;

public class Ch1Q36x
{
 public static void main(String[] args)
  {Scanner kbd = new Scanner(System.in);
  System.out.print("Enter wt in Kg: ");
  double kg = kbd.nextDouble();
  double lb = kg*2.20462;
  double oz = lb%1*16;
  double dr = oz%1*16;
  System.out.println((int)lb + " lbs " +
                 (int)oz + " Oz and " +
                 dr + " drams");
    }
}

Java Program for Farenheit to celsius conversion


import java.util.*;

public class Ch1Q35
{
 public static void main(String[] args)
  {
  System.out.print("Enter temp in Farenheit: ");
  Scanner kbd = new Scanner(System.in);
  double faren = kbd.nextDouble();
  double cel = (faren-32)/1.8;
  System.out.println("Equivalent Temp = " + cel);
    }
}