Wednesday, 9 October 2013

JAVA PROG 6

Week-3:
a)    Write a Java program that checks whether a given string is a palindrome or not.


Program:

import java.io.*;
class Palindrome
{
        public static void main(String args[]) throws IOException
        {
                int i,j;
                boolean flag=true;
                BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
                System.out.print("\nEnter String:");
                String s=br.readLine();
                j=s.length()-1;
                for(i=0;i<=j;i++)
                {
                    if(s.charAt(i)!=s.charAt(j--))
                                flag=false;

                }
                if(flag==false)         System.out.println("\n"+s+" Is Not Palindrome"); 
                else                          System.out.println("\n"+s+" Is Palindrome");
        }
}


Output:

Enter String: malayalam


malayalam Is Palindrome

JAVA PROGRAMS 5

a)    Write a Java Program that reads a line of integers, and then displays each integer, and the sum of all the integers (Use StringTokenizer class of java.util)

Program:

import java.io.*;
import java.util.*;

class SumOfInt
{
        public static void main(String args[]) throws IOException
        {
                int sum=0;
                BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
                System.out.print("\nEnter String Of Integers:");
                String s=br.readLine();
                StringTokenizer st=new StringTokenizer(s,",");
                while(st.hasMoreTokens())
                {
                        int i=Integer.parseInt(st.nextToken());
                        sum=sum+i;
                        System.out.println(i);
                }
                System.out.println("\nSum Of Integers Is: "+sum);

        }
}
Output:

Enter String Of Integers:12,5,3,1,2
12
5
3
1
2


Sum Of Integers Is: 23

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

FREE HIT COUNTERS