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
Problem: Our Armstrong number Please write code for C language So far we have worked on...
Problem: Our Armstrong number Please write code for C language So far we have worked on obtaining individual digits from 4 digits or 5 digit numbers. Then added them to find the sum of digits in various examples and assignments. However, the process of extracting individual digits is actually can be solved using a loop as you were doing a repetitive task by using mod operation and division operation. Now, we know how loops work and we can remove the...
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...
Write a C++ program which consists of several functions besides the main() function. 1)   The main()...
Write a C++ program which consists of several functions besides the main() function. 1)   The main() function, which shall ask for input from the user (ProcessCommand() does this) to compute the following: SumProductDifference and Power. There should be a well designed user interface. 2)   A void function called SumProductDifference(int, int, int&, int&, int&), that computes the sum, product, and difference of it two input arguments, and passes the sum, product, and difference by-reference. 3)   A value-returning function called Power(int a,...
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,...
Create a Python main program which calls two functions enterNum and calcResult and accomplishes the following:...
Create a Python main program which calls two functions enterNum and calcResult and accomplishes the following: 1. The main program calls the function enterNum 3 times. The first time enterNum is called, the main program stores the returned output in the variable xx. The second time, the returned output is stored in the variable yy and the third time in zz. 2. enterNum asks the user to enter a floating point number. This function has no input arguments and returns...
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! + …...
What would the pseudocode be for this? The code for this has already been answered. I...
What would the pseudocode be for this? The code for this has already been answered. I need the pseudocode. Credit card numbers typically consist of 13, 15, or 16 digits. For example, 4690 3582 1375 4657 is a hypothetical credit card number. The first digit designates the system. In (3.1.1), the first digit, 4, shows that the card would be a Visa card. The following digits specify other information such as the account number and bank number. (The precise meaning...
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...