Question

Write the following functions and provide a program to test them (main and all functions in...

  1. Write the following functions and provide a program to test them (main and all functions in one .py). 5 pts
    1. def firstDigit(n) (returning the first digit of the argument)
    2. def lastDigit(n) (returning the last digit of the argument)
    3. def digits(n) (returning the number of digits of the argument)

For example, firstDigit(1729) is 1, lastDigit(1729) is 9, and digits(1729) is 4.

keep it simple python

Homework Answers

Answer #1
def firstDigit(n):
    result = 0
    while n != 0:
        result = n % 10
        n = n // 10
    return result

def lastDigit(n):
    return n%10

def digits(n):
    count = 0
    while n!=0:
        n = n//10
        count += 1
    return count

def main():
    print(firstDigit(1729))
    print(lastDigit(1729))
    print(digits(1729))

main()
    

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Write the following functions and provide a program to test them (main and all functions in...
Write the following functions and provide a program to test them (main and all functions in one .py). 6 pts def allTheSame(x, y, z) (returning true if the arguments are all the same) def allDifferent(x, y, z) (returning true if the arguments are all different) def sorted(x, y, z) (returning true if the arguments are sorted, smallest one first keep it simple, python
Write the following function and provide a program to test it (main and function in one...
Write the following function and provide a program to test it (main and function in one .py) 5 pts def readFloat(prompt) that displays the prompt string, followed by a space, reads a floating-point number in, and returns it. Below is how you’re going to call the function: salary = readFloat(“Please enter your salary:”) percentageRaise = readFloat(“What percentage raise would you like?”) print("The salary is", salary) print("The raise percentage is", percentageRaise) keep it simple, python
write the following methods and provide a program to test them. double average(double x, double y,...
write the following methods and provide a program to test them. double average(double x, double y, double z), returning the average of the arguments. in java 1.7
given an integer, write a program that displays the number as follows first line         all digits...
given an integer, write a program that displays the number as follows first line         all digits second line   all except first digit third line        all except first two digits ..... last line          the last digit ex) 5 6 7 8 6 7 8 7 8 8
IN PYTHON : Write a program that asks the user for a number. Write the number...
IN PYTHON : Write a program that asks the user for a number. Write the number to a file called total.txt. Then ask the user for a second number. Write the new number on line 2 with the total for both numbers next to it separated by a comma. Then ask the user for a third number and do the same. Keep asking for numbers until the person puts in a zero to terminate the program. Close the file. Be...
write a code in python Write the following functions below based on their comments. Note Pass...
write a code in python Write the following functions below based on their comments. Note Pass is a key word you can use to have a function the does not do anything. You are only allowed to use what was discussed in the lectures, labs and assignments, and there is no need to import any libraries. #!/usr/bin/python3 #(1 Mark) This function will take in a string of digits and check to see if all the digits in the string are...
Function Example: Write a Python function that receives two integer arguments and writes out their sum...
Function Example: Write a Python function that receives two integer arguments and writes out their sum and their product. Assume no global variables. def writer(n1, n2): sum = n1 + n2 product = n1*n2 print("For the numbers", n1, "and", n2) print("the sum is", sum) print("and the product is", product) ... 1) Create a PYHW2 document that will contain your algorithms in flowchart and pseudocode form along with your screen shots of the running program. 2) Create the algorithm in both...
How to code this in python Write a program that computes and prints the first 1000...
How to code this in python Write a program that computes and prints the first 1000 prime numbers - simply write out each prime number on a new line. In implementing this solution I want you to define 2 functions: is_prime which can be used to test whether a number is a prime (e.g. is_prime(17) returns True but is_prime(9) returns False) and compute_primes which will return an array (or Python list) of the first n primes where n is passed...
given an integer, write a program in C++ that displays the number as follows first line        ...
given an integer, write a program in C++ that displays the number as follows first line         all digits second line   all except first digit ... last line          last digit ex) 5 6 7 8 6 7 8 7 8 8
This question is broken into 3 parts, each of which builds on the functions developed in...
This question is broken into 3 parts, each of which builds on the functions developed in the previous part. Note that you can (and should) still answer parts (b) and (c) even if you cannot answer the preceding parts - just assume that the functions work as they should, and continue. Please explain the code as well Write a C function called weighted_digit_sum that takes a single integer as input, and returns a weighted sum of that numbers digits. The...