Assignment-1

advertisement
CS/COE 0449
Introduction to Systems Software & Programming
Programming Assignment-1
Due Thursday Jan.18 by 11:59pm
Directions: Submit an electronic copy of this assignment to the course ftp-submission site.
 Pack all the assignment files to a FirstName_LastNameAssign1.zip file, using WinZip or a similar application.
 Submit the zipped package by using the computer science department's anonymous FTP server
(cs.pitt.edu/incoming/CS449/ or ftp://ftp.cs.pitt.edu/incoming/CS449/). Please make sure to submit to the
project number directory.
 Submission must be before the due date, otherwise, it will be considered late. Once you submit, you can't
take the file back! Therefore, please submit your final program and only one time.
1. Write a C program that calculates x raised to the y power. The program should have a
while repetition control structure.
Start writing this program by using the following:
a) Input integer variable x with scanf.
b) Input integer variable y with scanf.
c) Initialize integer variable i to 1.
d) Initialize integer variable power to 1.
e) Multiply variable power by x and assign the result to power.
f) Increment variable i by 1.
g) Test i to see if it is less than or equal to y in the condition of a while statement.
h) Output integer variable power with printf.
i) Initialize integer variable i to 1.
2. Write a C program that inputs one five-digit number, separates the number into its individual
digits and prints the digits separated from one another by three spaces each. [Hint Use the
combinations of integer division and remainder operation.] For example, if the user types in
42139, the program should print: 4 2 1 3 9
3. A palindrome is a number or a text phrase that reads the same backward as forward. For
example, each of the following five-digit integers is a palindrome: 12321, 55555, 45554, 11611.
Write a C program that reads in a five-digit integer and determines whether or not it is a
palindrome. [Hint Use the division and remainder operators to separate the number into
its individual digits.]
4. Input an integer containing only 0s and 1s (i.e. “binary” integer) and print its decimal
equivalent. [Hint: Use the remainder and division operators to pick off the “binary” number’s
digits one at a time from right to left. Just as in the decimal number system, in which the
rightmost digit has a positional value of 1, and the next digit left has a positional value of 10, then
100, then 1000, and so on, in the binary number system the rightmost digit has a positional value
of 1, the next digit left has a positional value of 2, then 4, then 8, and so on. Thus the decimal
number 234 can be interpreted as 4 * 1 + 3 * 10 + 2 * 100. The decimal equivalent of binary
1101 is 1 * 1 + 0 * 2 + 1 * 4 + 1 * 8 or 1 + 0 + 4 + 8 or 13.]
5. The factorial of non-negative integer n is written n! (pronounced “n factorial”) and is defined
as follows:
n! = n . (n – 1) . (n – 2) . …. .1 (for values of n greater than or equal to 1).
and
n! = 1 (for n =0).
For example: 5! = 5 . 4 . 3 . 2 . 1 = 120
a) Write a C program that reads a non-negative integer and computes and prints its factorial.
b) Write a C program that estimates the value of the mathematical constant e by using the
formula:
e = 1 + 1/1! + 1/2! + 1/3! + …..
c) Write a C program that computes the value ex by using the formula
ex= 1 + x/1! + x2/2! + x3/3! + …..
Download