You are on page 1of 2

PDS Lab, Section – 18 , Date: 5th Sept 2023

Assignment – 3 [Loops : While, For, Do-While]


-------------------------------------------------------------------------------------------------------------------------------
Instructions
-------------------------------------------------------------------------------------------------------------------------------

1. Create a directory named as Lab-3.


2. Give the name of the program as <p>.c where <p> implies the problem number,
like 1.c, 2.c, 3.c, etc. Store all the programs of this week under this directory.
3. You should upload all .c files (1.c, 2.c, 3.c ....) to the Moodle course web page
latest by 5.00 PM (without penalty). The cutoff time will be till 5.15 PM with a
penalty of 25% on your secured marks (i.e., if you secured 80 marks, after
penalty you will get 60 marks). Beyond 5.15 PM, the moodle system will not allow
you to submit, as a result you will get zero.

---------------------------------------------------------------------------------------------------------------------
1) Write a C program that prints the first n integral multiples of i on each line for i=1,2,..,m. The
values of n and m should be taken from the user and m, n should be positive such that 0 < (m,
n) < 15. Otherwise, an appropriate error message has to be printed. Also, the user has to be
asked for inputs as long as he/she does not type valid inputs. Each number should occupy a
width of 5 in your display. Also, there should be a space between successive numbers in a
row. (15M)
Example:
Enter m followed by n:22 12
m and n should be positive such that m,n<15. Enter again:12 15
m and n should be positive such that m,n<15. Enter again:-12 4
m and n should be positive such that m,n<15. Enter again:4 7
1 2 3 4 5 6 7
2 4 6 8 10 12 14
3 6 9 12 15 18 21
4 8 12 16 20 24 28

2) Write a C program to determine the number of lower case alphabets, uppercase alphabets,
single-quotes, digits, words, sentences, special characters (which are not mentioned above)
and characters. (Solve this problem using Do-While) (25M)
Example :
Enter the input text
Today's PDS Lab is on Loops. It Contains 1:While 2:For and 3:Do-While.
Number of uppercase letters = 12
Number of lowercase letters = 37
Number of single quotes = 1
Number of special characters = 5
Number of digits = 3
Number of words = 11
Number of sentences = 2
Number of characters = 71

3) Write a C program that calculates the mean and median of the first n prime numbers. The
value of n should be taken from the user interactively. Display the list of prime numbers,
their mean, median and also print a message whether the mean and median are the same.
If they are not the same, then also print a message telling the user the difference between
the mean and the median. (30M)

Enter the value of n(a natural number):


10
Prime[1] = 2
Prime[2] = 3
Prime[3] = 5
Prime[4] = 7
Prime[5] = 11
Prime[6] = 13
Prime[7] = 17
Prime[8] = 19
Prime[9] = 23
Prime[10] = 29
Mean and Median are not same.
The median of first n prime numbers is:12.00
The mean of first n prime numbers is:12.90
The difference between mean and median is:0.90

4) Write a C program to print the following pattern of numbers by giving the number of rows (N)
as input taken from the user and N should be positive such that 0 < N < 10. Otherwise, an
appropriate error message has to be printed. Also, the user has to be asked for the input as
long as he/she does not type valid input. (30M)

Enter the number of rows to be printed : 6


1
2 3 2
3 4 5 4 3
4 5 6 7 6 5 4
5 6 7 8 9 8 7 6 5
6 7 8 9 10 11 10 9 8 7 6

You might also like