Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Please do 2-6 please C language. Thank you . Write a program that asks the user to enter a two-digit number, then prints the number

Please do 2-6 please C language. Thank you image text in transcribed
image text in transcribed
image text in transcribed
image text in transcribed
image text in transcribed
image text in transcribed
image text in transcribed
. Write a program that asks the user to enter a two-digit number, then prints the number with its digits reversed. A session with the program should have the following appearance: Enter a two-digit number: 28 The reversal is: 82 Read the number using %a, then break it into two digits. Hint: If n is an integer, then n % 10 is the last digit in n and n /10 is n with the last digit removed. 2. Extend the program in Programming Project I to handle three-digit numbers. R ie number without using arithmetic to split the number into digits. Hinn: See the upc. c pro- gram of Scction 4.I 4. Write a program that reads an integer entered by the user and displays it in octal (base 8): Enter a number between o and 32767: 1953 In octal, your number is: 03641 The output should be displayed using five digits, even if fewer digits are sufficient. Hint: To convert the number to octal, first divide it by 8; the remainder is the last digit of the octal number (1. in this case). Then divide the original number by 8 and repeat the process to arrive at the next-to-last digit. (printf is capable of displaying numbers in base 8, as we'll sce in Chapter 7, so there's actually an easier way to write this program.) 5. Rewrite the upc.c program of Section 4.1 so that the user enters 11 digits at one time. instead of entering one digit., then five digits, and then another five digits. Enter the first 11 digits of a UPC: 01380015173 Check digit: 5 6. European countries use a 13-digit code, known as a European Article Number (EAN) instead of the 12-digit Universal Product Code (UPC) found in North America. Each EAN ends with a check digit, just as a UPC does. The technique for calculating the check digit is also similar: Add the second. fourth, sixth, cighth, tenth. and twelfth digits. Add the first, third. fifth. seventh. ninth. and eleventh digis. Multiply the first sum by 3 and add it to the second sum. The arithmetic operators-operators that perform addition, subtraction, multipli- cation, and division-are the workhorses of many programming languages, includ- ing C. Table 4.1 shows C's arithmetic operators. Table 4.1 Arithmetic Operators Unary Binary Additive Multiplicative *multiplication remainder +unary plus addition unary minussubtraction division The additive and multiplicative operators are said to be binary because they require two operands. The unary operators require one operand: i +1 +used as a unary operator ./ iiused as a unary operator ./ The unary + operator does nothing: in primarily to emphasize that a numeric constant is positive. fact. l didn't even exist in K&R C. It's used The binary operators probably look familiar. The only one that might not is t. the remainder operator. The value of i % j is the remainder when i is divided by j. For example, the value of 10 % 3 is 1, and the value of 12 % 4 is 0 binary operators in Table 4.1-with the exception of-allow either inte ger or floating-point operands, with mixing allowed. When int and float oper- ands are mixed, the result has type float. Thus. 9 2.5f has the value 11.5 and 6.7t /2 has the value 3.35 The / and operators require special care: - The / operator can produce surprising results. When both of its operands are integers, the/ operator "truncates" the result by dropping the fractional part Thus, the value of 1 /2 is 0, not 0.5 . The t operator requires integer operands: if either operand is not an integ behavior-44 on the program won't compile. -Using zero as ihe right operand of either / or \ causes undefined behavior. -Describing the result when / and % are used with negative operands i ndelined The C89 standard states that if cither operand is negative, the result of a div sion can be rounded either up or down. (For example, the value of -9/ 7 i- ijk is equivalent to i (jk) -i * -j s equivalent to (-1) * (-j) +i+jk is equivalent to (+i) (j/ k) Operator precedence rules alone aren't enough when an expression contains tw or more operators at the same level of precedence. In this situation, the associativit 56 Chapter 4 Expressions of the operators comes into play. An operator is said to be lefi associative if it groups from left to right.The hinary arithmetic operators(*,/.. +, and -)are all left asso- ciative, SO i - j - k is cquivalent to (i - j) -k * j / k is equivalent to (i * j) / k An operator is right associative if it groups from right to left. The unary arithmetic operators (+ and -) are both right associative. so is cquivalent to(+i) Precedence and associativity rules are important in many languages, but espe- cially so in C. However, C has so many operators (almost fifty!) that few program- mers bother to memorize the precedence and associativity rules. Instead. they able of operators Appendix A consult a table of operators when in doubt or just use plenty of parentheses. Computing a UPC Check Digit For a number of years, manufacturers of goods sold in U.S. and Canadiun tto PROGRAM Multiply the first sum by 3 and add it to the second sum. Subtract 1 from the total. Compute the remainder when the adjusted total is divided by 10. Subtract the remainder from 9 Using the Stouffer's example, we get 0+3+0+3 8 for the first sum and 1+8+0+5+7-21 for the second sum. Multiplying the first sum by 3 and add- ing the second yields 45. Subtracting I gives 44. The remainder upon dividing by 10 is 4. When the remainder is subtracted from 9, the result is 5. Here are a couple of other UPCs, in case you want to try your hand at computing the check digit (raiding the kitchen cabinet for the answer is not allowed): Jif Creamy Peanut Butter (18 oz.): Occan Spray Jellied Cranberry Sauce (8 oz.): 0 5150o 24128? 0 31200 01005? The answers appear at the bottom of the page Let's write a program that calculates the check digit for an arbitrary UPC We'll ask the user to enter the first 11 digits of the UPC, then we'll display the cor- responding check digit. To avoid confusion, we'lI ask the user to enter the number in three parts: the single digit at the left, the first group of five digits, and the sec- ond group of five digits. Here's what a session with the program will look like Enter the first (single) digit: Enter Eirst group of five digits: 13800 Enter second group of five digits: 15173 Check digit: 5 Instead of reading each digit group as a five-digit number, we'll read it as five one-digit numbers. Reading the numbers as single digits is more convenient; also, we won't have to worry that one of the five-digit numbers is too large to store in an int variable. (Some older compilers limit the maximum value of an int variable to 32.767.) To read single digits, we'll use acant with the %ld conversion speci- fication, which matches a one-digit integer upo.c i Computes a Universal Product Code check digit linclude int main (void) Eirst sum, second sum, total; printf("Enter the first (single) digit: ) scanf ("1d, &d) printf ("Enter first group of five digits:) scanf("%ld%1d\ld$1d%ld". &11, &i2, &13, &14, &15) ; printf("Enter second group of five digits: ") The wissing clhock digits are 8 (Ji) and 6 (Ocean Spray) 58 Chapter 4 Expressions first_sum-d12 i4+jl +13js second sum-i1 + i3 + i5 + j2 + j4; total 3eirst_sum second_sum printf ("Check digit : idin". 9 ( (total - 1) 10)) ; retyrn o Note that the expression 9- (( total-1) 10) could have been written as 9- (tota 1-1) % 10, but the extra set of parentheses makes it easier to

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Intelligent Information And Database Systems Asian Conference Aciids 2012 Kaohsiung Taiwan March 2012 Proceedings Part 2 Lnai 7197

Authors: Jeng-Shyang Pan ,Shyi-Ming Chen ,Ngoc-Thanh Nguyen

2012th Edition

3642284892, 978-3642284892

More Books

Students also viewed these Databases questions

Question

16. Why is the TCO so high?

Answered: 1 week ago

Question

Networking is a two-way street. Discuss this statement.

Answered: 1 week ago