printf("Sum of squares of digits of %d is %d.\n", num, square(num));
return 0;
}
#include<stdio.h>
#include<math.h>
int square(int num)
{
if(num == 0)
return 0;
else
return( pow((num%10), 2) + square(num/10) );
}
int main()
{
int num;
printf("Enter a positive integer number:\n");
scanf("%d", &num);
printf("Sum of squares of digits of %d is %d.\n", num, square(num));
return 0;
}
Output: Enter a positive integer number: 123 Sum of squares of digits of 123 is 14.
Here we are making use of pow() method present inside math.h library file. pow() takes base value as first argument and exponent value as its second argument.
Source Code: C Program To Find Sum of Squares of Digits using Recursion, Ternary/Conditional Operator and pow() method
printf("Binary Equivalent (Iterative) of %d is %d\n", num, binary(num));
printf("Binary Equivalent (Recursive) of %d is %d\n", num, binary_rec(num));
return 0;
}
int binary_rec(int num)
{
if(num == 0)
return 0;
else
return((num % 2) + 10 * binary_rec(num/2));
}
int binary(int num)
{
int rem, bin = 0, place = 1;
while(num)
{
rem = num % 2;
num = num / 2;
bin = bin + (rem * place);
place = place * 10;
}
return(bin);
}
#include<stdio.h>
int binary_rec(int);
int binary(int);
int main()
{
int num;
printf("Enter a Decimal number\n");
scanf("%d", &num);
printf("Binary Equivalent (Iterative) of %d is %d\n", num, binary(num));
printf("Binary Equivalent (Recursive) of %d is %d\n", num, binary_rec(num));
return 0;
}
int binary_rec(int num)
{
if(num == 0)
return 0;
else
return((num % 2) + 10 * binary_rec(num/2));
}
int binary(int num)
{
int rem, bin = 0, place = 1;
while(num)
{
rem = num % 2;
num = num / 2;
bin = bin + (rem * place);
place = place * 10;
}
return(bin);
}
Output 1: Enter a Decimal number 14
Iterative Logic Binary Equivalent of 14 is 1110
Recursive Logic Binary Equivalent of 14 is 1110
Output 2: Enter a Decimal number 41
Iterative Logic Binary Equivalent of 41 is 101001
Recursive Logic Binary Equivalent of 41 is 101001
Logic To Convert Decimal Number To Binary Number using Recursion
#include<stdio.h>
int binary_rec(int);
int binary(int);
int main()
{
int num;
printf("Enter a Decimal number\n");
scanf("%d", &num);
printf("\nIterative Logic\n");
printf("Binary Equivalent of %d is %d\n\n", num, binary(num));
printf("\nRecursive Logic\n");
printf("Binary Equivalent of %d is %d\n\n", num, binary_rec(num));
return 0;
}
int binary_rec(int num)
{
return( (num == 0) ? 0 : (num % 2) + 10 * binary_rec(num / 2));
}
int binary(int num)
{
int rem, bin = 0, place = 1;
while(num != 0)
{
rem = num % 2;
num = num / 2;
bin = bin + (rem * place);
place = place * 10;
}
return(bin);
}
printf("Binary Equivalent of %d is %d\n\n", num, binary(num));
printf("\nRecursive Logic\n");
printf("Binary Equivalent of %d is ", num);
binary_rec(num);
printf("\n");
return 0;
}
void binary_rec(int num)
{
if(num == 1)
printf("1");
else
{
binary_rec(num/2);
printf("%d", num%2);
}
}
int binary(int num)
{
int rem, bin = 0, place = 1;
while(num != 0)
{
rem = num % 2;
num = num / 2;
bin = bin + (rem * place);
place = place * 10;
}
return(bin);
}
#include<stdio.h>
void binary_rec(int);
int binary(int);
int main()
{
int num;
printf("Enter a Decimal number\n");
scanf("%d", &num);
printf("\nIterative Logic\n");
printf("Binary Equivalent of %d is %d\n\n", num, binary(num));
printf("\nRecursive Logic\n");
printf("Binary Equivalent of %d is ", num);
binary_rec(num);
printf("\n");
return 0;
}
void binary_rec(int num)
{
if(num == 1)
printf("1");
else
{
binary_rec(num/2);
printf("%d", num%2);
}
}
int binary(int num)
{
int rem, bin = 0, place = 1;
while(num != 0)
{
rem = num % 2;
num = num / 2;
bin = bin + (rem * place);
place = place * 10;
}
return(bin);
}
Output: Enter a Decimal number 14
Iterative Logic Binary Equivalent of 14 is 1110
Recursive Logic Binary Equivalent of 14 is 1110
Here we simply divide the number by 2 and keep passing it as new value of num to binary_rec() function, and we print num%2 once num = 1 and it returns the value 1.
Number Systems
1. Binary Number System uses base 2 and digits 01. 2. Octal Number System uses base 8 and digits 01234567. 3. Decimal Number System uses base 10 and digits 0123456789. 4. Hexadecimal Number System uses base 16 and digits 0123456789ABCDEF.
Lets write a C program to find GCD(Greatest Common Divisor) or HCF(Highest Common Factor) of two positive integer numbers input by the user using Euclid’s Algorithm and by using Recursive function call logic.
If user inputs 2 numbers n1 and n2, reduce the bigger number by modulo dividing it by the smaller number. For example, if n1 is greater than n2, then reduce the value of n1 by replacing it with n1%n2.
Assume that we’ve a function gcd() which returns gcd of 2 numbers passed to it. Ex: gcd(n1, n2);
According to Euclid’s Algorithm, we’ll get the same gcd if we reduce the bigger number by modulo dividing it by smaller number.
If n1 > n2 we need to pass gcd(n1%n2, n2); If n2 > n1, we need to pass gcd(n1, n2%n1);
We need to recursively execute above 2 lines of logic until either n1 is 0 or until n2 is 0. If n1 is 0, then value present in n2 is the gcd of (n1,n2). If n2 is 0, then value present in n1 is the gcd of (n1,n2).
Video Tutorial: C Program To Find GCD of Two Numbers using Recursion: Euclid’s Algorithm
printf("Enter a positive number to find its Factorial\n");
scanf("%d", &num);
printf("\nFactorial of %d is %d.\n", num, fact(num));
return 0;
}
int fact(int num)
{
if(num)
return(num * fact(num - 1));
else
return 1;
}
#include<stdio.h>
int fact(int);
int main()
{
int num;
printf("Enter a positive number to find its Factorial\n");
scanf("%d", &num);
printf("\nFactorial of %d is %d.\n", num, fact(num));
return 0;
}
int fact(int num)
{
if(num)
return(num * fact(num - 1));
else
return 1;
}
Output 1: Enter a positive number to find its Factorial 7
Factorial of 7 is 5040.
Output 2: Enter a positive number to find its Factorial 8
Factorial of 8 is 40320.
Logic To Find Factorial of a Number using Recursion
We ask the user to enter a positive integer number and we pass this number to a function called fact().
Inside fact() function Inside fact() function, we check if the number is non-zero, if true, we execute the code inside if block orelse(if num is zero), then the code inside else block gets executed.
Inside if block, we add the factorial of (num-1) to the value of num.
num * fact(num – 1)
and return the result to the calling function.
Inside else block, we simply return 1. Because factorial of 0 is 1. So when num is 0, we return 1.
Example:
num
num * fact(num-1)
5
5 * fact(4)
4
4 * fact(3)
3
3 * fact(2)
2
2 * fact(1)
1
1 * fact(0)
Value Returning – Control Shifting back.
Function
Return Value
Result
1 * fact(0)
return 1;
1 * 1 = 1
2 * fact(1)
1
2 * 1 = 2
3 * fact(2)
2
3 * 2 = 6
4 * fact(3)
6
4 * 6 = 24
5 * fact(4)
24
5 * 24 = 120
Finally 120 will be returned to main() method where we call the fact() method.
Source Code: C Program To Find Factorial of a Number using Recursion and Ternary or Conditional Operator
printf("Enter a positive number to find its Factorial\n");
scanf("%d", &num);
printf("\nFactorial of %d is %d.\n", num, fact(num));
return 0;
}
int fact(int num)
{
return( (num > 0) ? (num * fact(num - 1)) : 1 );
}
#include<stdio.h>
int fact(int);
int main()
{
int num;
printf("Enter a positive number to find its Factorial\n");
scanf("%d", &num);
printf("\nFactorial of %d is %d.\n", num, fact(num));
return 0;
}
int fact(int num)
{
return( (num > 0) ? (num * fact(num - 1)) : 1 );
}
Output 1: Enter a positive number to find its Factorial 5
Factorial of 5 is 120.
Output 2: Enter a positive number to find its Factorial 6
Whenever there is a recursive function call, function instance and the memory associated with that function instance gets pushed into the stack. When any function instance returns a value, that function instance and memory associated with that function instance gets popped out or removed or gets deleted from the memory stack.
#include<stdio.h>
#include<math.h>
int reverse(int num)
{
if(num)
return( (num%10) * pow(10, (int)log10(num)) + reverse(num/10) );
else
return 0;
}
int main()
{
int num, isNegative = 1, result = 0;
printf("Enter a number to reverse\n");
scanf("%d", &num);
isNegative = (num < 0);
if(isNegative)
num *= -1;
result = reverse(num);
if(isNegative)
result *= -1;
printf("Reverse of %d is %d\n", num, result);
return 0;
}
Output 1: Enter a number to reverse 12345 Reverse of 12345 is 54321
Output 2: Enter a number to reverse -12345 Reverse of -12345 is -54321
Logic To Reverse a Number using Recursion
We ask the user to input a number, and store it inside variable num. If num is negative, then we store 1(true) inside variable isNegative or store 0(false) inside variable isNegative if num is positive.
If isNegative is 1, then we change the value of num to positive and send it as argument to reverse() function.
Inside reverse() function If num is 0, it returns 0. Else we recursively call reverse() function as follows:
num % 10 gives the last digit in the number. log10(num) gives the length of the number or the number of digits present in the number. The count starts from 0. Ex: if num = 12345, then log10(num) would give 4.
num/10 reduces the number by 1 digit from right.
pow(10, (int)log10(num)) is used to properly place the reminder value in its decimal place.
Lets assume that user has input 1234 as value of num.
i.e., num = 1234;
Recursive Function Calls – Stacking of calls
num
num % 10
num%10*log10(num)
num/10
1234
reverse(1234)
1234
4
4 * 103 = 4000
reverse(123)
123
3
3 * 102 = 300
reverse(12)
12
2
2 * 101 = 20
reverse(1)
1
1
1 * 100 = 1
reverse(0)
Value Returning – Control Shifting back.
Return To
result
resolve
Return Value
reverse(0)
0
reverse(1)
1* 100+reverse(0)
1 * 1 + 0
1
reverse(12)
2*101+reverse(1)
2 * 10 + 1
21
reverse(123)
3*102+reverse(12)
3 * 100 + 21
321
reverse(1234)
4*103+reverse(123)
4 * 1000 + 321
4321
Note: Whenever there is a call to a function, the function instance(and memory associated with it) gets stored in the memory stack. Once the function returns value to calling function, the control shifts back to the calling function and the memory instance gets popped or deleted out of the memory stack immediately after returning the value.
Source Code: C Program To Reverse a Number using Recursion and Ternary Operator
printf("Reverse of %d is %d\n", num, reverse(num, count));
return 0;
}
#include<stdio.h>
#include<math.h>
int reverse(int num, int len)
{
if(num)
return( (num%10) * pow(10, len-1) + reverse(num/10, len-1) );
else
return 0;
}
int main()
{
int num, count = 0, temp;
printf("Enter a number to reverse\n");
scanf("%d", &num);
temp = num;
while(temp)
{
count++;
temp = temp / 10;
}
printf("Reverse of %d is %d\n", num, reverse(num, count));
return 0;
}
Output 1: Enter a number to reverse 123456 Reverse of 123456 is 654321
Output 2: Enter a number to reverse -123456 Reverse of -123456 is -654321
Logic To Reverse a Number using Recursion without using log10() function
Inside main method itself we calculate the number of digits present in the user entered number and then pass that information to the function reverse() along with the user entered number.
reverse(num, count);
Inside reverse() function Inside reverse() function, we get the reminder by modulo dividing num by 10. We place this remainder in it’s decimal place by multiplying it with
pow(10, len-1)
where len is the length or the number of digits present in the number.
Next we recursively call reverse() function and pass the value of (num/10) and (len-1).
Finally we combine all these results using below formula and return the value to the calling function recursively, until num is equal to 0: