Friday, June 8, 2012

Write a program to sort existing array?


♣♣♣ WRITE A PROGRAM TO SORT EXISTING ARRAY. ♣♣♣


import java.io.*;
class pro7
{
            public static void main(String args[]) throws IOException
            {
            int cnt,last,i,j,temp;
            int arr[]=new int[10];
            InputStreamReader a=new InputStreamReader(System.in);
            BufferedReader b=new BufferedReader(a);
            System.out.print("Enter the total number of array : ");
            last=Integer.parseInt(b.readLine());
                        for(cnt=0;cnt
                        {         
                        System.out.print("Enter element"+(cnt+1)+" : ");
                        arr[cnt]=Integer.parseInt(b.readLine());        
                        }
                        for(i=1;i<=last;i++)
                        {
                                    for(j=1;j<=last;j++)
                                    {
                                                if(arr[j-1]
                                                {
                                                temp=arr[j-1];
                                                arr[j-1]=arr[j];
                                                arr[j]=temp;
                                                }
                                    }
                        }
                        System.out.println("Array after sorting is :");
                        for(cnt=0;cnt
                        {
                        System.out.print(arr[cnt]+"   ");         
                        }         
            }
}

OUTPUT :-

            Enter the total number of array : 5
Enter Element1 :23
Enter Element2 :12
Enter Element3 :45
Enter Element4 :33
Enter Element5 :21

Array after sorting is :
            45        33        23        21        12

Write a program to insert element in existing array.?


♣♣♣ WRITE A PROGRAM TO INSERT ELEMENT IN EXISTING ARRAY. ♣♣♣

import java.io.*;
class pro6
{
            public static void main(String args[]) throws IOException
            {
            int cnt,last,pos,num;
            int arr[]=new int[10];
            int newarr[]=new int[10];
            InputStreamReader a=new InputStreamReader(System.in);
            BufferedReader b=new BufferedReader(a);
            System.out.print("Enter the total number of array : ");
            last=Integer.parseInt(b.readLine());
                        for(cnt=0;cnt
                        {         
                        System.out.print("Enter element"+(cnt+1)+" : ");
                        arr[cnt]=Integer.parseInt(b.readLine());        
                        }
                     System.out.print("Enter the position to insert new element in an existing array :");
                        pos=Integer.parseInt(b.readLine());
                        System.out.print("Enter the element : ");
                        num=Integer.parseInt(b.readLine());
                        for(cnt=0;cnt
                        {         
                                    if(cnt+1==pos)
                                    newarr[cnt]=num;
                                    else
                                    newarr[cnt]=arr[cnt];
                        }
                        System.out.println("Array after inserting new element is :");
                        for(cnt=0;cnt
                        {
                        System.out.print(newarr[cnt]+"   ");  
                        }         
            }
}
OUTPUT :-

            Enter the total number of array : 5
Enter Element1 :23
Enter Element2 :12
Enter Element3 :45
Enter Element4 :33
Enter Element5 :21
Enter the position to insert new element in an existing array :2
Enter the element : 88
Array after inserting new element is :
            23        88        45        33        21

Write a program to display al?l prime numbers between 1 to 1000.


♣♣♣ WRITE A PROGRAM TO DISPLAY ALL PRIME NUMBERS BETWEEN 1 TO 1000. ♣♣♣


import java.io.*;
class pro5
{
            public static void main(String args[]) throws IOException
            {
            int num,cnt,last,flg;
            InputStreamReader a=new InputStreamReader(System.in);
            BufferedReader b=new BufferedReader(a);
            System.out.print("Enter the last number of range: ");
            last=Integer.parseInt(b.readLine());
            System.out.print("List of all Prime Number upto "+last+" : \n");
                        for(num=1;num<=last;num++)
                        {
                        flg=1;
                                    for(cnt=2;cnt<=num/2;cnt++)
                                    {         
                                                if(num%cnt==0)
                                                {
                                                flg=0;
                                                break;
                                                }
                                    }
                        if(flg==1)
                        System.out.print(num+"   ");
                        }
            }
}

  

OUTPUT :-

            Enter the last number of range: 50

List of all Prime Number upto 50:

            1          2          3          5          7          11        13        17        19        23
           29         31        37       41       43          47

WRITE A PROGRAM TO DISPLAY MULTIPLICATION TABLE.?


♣♣♣ WRITE A PROGRAM TO DISPLAY MULTIPLICATION TABLE. ♣♣♣

import java.io.*;
class pro4
{
            public static void main(String args[]) throws IOException
            {
            int num,cnt,sum;
            InputStreamReader a=new InputStreamReader(System.in);
            BufferedReader b=new BufferedReader(a);
            System.out.print("Enter any number : ");
            num=Integer.parseInt(b.readLine());
                        for(cnt=1;cnt<=10;cnt++)
                        {
                        sum=num*cnt;
                        System.out.println(num+" * "+cnt+" = "+ sum);
                        }
            }
}
  
OUTPUT :-

            Enter any number : 3
            3  *  1  =  3
3  *  2  =  6
            3  *  3  =  9
            3  *  4  =  12
            3  *  5  =  15
3  *  6  =  18
            3  *  7  =  21
            3  *  8  =  24
            3  *  9  =  27
3  *  10  =  30

WRITE A PROGRAM TO FIND THE SUM OF DIGITS OF A GIVEN NUMBER.?


♣♣ WRITE A PROGRAM TO FIND THE SUM OF DIGITS OF A GIVEN NUMBER. ♣♣



import java.io.*;
class pro3
{
            public static void main(String args[]) throws IOException
            {
            int num,rem,sum=0;
            InputStreamReader a=new InputStreamReader(System.in);
            BufferedReader b=new BufferedReader(a);
            System.out.print("Enter any numeric number : ");
            num=Integer.parseInt(b.readLine());
                        while(num!=0)
                        {
                        rem=num%10;
                        sum=sum+rem;
                        num=num/10;
                        }
            System.out.print("Sum of Number= "+ sum);
            }
}


OUTPUT :-

            Enter any numeric  number : 4652
            Sum of Number = 17

Write a program to reverse the string.?


♣♣♣ WRITE A PROGRAM TO REVERSE THE STRING. ♣♣♣


import java.io.*;
class pro2
{
            public static void main(String args[]) throws IOException
            {
            String s1;
            InputStreamReader a=new InputStreamReader(System.in);
            BufferedReader b=new BufferedReader(a);
            System.out.print("Enter any string : ");
            s1=b.readLine();
            int cnt=s1.length();
            System.out.println("Real String = "+s1);
            System.out.print("Reverse String= ");
            while(cnt>0)
            {
                        System.out.print(s1.charAt(cnt-1));
                          cnt--;
              }
              System.out.println();
            }
}


OUTPUT :-

            Enter any string : KAPIL
            Real String = KAPIL
            Reverse String = LIPAK

Write a program to check whether two strings are equal or not. ?


♣♣♣ WRITE A PROGRAM TO CHECK WHETHER TWO STRINGS ARE EQUAL OR    
NOT. ♣♣♣                            

import java.io.*;
class pro1
{
            public static void main(String args[]) throws IOException
            {
            String s1,s2;
            InputStreamReader a=new InputStreamReader(System.in);
            BufferedReader b=new BufferedReader(a);
            System.out.print("Enter First string : ");
            s1=b.readLine();
            System.out.print("Enter Second string : ");
            s2=b.readLine();
                        if(s1.equals(s2))
                        System.out.print("Equal");
                        else
                        System.out.print("Not Equal");
            }
}



OUTPUT :-

            Enter first string : My Name is Kapil
            Enter Second string : My  Name  is Kamia
            Not Equal


            Enter first string : My Name is Kapil
            Enter Second string : My Name is Kapil
            Equal