Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Recursive Functions & Examples: Factorial, Fibonacci, Arithmetic, & Library Functions, Exercises of Computer Programming

Information on recursive functions, their execution, and examples of their usage in solving problems such as finding factorials, generating fibonacci sequences, and performing arithmetic manipulations. It also covers the use of library functions like sqrt() and other cmath functions.

Typology: Exercises

2011/2012

Uploaded on 07/31/2012

dhairya
dhairya 🇮🇳

5

(4)

31 documents

1 / 27

Toggle sidebar

Related documents


Partial preview of the text

Download Recursive Functions & Examples: Factorial, Fibonacci, Arithmetic, & Library Functions and more Exercises Computer Programming in PDF only on Docsity! Recursive Function A recursive function is a function that call itself again!! The recursion executes while the original call to the function is still open, i.e., it is not yet finished executing. The recursive step results in many more such calls. To stop recursion, there must be some stopping condition docsity.com Factorial Problem void main() { int fact=1; int number; cout<<"\n\n Please enter an integer to find its factorial : "; cin>>number; for (int x=number ; x>0 ; x-- ) fact*=x; cout<<"\n\n\t\t The Factorial of " << number << " is : " << fact; } docsity.com Solution void Fibo() { int i=1,j=1,sum=1,num, k = 1; cout<<"Enter the limit for the series ="; cin>>num; cout << i; cout << ","; while(k < num) { cout << sum; cout << ","; i=j; j=sum; sum=i+j; k = k + 1; } } docsity.com Lab Task Now make a Recursive function to generate the Fibonacci Series. docsity.com Lab Task Two numbers are entered through the keyboard. Write a program to find the value of one number raised to power of another. (By using recursive function) docsity.com // demonstrates arithmetic assignment operators #include<iostream> using namespace std; int main() { int ans = 27; ans += 10; //same as: ans = ans + 10; cout << ans << “, “; ans -= 7; //same as: ans = ans - 7; cout << ans << “, “; ans *= 2; //same as: ans = ans * 2; cout << ans << “, “; ans /= 3; //same as: ans = ans / 3; cout << ans << “, “; ans %= 3; //same as: ans = ans % 3; cout << ans << endl; return 0; } docsity.com 37, 30, 60, 20, 2 docsity.com Increment Operators  A more specialized operator  You often need to add 1 to the value of an existing variable  count = count + 1; // adds 1 to “count”  count += 1; // adds 1 to “count”  ++count; // adds 1 to “count” docsity.com Prefix: totalweight = awgweight * ++C0uUnNt ; totalweight avgwei ght count 2 pcs ES ee 2) | | “B —— Increment ia ee & —— multiply H a) a tf S ll 3) Postfix: totalweight = awgweight * count+ +; totalwei ght avgwei ght count ey 1) | | 7 7 " EE . 2) 1088.5 = 155.5 ] * | 7 “~— Multiply i 3) _ 1088.5 | 4155.5 | & Increment O2figl1.aps d it 7169160 pickup 09.10.98 jocsity.com // demonstrates the increment operator #include <iostream> using namespace std; int main() { int count = 10; cout << “count=”<<count << endl;//displays 10 cout << “count=”<<++count << endl;//displays 11 (prefix) cout << “count=”<<count << endl;//displays 11 cout << “count=”<<count++ << endl;//displays 11 (postfix) cout << “count=”<<count << endl;//displays 12 return 0; } docsity.com Decrement operator  The decrement operator, --, behaves very much like the increment operator, except that it subtracts 1 from its operand. It too can be used in both prefix and postfix forms. docsity.com Other cmath Functions TRY!!  ceil(x)  cos(x)  exp(x)  fabs(x)  floor(x)  log(x)  log10(x)  pow(x,y)  sin(x)  tan(x) docsity.com Lab Task  Write a program that inputs a decimal number and then round off it to the nearest integer. docsity.com Lab Task  Write a function which takes two double type inputs Base and Perpendicular of a right angle triangle and returns double type hypotenuse docsity.com Code Example You can use the random() function as well for simple ranges. #include <cstdlib> #include <iostream> Using namespace std; void main( ) { for (int i=0; i<10;i++) { cout<<rand ( ) <<","; } cout<<endl; } docsity.com Lab Task  Write a program to simulate coin toss. A function flip will toss the coin. Let your program to toss a coin 100 times and print heads or tails. At the end the program will tell how many times heads and tails occurred. docsity.com Lab Task Write a program for Guessing a number using the rand( ) function, a loop and if else statement. The Output should be like this: Guess the number (1 to 10): 5 The secret number is higher Guess the number (1 to 10): 8 The secret number is lower Guess the number (1 to 10): 7 Congratulations You have guessed the Right Number! docsity.com
Docsity logo



Copyright © 2024 Ladybird Srl - Via Leonardo da Vinci 16, 10126, Torino, Italy - VAT 10816460017 - All rights reserved