Wednesday, 9 October 2013

JAVA PROGRAMS 4

D)    Write a Java program to multiply two given matrices.

Program:
import java.io.*;
class MatrixMul
{
        public static void main(String args[]) throws IOException
        {
                int r1,c1,r2,c2,i,j,k;
                BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
                System.out.print("Enter No. Of Rows & Columns for Matrix 1:");
                r1=Integer.parseInt(br.readLine());
                c1=Integer.parseInt(br.readLine());
                System.out.print("Enter No. Of Rows & Columns for Matrix 2:");
                r2=Integer.parseInt(br.readLine());
                c2=Integer.parseInt(br.readLine());
                if(r2==c1)
                {
                        int a[][]=new int[r1][c1];
                        int b[][]=new int[r2][c2];
                        int c[][]=new int[r1][c2];
                        System.out.println("Enter Values for Matrix 1:");
                        for(i=0;i<r1;i++)
                        {
                          for(j=0;j<c1;j++)
                                a[i][j]=Integer.parseInt(br.readLine());
                        }
                        System.out.println("Enter Values for Matrix 2:");
                        for(i=0;i<r2;i++)
                        {
                          for(j=0;j<c2;j++)
                                b[i][j]=Integer.parseInt(br.readLine());
                        }
                        System.out.println("MATRIX MULTIPLICATION RESULT:");
                        for(i=0;i<r1;i++)
                        {
                          for(j=0;j<c2;j++)
                          {
                                c[i][j]=0;
                                for(k=0;k<r2;k++)
                                       c[i][j]=(a[i][k]*b[k][j])+c[i][j];
                          }
                        }
                        for(i=0;i<r1;i++)
                        {
                          for(j=0;j<c2;j++)
                                System.out.print(" "+c[i][j]);
                          System.out.println();
                        }

                }
                else   System.out.print("Invalid ORDER Of Matrices");

       }
 }

Output:

Enter No. Of Rows & Columns for Matrix 1:2
3
Enter No. Of Rows & Columns for Matrix 2:3
2
Enter Values for Matrix 1:
1
2
3
4
5
6
Enter Values for Matrix 2:
1
2
3
4
5
6
MATRIX MULTIPLICATION RESULT:
 22  28

 49  64

JAVA PROGRAMS 3

C)    Write a Java program that prompts the user for an integer and then prints out all prime numbers up to that integer.

Program:
import java.io.*;
class Prime
{
         int i,j,count;
         void seq(int n)
         {
                System.out.print("The Prime Numbers Upto "+n+" Are: ");
                for(i=1;i<=n;i++)
                        {
                        count=0;
                        for(j=1;j<=i;j++)
                                    {
                                                if(i%j == 0)
                                                            count++;
                                    }
                                    if(count==2)
                                                System.out.print(" "+i);
                        }                               
         }
         public static void main(String args[]) throws IOException
         {
           int n;
           Prime ob=new Prime();
           BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
           System.out.print("\nEnter The Value Of n: ");
           n=Integer.parseInt(br.readLine());
           ob.seq(n);
         }
}       

Output:
Enter The Value Of n: 21

The Prime Numbers Upto 21 Are: 2  3  5  7  11  13  17  19

JAVA PROGRAMS 2

B)      The Fibonacci sequence is defined by the following rule:
The first two values in the sequence are 1 and 1. Every subsequent value is the sum of the two values preceding it. Write a Java program that uses both recursive and non recursive functions to print the nth value in the Fibonacci sequence.

Program:
import java.io.*;
class Fibo
{
         int nonRecursive(int n1)
         {
                int f3=0,f1=0,f2=1;
                if(n1==0 || n1==1)        return n1;
                else
                {
                        int count=2;
                        while(count<=n1)
                        {
                                f3=f1+f2;
                                f1=f2;
                                f2=f3;
                                count++;
                        }
                        return f3;
                }
         }
         int recursive(int n1)
         {
                if(n1==0 || n1==1)               return n1;
                else                                      return recursive(n1-1)+recursive(n1-2);
         }

         public static void main(String args[]) throws IOException
         {
           int n;
           Fibo ob=new Fibo();
           BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
           System.out.print("\nEnter The Value Of n: ");
           n=Integer.parseInt(br.readLine());
           System.out.println("RECURSIVE METHOD");
           System.out.println(+n+"th Value In Fibonacci Sequence Is:"+ob.recursive(n));
           System.out.println("\nNON RECURSIVE METHOD ");
           System.out.println(+n+"th Value In Fibonacci Sequence Is:"+ob.nonRecursive(n));
          }
}     


Output:
E:\Lab>java Fibo
Enter The Value Of n: 5
RECURSIVE METHOD
5th Value In Fibonacci Sequence Is:5
NON RECURSIVE METHOD

5th Value In Fibonacci Sequence Is:5

JAVA PROGRAMS

a)      Write a Java program that prints all real solutions to the quadratic equation ax2 + bx +c = 0. Read in a, b, c and use the quadratic formula. If the discriminant b2- 4ac is negative, display a message stating that there are no real solutions.

Program:

import java.io.*;
class SolvingEquation
{
        int desc;
       double r1,r2;
       void solution(int a,int b,int c)
       {
           desc=(b*b)-(4*a*c);
           if(desc>0)
           {
                  r1=(-b+Math.sqrt(desc))/(2*a);
                  r2=(-b-Math.sqrt(desc))/(2*a);
                  System.out.print("\nThe Roots Are: "+r1+"\t"+r2);
           }
           else if(desc==0)           System.out.print("\nThe Roots Are Equal:"+(-b/(2*a)));
           else                               System.out.print("\nThere Are No Real Solutions");               
       }
       public static void main(String args[]) throws IOException
       {
           int a,b,c;
           SolvingEquation obj=new SolvingEquation();
           BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
           System.out.print("\nEnter value of a:");
           a=Integer.parseInt(br.readLine());
           System.out.print("\nEnter value of b:");
           b=Integer.parseInt(br.readLine());
           System.out.print("\nEnter value of c:");
           c=Integer.parseInt(br.readLine());
           obj.solution(a,b,c);
        }
}      



Output:
E:\Lab>javac SolvingEquation.java
E:\Lab>javac SolvingEquation.java
Enter value of a:2
Enter value of b:3
Enter value of c:1

The Roots Are: -0.5     -1.0

Tuesday, 8 October 2013

Soon, apply for passport from your phone.......!!!!!!!

CHENNAI: Soon there will be a smart way to apply for passports. The agency of external affairs is in the final stages of introduction an app that will allow people to concern and pay the charge for passports on their mobile phones.

The new app will be an upgrade edition of mPassport Seva that is available for 
Android, iOSand Windows Phone platform. mPassport Seva currently allows users to find general in sequence, locate nearest passport centre and police station, calculate fees and track the status of applications. More features like provision to file application and pay fees will be added toit.

"The capability to help applicants fill passports and make compensation through the app should be available in one to one-and-a-half months," said Golok Kumar Simli, principal consultant and head (technology), ministry of external affairs. Applicants will be able to log in, file the application and track its status but will have to carry hard copies of the documents required to the passport seva kendra after an ARN number is generated and an appointment fixed, he said. Citing security concerns, he, however, refused to explain how the app will work.

FREE HIT COUNTERS