Menu
Topics Index
...
`

Find Number Of 2s present between 0 and given number


If you need explanation Read this topic

If you need Answer Take test on this topic

Write a program to get number of 2's present between 0 and given number N including N.

Input (int) Output (int)
12 [2], 1[2] = 2
112 [2], 1[2], [2]0, [2]1, [2][2], [2]3, [2]4, [2]5, [2]6, [2]7, [2]8, [2]9, 3[2], 4[2], 5[2], 6[2], 7[2], 8[2], 9[2], 10[2], 11[2] = 22
555 216
765 257
1079 318



class CountNumberOfTwos
{
    public static void main(String s[])
    {
        int number = 222;
        System.out.println("Number of 2's present between zero and " + number + " are " + getTwosCount(number));

    }


    public static int getTwosCount(int number) {
    }

}
Topic: Java for loops vs Java while loops vs Java do while loops

If you need explanation Read this topic

If you need Answer Take test on this topic

User comments below. All of them might not be correct.

As per the question we need to count the numbers which contain 2 in it from 0 to input number... For that we need to take a count variable for counting the number which contain 2... Take a loop that iterates until 0 to input... Inside the loop take another loop which will get the remainder of number until number becomes 0.. In inner loop if we find 2 in any position then increment the count variable...

Posted by Gopinath Manchikanti    2015-02-16 08:47:50


As per the question we need to count the numbers which contain 2 in it from 0 to input number... For that we need to take a count variable for counting the number which contain 2... Take a loop that iterates until 0 to input... Inside the loop take another loop which will get the remainder of number until number becomes 0.. In inner loop if we find 2 in any position then increment the count variable...

Posted by Gopinath Manchikanti    2015-02-16 08:49:42


here we have two count twos in a given range... so frstly initialize count =0 .take a=2 to input... now inside this loop s=a, take loop while(s>0) {  b=s%10 s=s/10 .....if b==2 then. increment count by one..}} exit both loop... primt count.

Posted by Asma Mujtaba Khan    2015-02-16 10:28:27


This dose is now closed and the winners are Gopinath Manchikanti,  for 'First Correct Comment', Asma Mujtaba Khan,  for 'Second Correct Comment'. The 'lucky liker' is Bolo Raven. Please login into Merit Campus using facebook, to claim your recharge. Go to http://java.meritcampus.com/earnings to raise the recharge.

Posted by Merit Campus    2015-02-17 05:59:18


© meritcampus 2019

All Rights Reserved.

Open In App