Skip to main content

What is Divide And Conquer Technique

Divide and Conquer is a programming technique which makes the program more efficient to write. And this technique work on the concept of recursion to solve a problem step by step. Generally this technique work in three parts:- Divide:-  Divide the problem into some subproblem. Conquer:-  Conquer the subproblem by calling recursively until subproblem solved. Combine:-  (Optional Step) Combine the subproblem solution. So, that we will get the final problem Solution.  When the subproblems are large enough to solve recursively, we call the recursive case. Once the subproblem becomes small enough that we no longer recursive, we say that the recursion "bottom out" and that we have gotten down to the base case.                          Application of Divide and Conquer Quick Sort Strassen's algorithm for matrix multiplication Merge Sort Counting inversions Binary Search Finding Min and max Divide and Conquer Abstract Algorithm DAC(a,i,j) {   

Number to Word convert Program in Java


package numbertoword;

import java.util.*;

/**
 *
 * @author Rishabh
 */
public class NumberToWord {

    /**
     * @param args the command line arguments
     */
    static int count,i=0,position=0;
    static String arr[];
    public void odd(){}
    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
            String number=sc.next();
            int numlength=number.length();
            if(numlength==11||numlength==10)
            {  arr=new String[6];
                arr[0]="Arab";
                arr[1]="Crore";
                arr[2]="Lakh";
                arr[3]="Thousand";
                arr[4]="Hundred";
                arr[5]="";
            }
            if(numlength==9||numlength==8)
            {  arr=new String[5];
                arr[0]="Crore";
                arr[1]="lakh";
                arr[2]="thousand";
                arr[3]="hundred";
                arr[4]="";
            }
            if(numlength==7||numlength==6)
            {  arr=new String[4];
                arr[0]="lakh";
                arr[1]="thousand";
                arr[2]="hundred";
                arr[3]="";
            }
            if(numlength==5||numlength==4)
            {  arr=new String[3];
                arr[0]="thousand";
                   arr[1]="hundred";
                   arr[2]="";
            }
            if(numlength==3)
            {  arr=new String[2];
                   arr[0]="hundred";
                   arr[1]="";
            }
            while(numlength>0){
                if((numlength%2==1)&&(numlength>3))
                {
                        if('0'==number.charAt(position))
                        {
                           position=position+1;
                           numlength=numlength-1;
                        }
                        else{
                                if('1'==number.charAt(position))
                                { 
                                    String gnum=number.substring(position,position+2);
                                    switch(gnum)
                                    { 
                                        case "10":
                                              System.out.print("Ten");
                                            System.out.print("  "+arr[i]);
                                            i++;
                                            position=position+2;
                                            numlength=numlength-2;
                                        break; 
                                        case "11":
                                            System.out.print("Eleven");
                                            System.out.print("  "+arr[i]);
                                            i++;
                                            position=position+2;
                                            numlength=numlength-2;
                                        break;
                                        case "12":
                                            System.out.print("Twelve");
                                            System.out.print("  "+arr[i]);
                                            i++;
                                            position=position+2;
                                            numlength=numlength-2;
                                        break;
                                        case "13":
                                            System.out.print("Thirteen");
                                            System.out.print("  "+arr[i]);
                                            i++;
                                            position=position+2;
                                            numlength=numlength-2;
                                        break;
                                        case "14":
                                            System.out.print("Fourteen");
                                            System.out.print("  "+arr[i]);
                                            i++;
                                            position=position+2;
                                            numlength=numlength-2;
                                        break;   
                                        case "15":
                                            System.out.print("Fifteen");
                                            System.out.print("  "+arr[i]);
                                            i++;
                                            position=position+2;
                                            numlength=numlength-2;
                                        break;
                                        case "16":
                                            System.out.print("Sixteen");
                                            System.out.print("  "+arr[i]);
                                            i++;
                                            position=position+2;
                                            numlength=numlength-2;
                                        break;   
                                        case "17":
                                            System.out.print("Seventeen");
                                            System.out.print("  "+arr[i]);
                                            i++;
                                            position=position+2;
                                            numlength=numlength-2;
                                        break;   
                                        case "18":
                                            System.out.print("Eighteen");
                                            System.out.print("  "+arr[i]);
                                            i++;
                                            position=position+2;
                                            numlength=numlength-2;
                                        break;   
                                        case "19":
                                            System.out.print("Nineteen");
                                            System.out.print("  "+arr[i]);
                                            i++;
                                            position=position+2;
                                            numlength=numlength-2;
                                        break;   
                                        default :
                                            System.out.print("Some error occur");
                                    }
                                } 
                                else if('2'==number.charAt(position))
                                { 
                                     System.out.print("Twenty");
                                            position=position+1;
                                            numlength=numlength-1;
                                   
                                }
                                else if('3'==number.charAt(position))
                                { 
                                     System.out.print("Thirty");
                                            position=position+1;
                                            numlength=numlength-1;
                                   
                                }
                                else if('4'==number.charAt(position))
                                { 
                                     System.out.print("Forty");
                                            position=position+1;
                                            numlength=numlength-1;
                                   
                                }
                                else if('5'==number.charAt(position))
                                { 
                                     System.out.print("Fifty");
                                            position=position+1;
                                            numlength=numlength-1;
                                   
                                }
                                else if('6'==number.charAt(position))
                                { 
                                     System.out.print("Sixty");
                                            position=position+1;
                                            numlength=numlength-1;
                                   
                                }
                                else if('7'==number.charAt(position))
                                { 
                                     System.out.print("Seventy");
                                            position=position+1;
                                            numlength=numlength-1;
                                   
                                }
                                else if('8'==number.charAt(position))
                                { 
                                     System.out.print("Eighty");
                                            position=position+1;
                                            numlength=numlength-1;
                                   
                                }
                                else if('9'==number.charAt(position))
                                { 
                                     System.out.print("Ninty");
                                            position=position+1;
                                            numlength=numlength-1;
                                   
                                }
                            }
                } else if(((numlength%2==0)&&(numlength>2))||numlength==3){
                        if('0'==number.charAt(position))
                        {
                            position=position+1;
                            numlength=numlength-1;
                            i++;
                        }
                        else if('1'==number.charAt(position))
                        {   System.out.print("one");
                            System.out.print("  "+arr[i]);
                            i++;
                            position=position+1;
                            numlength=numlength-1;
                        }
                        else if('2'==number.charAt(position))
                        {   System.out.print("Two");
                            System.out.print("  "+arr[i]);
                            i++;
                            position=position+1;
                            numlength=numlength-1;
                        }
                        else if('3'==number.charAt(position))
                        {   System.out.print("Three");
                            System.out.print("  "+arr[i]);
                            i++;
                            position=position+1;
                            numlength=numlength-1;
                        }
                        else if('4'==number.charAt(position))
                        {   System.out.print("Four");
                            System.out.print("  "+arr[i]);
                            i++;
                            position=position+1;
                            numlength=numlength-1;
                        }
                        else if('5'==number.charAt(position))
                        {   System.out.print("Five");
                            System.out.print("  "+arr[i]);
                            i++;
                            position=position+1;
                            numlength=numlength-1;
                        }
                        else if('6'==number.charAt(position))
                        {   System.out.print("Six");
                            System.out.print("  "+arr[i]);
                            i++;
                            position=position+1;
                            numlength=numlength-1;
                        }
                        else if('7'==number.charAt(position))
                        {   System.out.print("Seven");
                            System.out.print("  "+arr[i]);
                            i++;
                            position=position+1;
                            numlength=numlength-1;
                        }
                        else if('8'==number.charAt(position))
                        {   System.out.print("Eight");
                            System.out.print("  "+arr[i]);
                            i++;
                            position=position+1;
                            numlength=numlength-1;
                        }
                        else if('9'==number.charAt(position))
                        {   System.out.print("Nine");
                            System.out.print("  "+arr[i]);
                            i++;
                            position=position+1;
                            numlength=numlength-1;
                        }
                       
                }else if(numlength<3){
                                if(numlength==2)
                                {
                                 if('0'==number.charAt(position))
                        {
                           position=position+1;
                           numlength=numlength-1;
                        }
                        else{
                                if('1'==number.charAt(position))
                                { 
                                    String gnum=number.substring(position,position+2);
                                    switch(gnum)
                                    { 
                                        case "10":
                                              System.out.print("Ten");
                                            System.out.print("  "+arr[i]);
                                            i++;
                                            position=position+2;
                                            numlength=numlength-2;
                                        break; 
                                        case "11":
                                            System.out.print("Eleven");
                                            System.out.print("  "+arr[i]);
                                            i++;
                                            position=position+2;
                                            numlength=numlength-2;
                                        break;
                                        case "12":
                                            System.out.print("Twelve");
                                            System.out.print("  "+arr[i]);
                                            i++;
                                            position=position+2;
                                            numlength=numlength-2;
                                        break;
                                        case "13":
                                            System.out.print("Thirteen");
                                            System.out.print("  "+arr[i]);
                                            i++;
                                            position=position+2;
                                            numlength=numlength-2;
                                        break;
                                        case "14":
                                            System.out.print("Fourteen");
                                            System.out.print("  "+arr[i]);
                                            i++;
                                            position=position+2;
                                            numlength=numlength-2;
                                        break;   
                                        case "15":
                                            System.out.print("Fifteen");
                                            System.out.print("  "+arr[i]);
                                            i++;
                                            position=position+2;
                                            numlength=numlength-2;
                                        break;
                                        case "16":
                                            System.out.print("Sixteen");
                                            System.out.print("  "+arr[i]);
                                            i++;
                                            position=position+2;
                                            numlength=numlength-2;
                                        break;   
                                        case "17":
                                            System.out.print("Seventeen");
                                            System.out.print("  "+arr[i]);
                                            i++;
                                            position=position+2;
                                            numlength=numlength-2;
                                        break;   
                                        case "18":
                                            System.out.print("Eighteen");
                                            System.out.print("  "+arr[i]);
                                            i++;
                                            position=position+2;
                                            numlength=numlength-2;
                                        break;   
                                        case "19":
                                            System.out.print("Nineteen");
                                            System.out.print("  "+arr[i]);
                                            i++;
                                            position=position+2;
                                            numlength=numlength-2;
                                        break;   
                                        default :
                                            System.out.print("Some error occur");
                                    }
                                } 
                                else if('2'==number.charAt(position))
                                { 
                                     System.out.print("Twenty");
                                            position=position+1;
                                            numlength=numlength-1;
                                   
                                }
                                else if('3'==number.charAt(position))
                                { 
                                     System.out.print("Thirty");
                                            position=position+1;
                                            numlength=numlength-1;
                                   
                                }
                                else if('4'==number.charAt(position))
                                { 
                                     System.out.print("Forty");
                                            position=position+1;
                                            numlength=numlength-1;
                                   
                                }
                                else if('5'==number.charAt(position))
                                { 
                                     System.out.print("Fifty");
                                            position=position+1;
                                            numlength=numlength-1;
                                   
                                }
                                else if('6'==number.charAt(position))
                                { 
                                     System.out.print("Sixty");
                                            position=position+1;
                                            numlength=numlength-1;
                                   
                                }
                                else if('7'==number.charAt(position))
                                { 
                                     System.out.print("Seventy");
                                            position=position+1;
                                            numlength=numlength-1;
                                   
                                }
                                else if('8'==number.charAt(position))
                                { 
                                     System.out.print("Eighty");
                                            position=position+1;
                                            numlength=numlength-1;
                                   
                                }
                                else if('9'==number.charAt(position))
                                { 
                                     System.out.print("Ninty");
                                            position=position+1;
                                            numlength=numlength-1;
                                   
                                }
                               
                                }
               
                                }if(numlength==1){
                                 if('0'==number.charAt(position))
                        {
                            position=position+1;
                            numlength=numlength-1;
                            i++;
                        }
                        else if('1'==number.charAt(position))
                        {   System.out.print("one");
                            System.out.print("  "+arr[i]);
                            i++;
                            position=position+1;
                            numlength=numlength-1;
                        }
                        else if('2'==number.charAt(position))
                        {   System.out.print("Two");
                            System.out.print("  "+arr[i]);
                            i++;
                            position=position+1;
                            numlength=numlength-1;
                        }
                        else if('3'==number.charAt(position))
                        {   System.out.print("Three");
                            System.out.print("  "+arr[i]);
                            i++;
                            position=position+1;
                            numlength=numlength-1;
                        }
                        else if('4'==number.charAt(position))
                        {   System.out.print("Four");
                            System.out.print("  "+arr[i]);
                            i++;
                            position=position+1;
                            numlength=numlength-1;
                        }
                        else if('5'==number.charAt(position))
                        {   System.out.print("Five");
                            System.out.print("  "+arr[i]);
                            i++;
                            position=position+1;
                            numlength=numlength-1;
                        }
                        else if('6'==number.charAt(position))
                        {   System.out.print("Six");
                            System.out.print("  "+arr[i]);
                            i++;
                            position=position+1;
                            numlength=numlength-1;
                        }
                        else if('7'==number.charAt(position))
                        {   System.out.print("Seven");
                            System.out.print("  "+arr[i]);
                            i++;
                            position=position+1;
                            numlength=numlength-1;
                        }
                        else if('8'==number.charAt(position))
                        {   System.out.print("Eight");
                            System.out.print("  "+arr[i]);
                            i++;
                            position=position+1;
                            numlength=numlength-1;
                        }
                        else if('9'==number.charAt(position))
                        {   System.out.print("Nine");
                            System.out.print("  "+arr[i]);
                            i++;
                            position=position+1;
                            numlength=numlength-1;
                        }}
   
   
    }
   
   
 
               
            } }}
   

Comments

Popular posts from this blog

What is Divide And Conquer Technique

Divide and Conquer is a programming technique which makes the program more efficient to write. And this technique work on the concept of recursion to solve a problem step by step. Generally this technique work in three parts:- Divide:-  Divide the problem into some subproblem. Conquer:-  Conquer the subproblem by calling recursively until subproblem solved. Combine:-  (Optional Step) Combine the subproblem solution. So, that we will get the final problem Solution.  When the subproblems are large enough to solve recursively, we call the recursive case. Once the subproblem becomes small enough that we no longer recursive, we say that the recursion "bottom out" and that we have gotten down to the base case.                          Application of Divide and Conquer Quick Sort Strassen's algorithm for matrix multiplication Merge Sort Counting inversions Binary Search Finding Min and max Divide and Conquer Abstract Algorithm DAC(a,i,j) {   

Bresenham circle drawing program in C

  /* This is the program for bresenham circle drawing  in C */ #include<stdio.h> #include<conio.h> #include<graphics.h> #include<dos.h> void main() { int gdriver = DETECT,gmode,x,y,r,x1,y1; float e; initgraph(&gdriver,&gmode,"C:\\tc\\BGI"); printf("center co-ordinate of x and y position of circle:\n"); scanf("%d %d",&x,&y); printf("input the radius of circle:\n"); scanf("%d",&r); x1=1; y1=r; putpixel(x+0,y+r,RED); putpixel(x+r,y+0,RED); putpixel(x-0,y-r,RED); putpixel(x-r,y-0,RED); e=3-2*r;   while(x1<y1) { if(e<0) { x1=x1+1; e=e+4*x1+6; } else { x1=x1+1; y1=y1-1; e=e+4*(x1-y1)+10; }           putpixel(x+x1,y+y1,1); putpixel(x+y1,y+x1,2); putpixel(x-x1,y+y1,3); putpixel(x-y1,y+x1,4); putpixel(x-x1,y-y1,5); putpixel(x-y1,y-x1,6); putpixel(x+x1,y-y1,7); putpixel(x+y1,y-x1,8); delay(100); } getch(); } /*NOTE:-You have to change the graphics

Extracting all states data like population,economy from a wikipedia link of India using Java Language

package urldatafetch; /**  *  * @author Rishabh  */ import java.io.IOException; import java.io.PrintWriter; import java.util.ArrayList; import java.util.Iterator; import org.jsoup.Jsoup; import org.jsoup.nodes.Document; import org.jsoup.nodes.Element; import org.jsoup.select.Elements; public class UrlDataFetch { public static void main(String[] args) throws IOException {       org.jsoup.nodes.Document doc = Jsoup.connect("https://en.wikipedia.org/wiki/India").get();       org.jsoup.select.Elements tables =doc.select("table");                    int i=1;                    String state[]=new String[29];        ArrayList<String> ar=new ArrayList<>();      for(org.jsoup.nodes.Element tableo :tables)      {  if(i==1 ||i==2 ||i==4){i++;}      else if(i==3)      {  org.jsoup.select.Elements rows=tableo.select("tr");         for(org.jsoup.nodes.Element row :rows)         {             org.jsoup.select.Elements co