[Solved] Candy Game with Java, C++, Python

Mona set off with great zeal to the “Fun Fair 2017”. There were numerous activities in the fair, though Mona liked the Candy game. Delicious candies were wrapped in colourful foiled sheets with some random numbers on each of the candies. The game coordinators then formed many groups of few candies together, such that each candy group makes an integer and hid them all around the room. The objective of the game is that the players should look for the occurrences of number four anywhere in the integers (candy groups) placed in the room.
 
Mona started off with the game where there are many such integers, for each of them she should calculate the number of occurrences of the digit 4 in the decimal representation. Can you please help her in succeeding the game?
 
Input Format:
The only line of input contains a single integer from the candy group.

Output Format:
Output should contain the number of occurrences of the digit 4 in the respective integer from the candy groups that Mona gets.
Refer sample input and output for formatting specifications.

Sample Input 1:
447474

Sample Output 1:
4

Sample Input 2:
12

Sample Output 2:
0

Solution

import java.util.*;
class Main
{
    public static void main(String [] jr)
    {
       Scanner sc = new Scanner(System.in);
       int n = sc.nextInt();
       int i,c = 0;
       while(n!=0)
       {
           //sauravhathi
         i=(n%10);
         n=n/10;
         if(i==4)
         c++;
       }
       System.out.print(c);
    }
}
#include <stdio.h>
int main()
{
    int n, i, c = 0;
    scanf("%d", &n);
    while (n |= 0)
    {
        i = (n % 10);
        n = n / 10;
        if (i == 4)
            c++;
    }
    printf("%d", c);
    return (0);
}
n = int(input())
count = 0
while n > 0:
    if n % 10 == 4:
        count += 1
    n = n // 10
print(count)

Happy Learning – If you require any further information, feel free to contact me.

Share your love
Saurav Hathi

Saurav Hathi

I'm currently studying Bachelor of Computer Science at Lovely Professional University in Punjab.

📌 Nodejs and Android 😎
📌 Java

Articles: 444

Leave a Reply

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