Professional Documents
Culture Documents
C++ Number Programs and Recursion
C++ Number Programs and Recursion
TRAILING ZEROS
#include<bits/stdc++.h>
#include <iostream>
using namespace std;
int fact(int n) {
if ((n==0)||(n==1))
return 1;
else
return n*fact(n-1);
}
int main() {
int n = 5;
int factorial = fact(n);
int result = 0;
while (factorial % 2 == 0)
{
result++;
factorial = factorial / 10;
}
cout<<"Trailing zeros "<<result;
return 0;
}
Check Penulty
#include <iostream>
#include <cmath>
using namespace std;
int main() {
int arr[5], n = 3;
cout<<"Enter "<<n<<" elements"<<endl;
for(int i=0; i < n; i++)
{
cin>>arr[i];
}
cout<<"Penulty is "<<check_penulty(arr,n);
#include<iostream>
using namespace std;
int main()
{
cout<<isPalindrome(121);
return 0;
}
#include<iostream>
using namespace std;
int main()
{
int min = 100;
int max = 150;
for(int i=min; i<=max; i++) {
if(isPalindrome(i)) {
cout<<i <<" ";
}
}
return 0;
}
#include<iostream>
using namespace std;
int main()
{
cout<<isPalindrome(153);
return 0;
}
#include <iostream>
#include <math.h>
using namespace std;
bool checkprime(int num)
{
if (num == 1)
return false;
int i = 2;
for (i = 2; i < sqrt(num); i++)
{
if (num % i == 0)
return false;
}
return true;
}
void PrintPrimesbwrange(int a, int b)
{
for (int i = a; i <= b; i++)
{
if (checkprime(i))
{
cout << i << " ";
}
}
}
int main()
{
int a = 11, b = 17;
PrintPrimesbwrange(a, b);
return 0;
}
#include <iostream>
using namespace std;
bool isPerfect(int n) {
int sum = 0;
for (int i = 1; i <= n - 1; i++) {
if (n % i == 0)
sum = sum + i;
}
if (sum == n)
return true;
else return false;
}
int main() {
if (isPerfect(6))
{
cout<<"It is a perfect number";
}
else
cout<<"It's not a perfect number";
return 0;
}
int Sum_of_N_Natural_Numbers(int n)
{
int sum = 0;
for (int i = 1; i <= n; i++) {
sum = sum + i;
}
return sum;
}
int main()
{
int num = 10;
cout<<Sum_of_N_Natural_Numbers(num);
return 0;
}
#include<bits/stdc++.h>
using namespace std;
// Driver function
int main()
{
int n = 20;
float a = 2.5, d = 1.5;
cout<<sumOfAP(a, d, n);
return 0;
}
#include <bits/stdc++.h>
using namespace std;
int main()
{
int year;
year=2000;
if(year % 400 == 0)
cout << year << " is a Leap Year";
else
cout << year << " is not a Leap Year";
return 0;
}
#include<bits/stdc++.h>
using namespace std;
void MinMax(int n)
{
int d,mini=INT_MAX,maxi=INT_MIN;
while(n!=0)
{
d=n%10;
mini = min(mini,d);
maxi=max(maxi,d);
n=n/10;
}
#include<bits/stdc++.h>
using namespace std;
// Base Condition.
if(N <= 1)
{
return N;
}
int main(){
}
Calculate the Power of a Number : Binary Exponentiation
#include<bits/stdc++.h>
using namespace std;
int main()
{
int n = 5, k = 3;
int ans = 1;
for (int i = 0; i < k; i++) {
ans = ans * n;
}
cout <<n<<" raised to the power "<<k<<" is "<< ans;
}
#include <stdio.h>
void primefactor(int num) {
printf("Prime factors of the number : ");
for (int i = 2; num > 1; i++) {
while (num % i == 0) {
printf("%d ", i);
num = num / i;
}
}
}
int main() {
int num;
num=12;
primefactor(num);
return 0;
}
#include<iostream>
using namespace std;
//Function to calculate the factorial of the individual
digits
int Factorial(int n) {
int fact = 1;
for (int i = 1; i <= n; i++) {
fact = fact * i;
}
return fact;
}
//Function to compute sum of factorials
int Strong_No(int num) {
int sum = 0;
//Extract all the digits from num
while (num > 0) {
int digit = num % 10;
sum = sum + Factorial(digit);
num = num / 10;
}
return sum;
}
int main() {
int number = 145;
int answer = Strong_No(number);
if (answer == number && number != 0) {
cout << "YES";
}
else {
cout << "NO";
}
}
Check if a number is Automorphic Number
Given a number, check if it is automorphic or not. A
number is called an Automorphic number if and only if its
square ends in the same digits as the number itself.
Input Format: N = 76
Result: Automorphic Number
Explanation: Calculating 76 * 76 gives 5776, it ends with
the given number.
#include <iostream>
bool isAutomorphic(int N) {
int sq = N * N;
while (N > 0) {
int main() {
int N = 25;
if(isAutomorphic(N))
cout<<"Automorphic Number"<<endl;
else
cout<<"Not Automorphic Number"<<endl;
return 0;
}
Approach:
Recursively call gcd(b,a%b) function till the base
condition is hit.
b==0 is the base condition.When base condition is hit
return a,as gcd(a,0) is equal to a.
#include<bits/stdc++.h>
using namespace std;
int gcd(int a, int b) {
if (b == 0) {
return a;
}
return gcd(b, a % b);
}
int main()
{
int a = 4, b = 8;
cout <<"The GCD of the two numbers is "<<gcd(a, b);
}
#include<bits/stdc++.h>
using namespace std;
int gcd(int a, int b) {
if (b == 0) {
return a;
}
return gcd(b, a % b);
}
int main()
{
int a = 4, b = 8;
int g = gcd(a, b);
int lcm = (a * b) / g;
cout <<"The LCM of the two given numbers is "<<lcm;
}
if (d > 0) {
cout << "Roots are real and different \n";
double root1 = (double)(-b + sqrt_val) / (2 * a);
double root2 = (double)(-b - sqrt_val) / (2 * a);
cout << root1 <<"\n"<< root2;
}
else if (d == 0) {
cout << "Roots are real and same \n";
double root1 = -(double)b / (2 * a);
double root2 = -(double)b / (2 * a);
cout << root1 <<"\n" <<root2;
}
else // d < 0
{
cout << "Roots are complex \n";
cout << -(double)b / (2 * a) << " + i" << sqrt_val
<< "\n"
<< -(double)b / (2 * a) << " - i" << sqrt_val;
}
}
int main()
{
int a = 1, b = -3, c = -10;
Roots(a, b, c);
return 0;
}
Approach:
Maintain a variable sum to store sum of digits of the
number.
Now check if n is divisible by sum or not.
If it is divisible print yes,else print no.
#include<bits/stdc++.h>
using namespace std;
int main()
{
int n = 378;
int temp = n;
int sum = 0;
while (temp!=0) {
sum += temp % 10;
temp /= 10;
}
if (n % sum == 0) {
cout << "YES it is Harshad Number" << "\n";
}
else {
cout << "NO it is not Harshad Number" << "\n";
}
}
Example 2:
Input: 21
Output: Not Abundant Number
Explanation:Divisors of 21 are 1,3,7. 1+3+7=11, Since 11
is smaller than 21, 11 is not an abundant number.
#include<bits/stdc++.h>
using namespace std;
int main()
{
int n = 18;
int sum = 0;
for (int i = 1; i <= n; i++) {
if (n % i == 0) {
sum += i;
}
}
sum-=n;
if (sum > n) {
cout << "Abundant Number" << "\n";
}
else {
cout << "Not Abundant Number" << "\n";
}
Recursion
#include <iostream>
using namespace std;
int main()
{
cout<<print(5);
return 0;
}
Factorial of a number
#include <iostream>
using namespace std;
int main()
{
cout<<factorial(4);
return 0;
}
Power Function using recursion
#include<bits/stdc++.h>
using namespace std;
//Recursive Function
int power(int base, int x){
#include <iostream>
using namespace std;
int main()
{
int num = 123;
cout<<print(num);
}
Tower of Hanoi
Algorithm
Let us consider a recursive function that takes the
following argument N, the number of disks, to_Rod,
which indicates the rod which is moved to,
from_rod, denoting the rod from which rod is
removed, and aux_rod, denoting the rod which is
used for transferring rods from from_rod to to_rod.
The base case is for N = 1. Move it from source to
destination, i.e. from_rod to to_rod.
Solve the problem recursively by moving disk 1, 2,
3,…, N – 1 i.e. from_rod to aux_rod by recursively
calling the function on (N – 1, from_rod, aux_rod,
to_rod).
Now, since the top N – 1 disks have been removed
from from_rod, move the last disk from from_rod to
to_rod.
Similarly, again remove the top N – 1 disk from
aux_rod to to_rod and recursively call the function
on (N – 1, aux_rod, to_rod, from_rod)
Repeat the above steps until it reaches the base
case.