0% found this document useful (0 votes)
18 views

Section 8: Part A: Tutorial Problems For Practice

The document contains instructions for a programming assignment with 5 problems. It instructs students to write programs to: [1] Find the third largest of a set of integers input by the user without using arrays; [2] Read a 4-digit number and sum its digits; [3] Calculate the sum of the first n terms of a mathematical series given x and n as input; [4] Find and print all perfect numbers less than or equal to 1000; [5] Find the maximum, minimum, and average of numbers input terminated by 0 without using arrays. Students are told to submit their solutions in a ZIP file to the course Moodle page by 12:45 hrs.

Uploaded by

Sohini Roy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views

Section 8: Part A: Tutorial Problems For Practice

The document contains instructions for a programming assignment with 5 problems. It instructs students to write programs to: [1] Find the third largest of a set of integers input by the user without using arrays; [2] Read a 4-digit number and sum its digits; [3] Calculate the sum of the first n terms of a mathematical series given x and n as input; [4] Find and print all perfect numbers less than or equal to 1000; [5] Find the maximum, minimum, and average of numbers input terminated by 0 without using arrays. Students are told to submit their solutions in a ZIP file to the course Moodle page by 12:45 hrs.

Uploaded by

Sohini Roy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Section 8

PDS Lab Lab-3 25.08.2016


 

Part‐A: Tutorial Problems for Practice 

Instructions:
 Draw flowchart and or algorithm depicting how to solve each problem in this part.
 Submit your work latest by today. Please clearly write your name, roll number and mobile
number on the front page of the sheets.
 Create a sub directory named as LabT3 under your home directory.
 Give the name of the programs as <R> <T>1.c, <R> <T>2.c, .. etc. for the tutorial problems
problem 1, 2….., respectively. Here <R> implies your Registration No.
 Store all the programs under this tutorial part in the directory LabT3

1. Let n, i and sum be int variables. The user enters a positive value of n. Which of the
following program segments prints the largest value of sum?

a) sum = 0; i = 1; while (++i < n) sum += i; printf("%d", sum);

b) sum = 0; i = 1; while (i++ < n) sum += i; printf("%d", sum);

c) for (sum = 0, i = 1; i < n; i++) sum += i; printf("%d", sum);

d) for (sum = 0, i = 1; i <= n; ++i) sum += i; printf("%d", sum);

[Hint: First try to have the answer of your own and then verify your answer by writing
appropriate programs for each statement.]

2. Consider the program segment given below.


int sum = 0;
int i = 0;
while (i < 5)
{
sum = sum + i;
i++;
}
printf(“%d\n”,sum);
 
Suppose, we replace the while loop in the segment above with a for loop. Which of the
following for loops will result in the same value of sum printing out?
a) for (int i = 0; i <= 5; i++)
sum = sum + i;
b) for (int i = 1; i <= 5; i++)
sum = sum + i;

c) for (int i = 1; i < 5; i++)


sum = sum + i;
d) for (int i = 2; i < 5; i++)
sum = sum + i;
e) for (int i = 1; i < 6; i++)
sum = sum + i;

[Hint: First try to have the answer of your own and then verify your answer by writing
appropriate programs for each statement.]

3. What is the output of the following program segment?


     
int x = 1357;
int z = 0;

for( int i = x; i>0; i = i/10) {


z = z + (i%10);
}
printf(“%d”,z);

[Hint: Run the program for the above and try to understand the output.]

 
 
 
 
 
 
 
 
 
 
 
 
 
 
Please turn the page…. 
Part‐B: Assignment Problems 

Instructions:
 Create a sub directory named as LabA3.
 Give the name of the programs as <R> <A2>1.c, <R> <A2>2.c, …, etc. for the tutorial
problems 1, 2….., respectively. Here <R> implies your Registration No.
 Store all the programs under this tutorial part in the directory LabA3.
 You should upload your programs to the Moodle course web page. Preferably in ZIP form
latest by 12:45 hrs. today only.
 
1. Read n integers and print the third largest among them. n is input by user. Don’t use any
array.

2. Read any 4 digit number and print the sum of its digits.

3. Find the sum of the first n terms of the following series.

1 + x + x2/2! + x3/3! + …. 

[Hint: Value of x and n is input by user. Do not use the factorial or power functions available in
math.h]

4. A number is called a perfect number, if the number is equal to the sum of all its positive
divisors except the number itself. For example, 6 = 1 + 2 + 3, 28 = 1 + 2+ 4 + 7 + 14 are
the two perfect numbers. Find and print all the perfect numbers less than or equal to
1000.

5. Read a sequence of integer numbers (terminated by 0) and find the maximum, minimum
and average value of the numbers (excluding 0) that have been entered.

[Hint: You should not use any array to store the number you read from the keyboard.]

[Submit your solutions to Moodle course management system, latest by 12:45 hrs. toady.] 

You might also like