Count number of 2’s in a given range (0 to n)? (ex: range between 0-20, Ans: 3 (i.e [2], 1[2], [2]0))

Google Search: https://www.google.com/search?q=Counting+2s&ie=utf-8&oe=utf-8&client=firefox-b#q=Counting+2s+in+c

SOlution: https://www.careercup.com/question?id=56794

//https://www.careercup.com/question?id=56794
#include<stdio.h>

int main()
{
int n,i,j;
int ctr=0;
scanf("%d",&n);
for(i=0;i<=n;i++)
{
for(j=i;j>0;j=j/10)
{
if((j%10)==2)
ctr++;
}
}
printf("The number of 2's is >> %d ",ctr);
return 0;
}

 

It would be a great help, if you support by sharing :)
Author: zakilive

Leave a Reply

Your email address will not be published. Required fields are marked *