Category Archives: Functions

C/C++ Program to Find GCD of Two Numbers Using Recursion

In this program we will use recursion and Euclid’s algorithm to find greatest common divisor of two numbers. The definition of Euclid’s algorithm is as follows: Also Read: C program to find factorial of any number using recursion Also Read: How to Convert a Recursive Function or Algorithm to Non-Recursive? C Program C++ Program

C/C++ Program for Fibonacci Series Using Recursion

Series 0, 1, 1, 2, 3, 5, 8, 13, 21 . . . . . . . is a Fibonacci series. In Fibonacci series, each term is the sum of the two preceding terms.   The C and C++ program for Fibonacci series using recursion is given below.   C Program C++ Program   Output:

C++ Templates: Program to Swap Two Numbers Using Function Template

What are Templates in C++? Templates help in defining generic classes and functions and hence allow generic programming. Generic programming is an approach where generic data types are used as parameters and the same piece of code work for various data types. Function templates are used to create family of functions with different argument types.… Read More »

C++ Program to Compute High Precision Division for Integers

As we know, the arithmetic operations in programming languages are limited by precision accuracy. That means you will not get an exact answer but a rounded-close answer. For example, if you have 10 divided by 3 then you will get answer like 3.33333339555496.   The following program will describe a quick and efficient method to… Read More »

C program to find factorial of any number using recursion

#include<stdio.h> #include<conio.h> void main() { int fac,n; int factorial(int); clrscr(); printf(“Enter any number:”); scanf(“%d”,&n); fac=factorial(n); printf(“Factorial=%d”,fac); getch(); } int factorial(int x) { int f; if(x==1||x==0) return 1; else f=x*factorial(x-1); return f; }

C++ program to swap values of two variables using pass by reference method

#include<iostream.h> #include<conio.h> void main() { clrscr(); int a,b; void swap(int &,int &); cout<<“Enter two values:”; cin>>a>>b; cout<<“nBefor swapping:na=”<<a<<“tb=”<<b; swap(a,b); cout<<“nnAfter swapping:na=”<<a<<“tb=”<<b; getch(); } void swap(int & x,int & y) { int temp; temp=x; x=y; y=temp; }

C++ Program to find cube of a number using function

#include<iostream.h> #include<conio.h> void main() { clrscr();                                                //to clear screen float cube(float);                                  //function prototype float a,cu; cout<<“Enter any… Read More »