C++ Program to Display Integers in Expanded Form

  In this article, we will create a C++ program to display Integers in Expanded form.


Program to display Integers in expanded form
Program to display integers in expanded form



The following header files are used in this program - 
  • iostream - For input or output stream.


The following user-defined functions are created - 

  • lengthofinteger(int n) - This function is used to calculate the length of the integer. This function takes an integer (the no. to be expanded) as a parameter and returns its length.
  • breakdown(int n,int len, int split[]) - This function is used to split the integer into digits and store them in an array at the position corresponding to its power of 10.


Code -

//Program to write Integers in Expanded form by Vishruth Codes
#include<iostream>
using namespace std;

//Function to calculate the length of the integer
int lengthofinteger(int n)
{
    int count=0;
    while(n>0)
    {
        n=n/10;
        count++;
    }
    return count;
}

//Function to split the integer into digits and store them in array at position corresponding to its power of 10.
//Example: 12345 is split into its digits 5,4,3,2,1 (*read the function) and stored an array named 'split' (split[]={5,4,3,2,1})
//  The position of each element is in accordance with power of 10 in the expanded form i.e.
//  Take the element 5 in the above example, it's position in the array is '0' (split[0])
//  which is corresponding to it's position 'ones' (...x10 + 5x1) in the expanded form of the integer 12345 (as 5x10^0=5x1).
void breakdown(int n,int len, int split[])
{
    int rem;
    for(int i=0;i<len;i++)
    {
        rem=n%10;
        if(n>0)
        {
        n=n/10;
        split[i]=rem;
        }
    }
}

int main()
{
    int n,len;
    //Input
    cout<<"\nEnter the number: ";
    cin>>n;
    len=lengthofinteger(n);
    cout<<"\nThe length of integer is: "<<len;
    int split[len];
    breakdown(n,len,&split[0]);

    //Output
    cout<<"\n\nInteger "<<n<<" expanded form is: ";
    for(int i=len-1;i>=0;i--)
    {
        int mul=1;
        for(int j=0;j<i;j++)
        {
            mul=mul*10;
        }
        if(i!=0)
        {
        cout<<split[i]<<"x"<<mul<<" + ";
        }
        else{
            cout<<split[i]<<"x"<<mul;
        }
    }
    return 0;
}


Subject - 

In the above example, I have implemented a C++ program to display integers in expanded form using the concept of functions.


Basic features - 
  • Works for integers of all possible sizes.
  • Measures the length of the given integer.


Working - 

  1. The user inputs a number that he/she/they wants to expand.
  2. To determine the length of the given integer, the function lengthofinteger(int n) is called by the main() by passing the given number as an argument, which measures the length based on the no. of times the given integer was able to be divisible by 10 until the quotient result turned out to be zero, and it returns that count.
  3. After measuring the length, the function breakdown(n,len,&split[0]) is called by the main() by passing the given number, the length of that number, and an array 'split' (the former two via pass by value technique whereas the latter via pass by address). This function will split the integer into digits and store them in an array y at the position corresponding to its power of 10. 
  4. For example: 12345 is split into its digits 5,4,3,2,1 (*read the function) and stored an array named 'split' (split[]={5,4,3,2,1}). The position of each element is in accordance with power of 10 in the expanded form i.e. Take the element 5 in the above example, it's position in the array is '0' (split[0]) which is corresponding to it's position 'ones' (...x10 + 5x1) in the expanded form of the integer 12345 (as 5x10^0=5x1).
  5. We came up with a logic in which, using the array 'split' and the order in which the elements are stored within it, we will be able to deduce the elements' corresponding power of 10, and display it in expanded form.


Output - 

Program to write integers in expanded form - output
Sample Output



Thanks for reading. If you found any errors or have suggestions to improve the article, please mention them in the comments.

Post a Comment

0 Comments