Showing posts with label Zoho. Show all posts
Showing posts with label Zoho. Show all posts

Tuesday, 5 September 2017

Print Given Money or Value in Words

PROGRAM : 


#include<stdio.h>

int main()
{
         int i,j,k=0,value,count,a[10],dummy;
         char ones[10][6]={"zero","one","two","three","four","five","six","seven","eight","nine"};
         char temp[10][10]={"ten","eleven","twelve","thirteen","fourteen","fifteen","sixteen","seventeen","eighteen","nineteen"};
         char tens[11][8]={"-","-","twenty","thirty","forty","fifty","sixty","seventy","eighty","ninety"};

 //hundreds are same as ones
         printf("Enter Value upto 6 digits to get in Words\n");
         dummy=value;
         scanf("%d",&value);
           while(value>0)
           {
               i=value%10;
               a[k]=i;
               k++;
               value/=10;
            }
 //Here if 12345 is given then a[0]=5,a[1]=4 and soon then last but one digit will be at a[1]
           int totalDigits=k-1;
           if(totalDigits<6)
           {
                    for(i=totalDigits;i>=0;i--)
                    {
                          if(i==5 && a[i]!=0)
                               printf("%s lakh ",ones[a[i]]);
                           if(i==4 || i==3)
                          {
       //Start
                               if(i==4 && a[i]==0)
                             {
                                 printf("%s thousand ",ones[a[i-1]]);
                                   i--;
                             }
                              else if(i==4 && a[i]==1)
                               {
                                 printf("%s thousand ",temp[a[i-1]]);
                                 i--;
                             }
                               else if(i==4)
                                {
                                 printf("%s ",tens[a[i]]);
                             }
                             else
                             {
                                 printf("%s thousand ",ones[a[i]]);
                             }
          //end
                          }
                        
                          if(i==2 && a[i]!=0)
                          {
                                  printf("%s hundred and ",ones[a[i]]);
                          }
                           if(i==2 && a[i]==0)
                           {
                                   printf("and ");
                          }
                            if(i==1 || i==0)
                          {
                               if(i==1 && a[i]==0)
                                 {
                                      printf("%s only\n",ones[a[i-1]]);
                                   i--;
                                  break;
                                  }
                               if(i==1 && a[i]==1)
                                 {
                                   printf("%s only\n",temp[a[i-1]]);
                                    i--;
                                 break;
                                }
                               if(i==1 && a[i]!=1 && a[i]!=0)
                                 {
                                    printf("%s ",tens[a[i]]);
                                 }
                                 if(i==0 && a[i]!=0)
                                 {
                                printf("%s only\n",ones[a[i]]);
                                  }
                             }
                    }
         }
        else
        {
             printf("Enter only upto 6 digits\n");
         }
return 0;
}

OUTPUT :



Number of Characters need to make a String Palindrome


#include<stdio.h>
#include<string.h>

int main(){
   
    int i=0,j,totChar=0,flag=0,c;
    char str[100];
    printf("Enter a String\n");
    scanf("%s",str);
    int len=strlen(str);
    j=len-1;
   
    while(i<j)
    {
           if(str[i]==str[j])
           {
               i++;
               j--;
              flag=1;
           }
        else
        {
            totChar++;
            i++;
            flag=0;
        }
    }
    if(flag!=1)
    totChar=len-1;
    c=totChar-1;
    printf("Total Characters required=%d\n",totChar);
   
    for(i=0;i<totChar;i++)
        str[len+i]=str[c--];
    str[len+i]='\0';
    printf("Final String of Palindrome=%s\n",str);
    return 0;
}

Input    : mada
Output : Total Characters required =1
               Final String of Palindrome = madam

Find Subsets of Given String


Method I:
       Can find substring for any length of string because we are taking binary number as string while Conversion itself

#include<stdio.h>
#include<string.h>
#include<math.h>

char *GetBinary(int dec);

int main()
{
 int i,j,len,count=0,no_of_zeros;
 char str[20],temp[20],binary[20];
 printf("Enter a string\n");
 gets(str);
 len=strlen(str);
 printf("Subsets of Given String are");
 for(i=0;i<(pow(2,len));i++)//2^len is the number of subsets for given length.
 {
  strcpy(binary,GetBinary(i));
  no_of_zeros=len-strlen(binary);//Since zeros will not be there before single digits and double etc depending on length of binary number.

 //To compensate them we used here
  for(j=0;j<len;j++)
  {
         if(j>=noofzeros)
        {
          if(binary[j-no_of_zeros]!='0')
           printf("%c",str[j]);
        }
  }
        printf("\n");
 }
 return(0);
}

char *GetBinary(int dec)
{
 int temp,i,j=1,k=0;
 char binary[20];
 temp=dec;
 while(temp!=0)
 {
   i=temp%2;
      binary[k]=i+'0';
   temp=temp/2;
      j=j*10;
      k++;
 }
 binary[k]='\0';
 return(strrev(binary));
}

Method II:
       Can find subsets for smaller strings as we are taking binary number as integer
 
#include<stdio.h>
#include<string.h>
#include<math.h>

int GetBinary(int dec);

int main()
{
 int i,j,binary,len,count=0,tempLen;
 char str[20],temp[20];
 printf("Enter a string\n");
 gets(str);
 len=strlen(str);
 for(i=1;i<(pow(2,len));i++)//2^len is the number of subsets for given length
 {
  binary=GetBinary(i);
  sprintf(temp, "%d", binary);//To convert integer to string
  tempLen=len-strlen(temp);
  for(j=0;j<len;j++)
  {
   if(j>=tempLen)
   {
    if(temp[j-tempLen]!='0')
       printf("%c",str[j]);
   }
  }
  printf("\n");
 }
return 0;
}

int GetBinary(int dec)
{
 int temp,i,j=1,binary=0;
 temp=dec;
 while(temp!=0)
 {
   i=temp%2;
      binary=binary+(i*j);
   temp=temp/2;
      j=j*10;
 }
 return(binary);
}

Input :
Tea
Output:
T

a

Te
Ta

ea
Tea


Input :
Help
Output :
H
e
l
p
He
Hl
Hp
el
ep
lp
Hel
Hep
Hlp
elp
Help