Wednesday, 9 October 2013

JAVA PROGRAMS 7

a)    Write a Java program for sorting a given list of names in ascending order.

Program:

import java.io.*;
class Ascend
{
        public static void main(String args[]) throws IOException
        {
                int n,i,j;
                String temp;
                BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
                System.out.print("\nEnter Number Of Names:");
                n=Integer.parseInt(br.readLine());
                String s[]=new String[n];
                System.out.println("Enter "+n+" Names:");
                for(i=0;i<n;i++)
                        s[i]=br.readLine();
                for(i=0;i<n;i++)
                        {
                                    for(j=0;j<n;j++)
                                    {
                                                if((s[j].compareTo(s[i]))>0)
                                                {
                                                            temp=s[i];
                                                            s[i]=s[j];
                                                            s[j]=temp;
                                                }
                                    }
                        }
                System.out.println("\nAscending Order Of Names:");
                        for(i=0;i<n;i++)
                                     System.out.println(s[i]);
        }
}

Output:

Enter Number Of Names:2
Enter 2 Names:
vineela
aswini
Ascending Order Of Names:
aswini

vineela

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

FREE HIT COUNTERS