[Solved] Candy Quiz with Java, C++

Candy Quiz: Anto and Jim were best friends. They went to a nearby Super Market to get some candies, where they saw an on-the-spot event organized for the kids. The event is a one minute quiz game where two kids can participate and the winners were announced a basket full of candies.

Anto and Jim eagerly participated in the game and managed to answer most of the questions right. The final question came to them from the host looked a bit tricky to solve. They wanted your help to answer it and take home the candies. The question posted to them was to find the last non-zero digit of n! where the event host gave them the value of ‘n’.

Given ‘n’, write a recursive method to find the last non-zero digit of n!.

Hence create a class named Factorial with the following method.

Method NameDescription
int lastNonZeroDigit(int)                                          This recursive method should return the last non-zero digit of n!.

Create a driver class called Main. In the Main method, obtain input from the user in the console and display the last non-zero digit of n! by calling the lastNonZeroDigit method present in the Factorial class.

[Note: Strictly adhere to the Object Oriented Specifications given in the problem statement.

All class names, attribute names and method names should be the same as specified in the problem statement.]
Input Format:
First and only line of the input is an integer ‘n’.

Output Format:
Output the last non-zero digit of n! in a single line.
Refer sample input and output for formatting specifications.

Sample Input 1:
5

Sample Output 1:
2

Sample Input 2:
33

Sample Output 2:
8

Solution

import java.util.*;
public class Main{

    static int dic[] = {1, 1, 2, 6, 4, 2, 2, 4, 2, 8};
    public static void main(String[] args){
         //sauravhathi
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        System.out.println(lastNonZeroDigit(n));
    }

    static int lastNonZeroDigit(int n){
        if(n < 10){
            return dic[n];
            //sauravhathi
        }
        else{
            if(((n / 10) % 10) % 2 == 0){
                return (6 * lastNonZeroDigit(n / 5) * dic[n % 10]) % 10;
            }
            else{
                 //sauravhathi
                return (4 * lastNonZeroDigit(n / 5) * dic[n % 10]) % 10;
            }
        }
    }

}
#include <iostream>
using namespace std;

int dig[] = {1, 1, 2, 6, 4, 2, 2, 4, 2, 8};

int lastNonZeroDigit(int n)
{
    if (n < 10)
        return dig[n];

    if (((n / 10) % 10) % 2 == 0)
        return (6 * lastNonZeroDigit(n / 5) * dig[n % 10]) % 10;
    else
        return (4 * lastNonZeroDigit(n / 5) * dig[n % 10]) % 10;
}
//sauravhathi

int main()
{
    int n;
    cin >> n;
    cout << lastNonZeroDigit(n);
    return 0;
}

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 *