fork(4) download
  1. #include <stdio.h>
  2. #include <string.h>
  3. #define NUMBER 1
  4. main ()
  5. {
  6. /*This program calculates the number of 2's present in the number x*/
  7.  
  8. int arr[]={221};
  9. int i=0,count=0;
  10.  
  11. for(i=0;i<NUMBER;i++)
  12. {
  13. while(arr[i]>0)
  14. {
  15. int rem,quo;
  16. rem=arr[i]%10;
  17. quo=arr[i]/10;
  18. printf("rem= %d and quo=%d \n ", rem,quo);
  19. if(rem==2&&quo==2)
  20. {
  21. count=count+2;
  22.  
  23. printf("Number is : %d\n",arr[i]);
  24. arr[i]=arr[i]/10;
  25.  
  26. }
  27. else if(rem==2)
  28. {
  29. count++;
  30. printf("Number is : %d\n",arr[i]);
  31. }
  32. else if(quo==2)
  33. {
  34. count++;
  35. printf("Number is : %d\n",arr[i]);
  36. arr[i]=arr[i]/10;
  37. }
  38. arr[i]=arr[i]/10;
  39. }
  40. }
  41. /* complexity of this program : n* size of the number ( digits present in the number*/
  42. /* Worst case O(n^2)*/
  43.  
  44. printf("\n\n\n");
  45. printf("THe number of 2's are : %d\n",count);
  46. printf("\n\n\n");
  47. }
Success #stdin #stdout 0s 3296KB
stdin
Standard input is empty
stdout
rem= 1 and quo=22 
 rem= 2 and quo=2 
 Number is : 22



THe number of 2's are : 2