10 June 2018

print numbers consisting 3 & 4

i/p 10
o/p 3 4 33 34 43 44 333 334 343 344

complexity- O(n^2)


void a()
    {
        int i=1,ct=0;
        while(i>0 && ct<10)
        { int a=i,c=0;
            while(a>0)
            {
                if(a%10==3 || a%10==4)
                {
                    a=a/10;
                }else
                {
                    c=1;
                    break;
                }
            }if(c==0){
            System.out.print(i+" "); ct++;}
           i++;
        }
    }

complexity-0(1)



import java.io.*;

class GFG {
    int ct=0;
    void pat1(int a,int c,int dup)
    {     
              if(a%10!=3 && a%10!=4)
                {
                    c=1;
                }
                a=a/10;

                if(a>0)
                       {
                pat1(a,c,dup);
                       } else
                              {
                               if(c==0) {
                       System.out.print(dup+" ");
                       ct++;
                         }
                   }
    }
    void pat(int i,int c)
    {
        if(i>0 && ct<c)
        {
          pat1(i,0,i);
          pat(++i,c);
        }
    }
    public static void main (String[] args) {
       GFG ob=new GFG();
       ob.pat(1,10);
    }
}

No comments:

Post a Comment