C++ Program to Check for Armstrong Number

C++ Program to Check for Armstrong Number

  • Write a program in C++ to check armstrong number.

C++ program to check armstrong number

C++ program to check armstrong number

#include <iostream>
 
using namespace std;
  
int main(){
    int number, sum = 0, lastDigit, temp;
    cout << "Enter a Number\n";
    cin >> number;
    temp = number;
    // Calculate sum of cube of every digit
    while(temp != 0){
        lastDigit = temp%10;
        sum = sum + (lastDigit*lastDigit*lastDigit);
        temp = temp/10;
    }
      
    if(sum == number){
        cout << number <<" is Armstrong Number";
    } else {
        cout << number <<" is not an Armstrong Number";
    }
     
    return 0;
}

Output

Enter a Number
153
153 is Armstrong Number
Enter a Number
120
120 is not an Armstrong Number

C++ Program to check Whether a Number is Palindrome or Not

In the previous article, we have discussed C++ Program to print ASCII Value of All Alphabets. In this article, we will see C++ Program to check Whether a Number is Palindrome or Not.

C++ Program to check Whether a Number is Palindrome or Not

  • Write a C++ program to check whether a number is palindrome or not.

Before jumping to a C++ program to check whether a number is palindrome or not, here is a brief overview of palindrome numbers.

A number is palindrome, if number remains same after reversing it’s digits.

For Example:
1234321 is palindrome number, but 123456 is not a palindrome number.

To check whether N is palindrome number or not, first of all we have to reverse the sequence of digits of N and then compare it with original N. If both are equal then N is palindrome number.

Algorithm to check a number is palindrome or not

  • Take a number as input from user and store it in an integer variable(Let’s call it inputNumber).
  • Reverse the digits of inputNumber, and store it in another integer variable(Let’s call it reverseNumber).
  • Compare inputNumber and reverseNumber.
  • If both are equal then inputNumber is palindrome number otherwise not a palindrome number.

C++ program to check palindrome number

C++ program to check palindrome number

#include <iostream>
 
using namespace std;
  
int main(){
    int inputNumber, reverseNumber = 0, rightDigit, temp;
    cout << "Enter a number\n";
    cin >> inputNumber;
     
    temp = inputNumber;
    // reverse inputNumber and store it in reverseNumber
    while(temp != 0){
        rightDigit = temp % 10;
        reverseNumber = (reverseNumber * 10) + rightDigit;
        temp = temp/10;
    }
    // If input Number and reverse Number are same 
 // then palindrome othrwise not
    if(reverseNumber == inputNumber){
        cout << inputNumber << " is Palindrome Number";
    } else {
        cout << inputNumber << " is not a Palindrome Number";
    }
      
    return 0;
}

Output

Enter a number
1234321
1234321 is Palindrome Number
Enter a number
123456
123456 is not a Palindrome Number

In above program, we first take a number as input from user using cin and store it in variable original. Copy the value of original variable to another variable copy. Then using a while loop, we reverse the sequence of digits of a number. Finally, we compare reverse and original number. If both are equal then input number is palindrome otherwise not a palindrome number.

If you are a newbie to the programming languages and want to explore all Example C++ Program for acquiring the best coding skills. Have a look at our C++ Programs Tutorial.

C++ Program to Convert Decimal Number to Octal Number

In the previous article, we have discussed C++ Program to Convert Decimal Number to Binary Number. In this article, we will see C++ Program to Convert Decimal Number to Octal Number.

C++ Program to Convert Decimal Number to Octal Number

  • Write a C++ program to convert decimal to octal number.
  • Write a C++ program to convert octal to decimal number.

In below C++ programs we will learn about fundamentals of decimal and octal number system, how to convert decimal numbers to octal numbers and vice-versa. Given a decimal and an octal number we have to convert it to octal and decimal numbers respectively.

Decimal number system is a base 10 number system using digits for 0 to 9 and Octal number system is base 8 number system and uses 0 and 7.

For Example
100 in decimal number is equivalent to 144 in octal number system.

C++ program to convert a decimal to octal number

Algorithm to convert decimal to octal number

  • Divide the input decimal number by 8 and store the remainder.
  • Store the quotient back to the input number variable.
  • Repeat this process till quotient becomes zero.
  • Equivalent octal number will be the remainders in above process in reverse order.

For Example:

Suppose input decimal number is 500
Step 1. 500/8 , Remainder = 4, Quotient = 62
Step 2. 62/8 , Remainder = 6, Quotient = 7
Step 3. 7/8 , Remainder = 7, Quotient = 0
Now, the Octal equivalent of 500 is the remainders in reverse order : 764

C++ program to convert a decimal to octal number

// C++ program to convert decimal numbers to octal numbers
#include <iostream>
using namespace std;
  
long decimalToOctal(long n);
int main() {
    long decimal;
    cout << "Enter a decimal number\n";
    cin >> decimal;
    cout << "Octal number : " << decimalToOctal(decimal);
      
    return 0;
}
  
// Function to convert a decinal number to octal number
long decimalToOctal(long n) {
    int remainder; 
    long octal = 0, i = 1;
   
    while(n != 0) {
        remainder = n%8;
        n = n/8;
        octal = octal + (remainder*i);
        i = i*10;
    }
    return octal;
}

Output

Enter a decimal number
1234
Octal number : 2322

In above C++ program, we first take an integer as input from user and store it in variable decimal. Then we call decimalToOctal function to convert decimal function to octal number by implementing above mentioned algorithm.

C++ Program to Convert Octal Number to Decimal Number

Algorithm to convert octal to decimal number

  • We multiply each octal digit with 8^i and add them, where i is the position of the octal digit(starting from 0) from right side. Least significant digit is at position 0.

Let’s convert 1212(octal number) to decimal number
Decimal number = 1*8^3 + 2*8^2 + 1*8^1 + 2*8^0 = 512 + 128 + 8 + 2 = 650

C++ Program to Convert Octal Number to Decimal Number

// C++ program to convert octal numbers to decimal numbers
#include <iostream>
#include <cmath>
using namespace std;
  
long octalToDecimal(long n);
int main() {
    long octal;
    cout << "Enter an octal number\n";
    cin >> octal;
    cout << "Decimal number : " << octalToDecimal(octal);
      
    return 0;
}
  
// Function to convert a octal number to decimal number
long octalToDecimal(long n) {
 int remainder; 
    long decimal = 0, i=0;
    while(n != 0) {
        remainder = n%10;
        n = n/10;
        decimal = decimal + (remainder*pow(8,i));
        ++i;
    }
    return decimal;
}

Output

Enter an octal number
2322
Decimal number : 1234

In above program, we first take an octal number as input using cin and store it in a long variable octal. Then we call octalToDecimal function by passing octal variable as parameter to convert octal number to decimal number by implementing above mentioned algorithm.

Coding in C++ Programming language helps beginners & expert coders to use the classes for various concepts programs. So, look at the C++ Code Examples of all topics and write a logic with ease.

C++ Program to Find GCD of Two Numbers

In the previous article, we have discussed C++ Program to Display Fibonacci Series using Loop and Recursion. In this article, we will see C++ Program to Find GCD of Two Numbers.

C++ Program to Find GCD of Two Numbers

  • Write a C++ program to find GCD of two numbers using loop.
  • Write a C++ program to calculate HCF of two numbers.

C++ program to find GCD of two numbers

C++ program to find GCD of two numbers

#include <iostream>
 
using namespace std;  
  
int getMinimum(int a, int b){
    if(a >= b)
        return b;
    else
        return a;
}
  
int main() {  
    int a, b, min, i, gcd = 1;  
    /* 
     * Take two numbers as input from user  
     */
    cout << "Enter Two Integers\n";  
    cin >> a >> b;  
    
    min = getMinimum(a, b);
    // Search for larget number less than min which divides both a and b
    for(i = 1; i <= min; i++) {  
        if(a%i==0 && b%i==0) {
            /* Update GCD to new larger value */
            gcd = i;  
        }  
    }  
    
    cout << "GCD of " << a << " and " << b << " is " << gcd; 
    return 0;  
}

Output

Enter Two Integers
30 12
GCD of 30 and 12 is 6

C++ program to find GCD of two numbers by repetative subtracting

C++ program to find GCD of two numbers by repetative subtracting

#include <iostream>
 
using namespace std;
 
int main() {
    int num1, num2;
    cout << "Enter Two Integers\n";
    cin >> num1 >> num2;
     
    while(num1 != num2) {
        if(num1 > num2)
            num1 = num1 - num2;
        else
            num2 = num2 - num1;
    }
 
    cout << "GCD is " << num1;
     
    return 0;
}

Output

Enter Two Integers
22 8
GCD is 2

Practicing with CPP Programs aid you to master coding skills and learn all basic & advanced concepts in the object-oriented programming language C++.

C++ Program to Print Multiplication Table of a Number

In the previous article, we have discussed C++ Program Linear Search in Array. In this article, we will see C++ Program to Print Multiplication Table of a Number.

C++ Program to Print Multiplication Table of a Number

  • Write a C++ program to print multiplication table of a number.

To print multiplication table, we first take a number n and term(t) as input from user. We will use a for loop to print the multiplication table of n till t times.

C++ Program to Generate Multiplication Table

C++ Program to Generate Multiplication Table

#include <iostream>
 
using namespace std;
 
int main() {
    int n, term, i;
     
    cout << "Enter a number to print multiplication table\n";
    cin >> n;
    cout << "Enter number of terms in table\n";
    cin >> term;
    /* Generate multiplication table */
    for(i = 1; i <= term; ++i){
        cout << n << " X " << i << " = " << n*i << endl;
    }
      
    return 0;
}

Output

Enter a number to print multiplication table
6
Enter number of terms in table
9
6 X 1 = 6
6 X 2 = 12
6 X 3 = 18
6 X 4 = 24
6 X 5 = 30
6 X 6 = 36
6 X 7 = 42
6 X 8 = 48
6 X 9 = 54

Are you fed up with browsing all concepts related to C++ Programming Programs? Not anymore because all the basic and complex Programs for C++ are covered on our main page just tap on the link available here & save your time to practice more.

C++ Program to Find Sum of Natural Numbers Using Recursion

In the previous article, we have discussed C++ program to Check Whether a Number can be Split into Sum of Two Prime Numbers. In this article, we will see C++ Program to Find Sum of Natural Numbers Using Recursion.

C++ Program to Find Sum of Natural Numbers Using Recursion

  • How to find sum of natural numbers using recursion in C++.
  • Write a C++ program to find sum of all natural numbers using recursion.

C++ program to calculate sum of all natural numbers using recursion

C++ program to calculate sum of all natural numbers using recursion

#include<iostream>
 
using namespace std;
 
int getSum(int N);
int main() {
    int n;
    cout << "Enter an Integer\n";
    cin >> n;
     
    cout << "Sum of Numbers from 1 to " << n << " = " << getSum(n);
    return 0;
}
 
int getSum(int N) {
    if(N >= 1)
        return N + getSum(N-1);
    else
        return 0;
}

Output

Enter an Integer
10
Sum of Numbers from 1 to 10 = 55

Learn and explore basic C++ Practice Programs that help to enhance your skills & knowledge of the programming language. Get to know how to write simple and clear code on this page.

C++ program to Check Whether a Number can be Split into Sum of Two Prime Numbers

In the previous article, we have discussed C++ Program to Check Prime Number Using User-defined Function. In this article, we will see C++ program to Check Whether a Number can be Split into Sum of Two Prime Numbers.

C++ program to Check Whether a Number can be Split into Sum of Two Prime Numbers

  • How to split a number into two prime numbers.
  • Write a C++ program to split a number in two prime numbers.

C++ program to Check Whether a Number can be Express as Sum of Two Prime Numbers

C++ program to Check Whether a Number can be Express as Sum of Two Prime Numbers

#include<iostream>
 
using namespace std;
  
int isPrime(int num);
 
int main() {
  int num, i;
  cout << "Enter a positive number\n";
  cin >> num;
  for(i = 2; i <= num/2; i++){
      if(isPrime(i)){
          if(isPrime(num-i)){
         cout << num << " = " << i << " + " << num-i;
              return 0; 
          } 
      }
  }
   
  cout << "Not Possible";
        
  return 0;
}
 
// returns 1 if num is prime number otherwise 0
int isPrime(int num){
    int i;
    // Check whether num is divisible by any 
    // number between 2 to (num/2)
    for(i = 2; i <=(num/2); ++i) {
        if(num%i==0) {
            return 0;
        }
    }
    return 1;
}

Output

Enter a positive number
24
24 = 5 + 19
Enter a positive number
27
Not Possible

Explore various CPP Codes lists that are used in C++ programming to write a sample program. These codes play an important role while learning the programs.

C++ Program to Check Prime Number Using User-defined Function

In the previous article, we have discussed C++ Program to Display Armstrong Number Between Two Intervals. In this article, we will see C++ Program to Check Prime Number Using User-defined Function.

C++ Program to Check Prime Number Using User-defined Function

  • C++ Program to Check Prime Number Using User-defined Function

C++ Program to check prime numbers using function

C++ Program to check prime numbers using function 1

#include<iostream>
 
using namespace std;
  
int isPrime(int num);
 
int main() {
  int num;
  cout << "Enter a positive number\n";
  cin >> num;
    
  if(isPrime(num))
      cout << num << " is a Prime Number",num);
  else
      cout << num <<" is NOT a Prime Number",num);
        
  return 0;
}
 
// returns 1 if num is prime number otherwise 0
int isPrime(int num){
    int i;
    // Check whether num is divisible by any 
    // number between 2 to (num/2)
    for(i = 2; i <=(num/2); ++i) {
        if(num%i==0) {
            return 0;
        }
    }
    return 1;
}

Output

Enter a positive number
23
23 is a Prime Number
Enter a positive number
50
50 is NOT a Prime Number

We know that C++ is an object oriented programming language. The advanced C++ Topics are enumerated constants, multi-dimensional arrays, character arrays, structures, reading and writing files and many more. Check the complete details of all those from this article.

C++ Program to Display Factors of a Number

C++ Program to Display Factors of a Number

  • Write a program in C++ to print factors of a number using loop.

C++ Program to find all positive factors of a number

C++ Program to Display Factors of a Number

#include <iostream>
 
using namespace std;  
    
int main() {  
    int i, N;   
     
    cout << "Enter a Number\n";  
    cin >> N;  
    
    cout << "Factors of " << N << endl;  
      
    // Check for every number between 1 to N, 
    // whether it divides N 
    for(i = 1; i <= N; i++) {   
        if(N%i == 0) {  
            cout << i << " ";  
        }  
    }  
    
    return 0;  
}

Output

Enter a Number
50
Factors of 50
1 2 5 10 25 50

C++ Program to Display Armstrong Number Between Two Intervals

In the previous article, we have discussed C++ Program to Get the List of all Files in a Given Directory and its Sub-Directories. In this article, we will see C++ Program to Display Armstrong Number Between Two Intervals.

C++ Program to Display Armstrong Number Between Two Intervals

  • Write a C++ program to print all armstrong numbers between two intervals.

In this C++ program, we will find all armstrong numbers between two given integers. Here is a brief introduction of armstrong number:

An Armstrong number is a number whose sum of cubes of every digit of a number is equal to the number itself.
For Example:

407 is an Armstrong number
407 = 4*4*4 + 0*0*0 + 7*7*7

Algorithm to check for Armstrong Number

  • Take a number as input from user and store it in an integer variable(Let’s call it inputNumber).
  • Find the cubic sum of digits of inputNumber, and store it in sum variable.
  • Compare inputNumber and sum.
  • If both are equal then input number is Armstrong number otherwise not an Armstrong number.

In this program, we will take two two integers as input from user and then print all armstrong numbers between given two integers. Here is the C++ program to print all armstrong number between given interval.

C++ program to print all armstrong numbers between two integers

C++ program to print all armstrong numbers between two integers

// C++ Program to Print Armstrong Number Between Two Intervals
#include <iostream>
using namespace std;
  
/*
 * Funtion to calculate the sum of cubes of digits of a number
 * getCubicSumOfDigits(123) = 1*1*1 + 2*2*2 + 3*3*3;
 */
int getCubicSumOfDigits(int number){
    int lastDigit, sum = 0;
    while(number != 0){
        lastDigit = number%10;
        sum = sum + lastDigit*lastDigit*lastDigit;
        number = number/10;
    }
    return sum;
}
 
int main(){
    int x, y, sum, i;
     
    cout << "Enter two integers\n";
    cin >> x >> y;
     
 cout << "Armstrong numbers between " << x <<" and "<< y << endl;
    // Iterate from x till y, and check for Armstrong number
    for(i = x; i <= y; i++){
        sum = getCubicSumOfDigits(i);
        if(sum == i){
            cout << i << endl;
        }
    }
 
    return 0;
}

Output

Enter two integers
200 500
Armstrong numbers between 200 to 500
370
371
407

In above program, we first take two numbers as input from user and store it in variable x and y. Using a for loop, we iterate from x till y and check for each number whether it is armstrong number or not.

We have defined a function “getCubicSumOfDigits”, which takes an integer parameter as input and then returns the sum of cubes of digits of a number. Inside getCubicSumOfDigits function, we extract digits of number one by one add the cube of the digit to a variable sum.

For Example:

getCubicSumOfDigits(123) = 1*1*1 + 2*2*2 + 3*3*3 = 36.

Finally, we compare the cubic sum of digits of a number with the number itself. If both are equal than number is an armstromg number otherwise not an armstromg number.

Write simple C++ programs to print a message, swap numbers, and many more to get an idea on how the program works. Here given C++ Programs for Practice are useful to master your coding skills and learn various topics.