#include <stdio.h>
#include <string.h>
#define NUMBER 1
main ()
{
/*This program calculates the number of 2's present in the number x*/
int arr[]={221};
int i=0,count=0;
for(i=0;i<NUMBER;i++)
{
while(arr[i]>0)
{
int rem,quo;
rem=arr[i]%10;
quo=arr[i]/10;
printf("rem= %d and quo=%d \n ", rem,quo);
if(rem==2&&quo==2)
{
count=count+2;
printf("Number is : %d\n",arr[i]);
arr[i]=arr[i]/10;
}
else if(rem==2)
{
count++;
printf("Number is : %d\n",arr[i]);
}
else if(quo==2)
{
count++;
printf("Number is : %d\n",arr[i]);
arr[i]=arr[i]/10;
}
arr[i]=arr[i]/10;
}
}
/* complexity of this program : n* size of the number ( digits present in the number*/
/* Worst case O(n^2)*/
printf("\n\n\n");
printf("THe number of 2's are : %d\n",count);
printf("\n\n\n");
}
I2luY2x1ZGUgPHN0ZGlvLmg+IAojaW5jbHVkZSA8c3RyaW5nLmg+IAojZGVmaW5lIE5VTUJFUiAxIAptYWluICgpIAp7IAovKlRoaXMgcHJvZ3JhbSBjYWxjdWxhdGVzIHRoZSBudW1iZXIgb2YgMidzIHByZXNlbnQgaW4gdGhlIG51bWJlciB4Ki8gCgppbnQgYXJyW109ezIyMX07IAppbnQgaT0wLGNvdW50PTA7IAoKZm9yKGk9MDtpPE5VTUJFUjtpKyspIAp7IAp3aGlsZShhcnJbaV0+MCkgCnsgCmludCByZW0scXVvOwkKcmVtPWFycltpXSUxMDsgCnF1bz1hcnJbaV0vMTA7IApwcmludGYoInJlbT0gJWQgYW5kIHF1bz0lZCBcbiAiLCByZW0scXVvKTsKaWYocmVtPT0yJiZxdW89PTIpIAp7IApjb3VudD1jb3VudCsyOyAKCnByaW50ZigiTnVtYmVyIGlzIDogJWRcbiIsYXJyW2ldKTsgCmFycltpXT1hcnJbaV0vMTA7IAoKfSAKZWxzZSBpZihyZW09PTIpIAp7IApjb3VudCsrOyAKcHJpbnRmKCJOdW1iZXIgaXMgOiAlZFxuIixhcnJbaV0pOyAKfQkKZWxzZSBpZihxdW89PTIpIAp7IApjb3VudCsrOyAKcHJpbnRmKCJOdW1iZXIgaXMgOiAlZFxuIixhcnJbaV0pOyAKYXJyW2ldPWFycltpXS8xMDsgCn0gCmFycltpXT1hcnJbaV0vMTA7IAp9IAp9IAovKiBjb21wbGV4aXR5IG9mIHRoaXMgcHJvZ3JhbSA6IG4qIHNpemUgb2YgdGhlIG51bWJlciAoIGRpZ2l0cyBwcmVzZW50IGluIHRoZSBudW1iZXIqLyAKLyogV29yc3QgY2FzZSBPKG5eMikqLyAKCnByaW50ZigiXG5cblxuIik7IApwcmludGYoIlRIZSBudW1iZXIgb2YgMidzIGFyZSA6ICVkXG4iLGNvdW50KTsgCnByaW50ZigiXG5cblxuIik7IAp9