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


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 »

C++ Program to do Addition,subtraction and multiplication of two numbers using function

#include<iostream.h> #include<conio.h> int res; void main() { clrscr(); int sum(int,int); int sub(int,int); int mul(int,int); int a,b,m,su,s; cout<<“Enter two numbers:”; cin>>a>>b; s=sum(a,b); su=sub(a,b); m=mul(a,b); cout<<“Sum:”<<s<<“nSubtraction:”<<su<<“nMultiplication:”<<m; getch(); } sum(int a,int b) { res=a+b; return(res); } sub(int a,int b) { res=a-b; return(res); } mul(int a,int b) { res=a*b; return(res); }

C++ Program to print following series using function: x + x^3/3! + x^5/5! +…….+ x^n/n!

#include<iostream.h> #include<conio.h> void main() { clrscr(); int x,n; double sum(int,int); double res; cout<<“x + x^3/3! + x^5/5! +…….+ x^n/n!”; cout<<“nnEnter value of x and n:”; cin>>x>>n; res=sum(x,n); cout<<“nSum of series is “<<res; getch(); } double sum(int a,int b) { long power(int,int); int i,j; double s=0,fac=1; long p; for(i=1;i<=b;i+=2) { p=power(a,i); for(j=1;j<=i;++j) { fac*=j; } s+=p/fac;… Read More »