0% found this document useful (0 votes)
7 views2 pages

ITP Exp9

The document outlines a programming laboratory experiment for Fall 2022, focusing on three main tasks in C programming. The first task involves decoding a 4-digit password using functions, the second task requires implementing an iterative algorithm to find real roots of a nonlinear equation, and the third task checks if generated numbers are perfect numbers. Each task includes specific steps and example outputs to guide the programming process.
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)
7 views2 pages

ITP Exp9

The document outlines a programming laboratory experiment for Fall 2022, focusing on three main tasks in C programming. The first task involves decoding a 4-digit password using functions, the second task requires implementing an iterative algorithm to find real roots of a nonlinear equation, and the third task checks if generated numbers are perfect numbers. Each task includes specific steps and example outputs to guide the programming process.
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/ 2

Introduction To Programming Laboratory, Fall 2022

COMPUTER PROGRAMMING LABORATORY


Experiment # 9:
Functions III

QUESTIONS

1) Write a C program to decode a 4-digit password (as an example 2785). The most
significant digit (MSD) of this password is 2 and the least significant digit of this password is 5.
Your program must perform the following steps.

i) In the main function generate and print a 4-digit password using the rand function. Then call
the function separate void separate (int password);
ii) The separate function must separate the digits of the password one-by-one starting from the
MSD (i.e 2 first, then 7, then 8 and finally 5).
iii) For each digit of the password separate function calls the function decode int decode (int
digit)
iv) Decode function must guess the digit and returns the guess to the separate function. The
separate function must print the guessed digit of the password.
An example output of the program could be as follows

2) There are iterative algorithms to find real roots of nonlinear equations. In this question,
you need to implement one whose formula given below in C programming language.

𝑓(𝑥𝑛 )
𝑥𝑛+1 = 𝑥𝑛 −
𝑓′(𝑥𝑛 )

where 𝑓′(𝑥𝑛 ) is derivative of 𝑓(𝑥𝑛 ) function. In this question, you will find root of 𝑓(𝑥) =
3𝑥 2 − cos 𝑥, whose derivative function 𝑓′(𝑥) = 6𝑥 + sin 𝑥.

Here the steps:


1. In main function prompt initial value 𝑥0 and number of iteration n from user. Then, the
main function calls function whose prototype double findRoot(double x0, int n).
2. In findRoot function, 𝑥𝑛+1 is recursively calculated for n iterations. To this end, functions
with prototype double foo(double x) and double derivative_foo(double x) must be
defined to yield output value for 𝑓(𝑥) and 𝑓′(𝑥), respectively. For each iteration, print
the 𝑥𝑛 value and f(𝑥𝑛 ) followed by iteration number. Then, return the final value of 𝑥𝑛 .
3. Print the final value of 𝑥𝑛 and f(𝑥𝑛 ) in the main function.
4. Test your program with 𝑥0 = 35 and n = 10.

An example output for this program could be as follows:

E-mail : [email protected]
Introduction To Programming Laboratory, Fall 2022

3) In number theory, a perfect number is a positive integer that is equal to the sum of its
proper positive divisors, that is, the sum of its positive divisors excluding the number itself.
Example: The first perfect number is 6. Its proper divisors are 1, 2, and 3, and 1 + 2 + 3 = 6.
The next perfect number is 28 = 1 + 2 + 4 + 7 + 14.

Write a C program to check whether the generated 10 number is a perfect number or not.

i) In the main function enter the shifting and scaling factors from the keyboard for the range of
the random numbers. Then, call the function checkPerfect 10 times for the generated 10
random numbers (Enter 3 as shifting value, 8 as scaling factor).
ii) The checkPerfect function checks whether a number is perfect or not. The checkPerfect
function returns 1 if the number is perfect, otherwise 0. int checkPerfect(int num);

An example output of the program could be as follows:

E-mail : [email protected]

You might also like