You are on page 1of 2

C programming

Laboratory tasks

Lab 4 – Loops

1. Write a program to request values for first and last and print a table of squares from first to
last.
2. Write a program to request a value for n; the user is then asked to enter n numbers, one at a
time. The program calculates and prints the sum of the numbers. The following is a sample
run:
How many numbers? 3
Enter a number? 12
Enter a number? 25
Enter a number? 18

The sum of the 3 numbers is 55

3. Write a program to request an integer n from 1 to 9 and print a line of output consisting of
ascending digits from 1 to n followed by descending digits from n-1 to 1.
For example, if n = 5, print the line 123454321
4. Write a program that prompts user for an integer and digit. Then define whether the given
digit exists in the prompted integer.
a. Extend it, so that it defines whether there are 2 sequential digits in the number
5. Write a program to check if the number is a palindrome
6. Let’s take some positive integer n. We shall change it nest way: if number is even, divide it
by 2, if odd, add 1 to it. After several such changes we always get the number 1. For
example, from number 11 we get 12, then 6, 3, 4, 2 and at last 1. So, to get 1 from 11 we
need to make 6 changes.
7. The positive integer m is called a smooth divisor of n if the quotient and remainder of
dividing n by m are equal. The positive integer n is given. Find the number of its
smooth divisors.
8. (Table of Decimal, Binary, Octal and Hexadecimal Equivalents) Write a program
that prints a table of the binary, octal and hexadecimal equivalents of the decimal
numbers in the range 1 through 256. [Note: You can display an integer as an octal or
hexadecimal value with the conversion specifiers %o and %X, respectively.]

9. (Calculating the Value of π) Calculate the value of π from the infinite series

4 4 4 4 4
π=4− + − + − + …
3 5 7 9 11

Print a table that shows the value of π approximated by one term of this series, by two
terms, by three terms, and so on. How many terms of this series do you have to use
before you first get 3.14? 3.141? 3.1415? 3.14159?
10. Write a program that estimates the value of the mathematical constant e by using the
formula:
1 1 1
e=1+ + + +…
1! 2 ! 3 !

You might also like