Question

In: Computer Science

**Please write in Java, in a very simple/beginner coding style/language - thank you!** Directions: Given a...

**Please write in Java, in a very simple/beginner coding style/language - thank you!**
Directions
: Given a factorial n!. Find the sum of its digits, and the number of trailing zeros (ie: the number of zeros at the end of the factorial).
Input: integer nn, a non-negative integer where n≤20n≤20
Output: integer xyxy, the concatenation of x and y, where x is the sum of the digits in n! and y is the number
of the zeros in n!) Note, 0≤x,y0≤x,y.

Example: Consider the factorial: 5! = 5x4x3x2x1 = 120. The sum of its digits is 1+2+0 = 3, and the number of zeros at the end of 5! = 120 is 1. Then here, x = 3 and y = 1, so the answer/output for 5! is 31.

Extra Credit: allow the user to choose any integer n≥1

Solutions

Expert Solution

import java.util.Scanner;

class Factorial {

    // function to find total zeros

    static int find_zeros(long factorial) {

        int zeros = 0;

        // till factorial is divisible by 10

        while (factorial % 10 == 0) {

            zeros += 1;

            factorial /= 10;

        }

        return zeros;

    }

    // function to find sum of digits

    static int find_sum(long factorial) {

        int sum = 0;

        while (factorial > 0) {

            sum += factorial % 10;

            factorial /= 10;

        }

        return sum;

    }

    public static void main(String[] args) {

        // read n

        Scanner sc = new Scanner(System.in);

        System.out.print("Enter n: ");

        int n = sc.nextInt();

        // compute factorial

        long factorial = 1;

        for (int i = 1; i <= n; i++)

            factorial *= i;

        int sum = find_sum(factorial);

        int zeros = find_zeros(factorial);

        // print

        System.out.printf("%d%d", sum, zeros);

    }

}

.

Output:

.


Related Solutions

In java beginner coding language ,Write a class called Sphere that contains instance data that represents...
In java beginner coding language ,Write a class called Sphere that contains instance data that represents the sphere’s diameter. Define the Sphere constructor to accept and initialize the diameter, and include getter and setter methods for the diameter. Include methods that calculate and return the volume and surface area of the sphere. Include a toString method that returns a one-line description of the sphere. Create a driver class called MultiSphere, whose main method instantiates and updates several Sphere objects.
In this question, you are asked to write a simple java program to understand natural language....
In this question, you are asked to write a simple java program to understand natural language. The user will enter the input following the format: Name came to City, Country in Year. For example: Robin came to Montreal, Canada in 2009. Assume a perfect user will follow the exactly above formats for the inputs. Your program should be able to analyze the key words (Name, City, Country and Year) from the inputs and reorganize the outputs following format: Name stay...
Using python as the coding language please write the code for the following problem. Write a...
Using python as the coding language please write the code for the following problem. Write a function called provenance that takes two string arguments and returns another string depending on the values of the arguments according to the table below. This function is based on the geologic practice of determining the distance of a sedimentary rock from the source of its component grains by grain size and smoothness. First Argument Value Second Argument Value Return Value "coarse" "rounded" "intermediate" "coarse"...
What is forward contract? (please write everything about forward contract in very simple language that anybody...
What is forward contract? (please write everything about forward contract in very simple language that anybody can understand it)                200 words please
In Java please. Thank you! Recursion For this assignment you are going to write six different...
In Java please. Thank you! Recursion For this assignment you are going to write six different methods. Each method is to be written recursively. Any method that is written iteratively will not receive any credit, even if it is correct and produces the same results or output. You will be given a starter file. You are not allowed to change the signatures of any of the given methods. You are not allowed to add any methods to your solutions. Write...
Language for this question is Java write the code for the given assignment Given an n...
Language for this question is Java write the code for the given assignment Given an n x n matrix, where every row and column is sorted in non-decreasing order. Print all elements of matrix in sorted order.Input: The first line of input contains an integer T denoting the number of test cases. Then T test cases follow. Each test case contains an integer n denoting the size of the matrix. Then the next line contains the n x n elements...
C++ program, I'm a beginner so please make sure keep it simple Write a program to...
C++ program, I'm a beginner so please make sure keep it simple Write a program to read the input file, shown below and write out the output file shown below. Use only string objects and string functions to process the data. Do not use c-string functions or stringstream (or istringstream or ostringstream) class objects for your solution. Input File.txt: Cincinnati 27, Buffalo 24 Detroit 31, Cleveland 17 Kansas City 24, Oakland 7 Carolina 35, Minnesota 10 Pittsburgh 19, NY Jets...
This program is to be written in Java Language. Thank you A College has conducted a...
This program is to be written in Java Language. Thank you A College has conducted a student survey. Students were asked to rate their satisfaction with remote learning courses. Students rated their satisfaction on a scale of 1 to 5 (1 = "I hate it", 5 = "I love it"). The student responses have been recorded in a file called "StudentResponses.txt". Each line of the file contains one student response. Program 1 You are to write a program that reads...
simple Java project// please explain every statement with reasoning. Thank you Read from a file that...
simple Java project// please explain every statement with reasoning. Thank you Read from a file that contains a paragraph of words. Put all the words in an array, put the valid words (words that have only letters) in a second array, and put the invalid words in a third array. Sort the array of valid words using Selection Sort. Create a GUI to display the arrays using a GridLayout with one row and three columns. The input file Each line...
Please answer the following by coding in R with comments ! Thank you!!! Evaluation of a...
Please answer the following by coding in R with comments ! Thank you!!! Evaluation of a square root is achieved using the sqrt() function, but a warning will be issued when the argument is negative. Consider the following code which is designed to test whether a given value is positive before checking whether the square root of the value is less than 5. testValue <-7 (testValue > 0) & (sqrt(testValue) < 5) ## [1] TRUE testValue <--7 (testValue > 0)...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT