Question

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

  1. 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 last digit of the number (the ones digit) has a weight of 1, so should be added to the sum "as is". The second from last digit (the tens digit) has a weight of 2, and so should be multiplied by 2 then added to the sum. The third from last digit should be multiplied by 1 and added to the sum, and so on, with the weights alternating between 1 and 2 for each digit in turn. The final sum of all weighted digits should be returned from the function. For example, if the number 2561 was input to the function, the sum would be 1x1 + 2x6 + 1x5 + 2x2 = 22. HINT: Start from the right-most digit, and use simple C operators to find both the digit and the remaining number once that digit is removed.
  2. Write another C function called reduced_sum which repeatedly calls the weighted_sum function developed in part (a) until the result is a single digit (less than 10). This value is then returned from the function. For example, if the same number 2561 was input to the function, it would return 4, since 2561 -> 22 -> 6 (1x2 + 2x2). Therefore 6 will be returned.
  3. The only possible results of the reduced_sum function are 0 to 9. Write a main() program which uses the functions developed in parts (a) and (b) to find how many times each of these possible results occurs for the numbers from 0 to 10000. You should not print out the result of each test, just the total sum for each of the final results 0 through 9, so 10 total lines of output.

Homework Answers

Answer #1

* C Program to Find Sum of First and Last Digit Of a Number */

#include <stdio.h>
#include <math.h>

int main()
{
   int Number, FirstDigit, Count, LastDigit, Sum = 0;

   printf("\n Please Enter any Number that you wish : ");
   scanf("%d", & Number);
  
   Count = log10(Number);   
   FirstDigit = Number / pow(10, Count);
  
   LastDigit = Number % 10;
  
   Sum = FirstDigit + LastDigit;
  
   printf(" \n The Sum of First Digit (%d) and Last Digit (%d) of %d = %d", FirstDigit, LastDigit, Number, Sum);

   return 0;
}


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
In this example you are allowed to use from the C standard library only functions for...
In this example you are allowed to use from the C standard library only functions for input and output (e.g. printf(), scanf()) For this exercise you should be able to write a logical expression (i.e., with logical operators) which checks if some integer x consists of exactly 5 digits. Ex: 30498 and -14004 are 5-digit numbers, while 1018, -2 and 46 are not. Complete the intQ2(intQ2_input) function that takes an input integer parameter and returns 1 if the number is...
In this example you are allowed to use from the C standard library only functions for...
In this example you are allowed to use from the C standard library only functions for input and output (e.g. printf(), scanf()) Complete the following functions using C programming language: a) Complete the int Q7a(intQ7_input) function takes a seven-digit positive integer as input and returns it reversed. For example, if the integer is 9806593, the program should print 3956089. You are not permitted to use any function of C standard library other than scanf()and printf().You are not permitted to use...
Create a function called, convert. This function receives a string parameter called word which only contains...
Create a function called, convert. This function receives a string parameter called word which only contains digits (the string represents a positive number) and returns a list of numbers. This is how the function works: - This function calculates the number of times each digit has repeated in the input string and then generates a number based on that using the following formula and adds it to a list. For instance, if the digit x has been repeated n times,...
One way to represent a very large integer (one that won't fit into a variable of...
One way to represent a very large integer (one that won't fit into a variable of type short, int, or even long) is to use an array. The array is of type int, so each element in the array can hold an integer -- we will store just one digit of our number per array element. So if a user entered 2375, it might be stored as -------------------------- | 2 | 3 | 7 | 5 | ... | --------------------------...
One way in which computers can compute the values of functions like the sine, cosine, logarithm...
One way in which computers can compute the values of functions like the sine, cosine, logarithm and so on is to use a power series expansion. This is an infinite series from which the first few terms are calculated and used to find an approximate value for the function. The series expansion for the exponential function is : ex = exp(x) = 1 + x + x2 /2! + x3/3! + x4 /4/! + … + xn /n! + …...
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...
How to write a C++ program. Additive persistence is a property of the sum of the...
How to write a C++ program. Additive persistence is a property of the sum of the digits of an integer. The sum of the digits is found, and then the summation of digits is performed creating a new sum. This process repeats until a single integer digit is reached. Consider the following example: 1. The beginning integer is 1234 2. Sum its digits is 1+2+3+4 = 10 3. The integer is now 10 4. The sum of its digits is...
c++ 19.36 LAB: Output values below an amount - functions Write a program that first gets...
c++ 19.36 LAB: Output values below an amount - functions Write a program that first gets a list of integers from input. The input begins with an integer indicating the number of integers that follow. Then, get the last value from the input, and output all integers less than or equal to that value. Ex: If the input is: 5 50 60 140 200 75 100 the output is: 50 60 75 The 5 indicates that there are five integers...
WRITE C++ PROGRAM FOR 1,2,3,4 PARTS of question, DO ADD COOMENTS AND DISPLAY THE OUTPUT OF...
WRITE C++ PROGRAM FOR 1,2,3,4 PARTS of question, DO ADD COOMENTS AND DISPLAY THE OUTPUT OF A RUNNING COMPILER QUESTION: 1) Fibonacci sequence is a sequence in which every number after the first two is the sum of the two preceding ones. Write a C++ program that takes a number n from user and populate an array with first n Fibonacci numbers. For example: For n=10 Fibonacci Numbers: 1, 1, 2, 3, 5, 8, 13, 21, 34, 55 2): Write...
Java Please [(1)] A palindrome is a string that reads the same forwards as backwards. Using...
Java Please [(1)] A palindrome is a string that reads the same forwards as backwards. Using only a fixed number of stacks and queues, the stack and queue ADT functions, and a fixed number of int and char variables, write an algorithm to determine if a string is a palindrome. Assume that the string is read from standard input one character at a time. The algorithm should output true or false as appropriate [(2)] Let Q be a non-empty queue,...