Program to form a number using entered digits

Description

To form number using entered digit, first declare a variable
assign 0 to it. Next add to this variable by multiplying to 10th
power of index of incoming number.

Example: If entered digit is 1, 2, 3
then,
for 1, index=0 then number = 0 + 1*(10, 0) = 1
for 2, index=1 then number = 1 + 2*(10, 1) = 21
for 3, index=2 then number = 21 + 3*(10, 2) = 321

C/C++

/* C program to print number formed using entered digits*/
//Save it as NumberUsingInputDigit.c

#include<stdio.h>
int main(){

    int i, n;

    printf("Total number of digit you want to enter : ");
    scanf("%d",&n);

    int arr[n];

    for (i = 0; i < n; i++) {
        printf("\nEnter the %d digit : ",(i+1));
        scanf("%d",&arr[i]);
    }

    double num = 0;
    
    
    //Syntax of pow function -
    //double pow(double x, double y)
    
    
    
    /*
    To find the power of int or a float variable,explicitly convert the type
    to double using cast operator.
    */
    
    
    for(i=0;i<n;i++) {
        num = num + (arr[i] * pow(10,(double)i));
    }
    
    /*
    Finally typecast the num to get result in integral value
    */
    
    printf("\nThe number formed is : %d", (int)num);

    return 0;
}
Input:
Total number of digit you want to enter : 
5
Enter the 1 digit : 
2
Enter the 2 digit : 
5
Enter the 3 digit : 
8
Enter the 4 digit : 
7
Enter the 5 digit : 
2

Output:
The number formed is : 27852

Java

/*Java program to print number formed using entered digits*/
//Save it as NumberUsingInputDigit.java

import java.io.*;
import java.util.Scanner;

public class NumberUsingInputDigit {

    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);

        System.out.println("Total number of digit you want to enter : ");
        int n = scanner.nextInt();

        int arr[] = new int[n];

        for (int i = 0; i < n; i++) {
            System.out.println("Enter the "+(i+1) +" digit : ");
            arr[i] = scanner.nextInt();
        }
        
        int num = 0;
        
        for (int i = 0; i < n; i++) {
            num = (int) (num + arr[i] * Math.pow(10,i));
        }
        
        System.out.println("The number formed is : " + num);
    }
}
Input:
Total number of digit you want to enter : 
5
Enter the 1 digit : 
2
Enter the 2 digit : 
5
Enter the 3 digit : 
8
Enter the 4 digit : 
7
Enter the 5 digit : 
2

Output:
The number formed is : 27852

Related Programs

1) Program for finding Second Largest Element Of Array
2) Program to swap maximum and minimum element of Array
3) Program to Find the smallest missing number
4) Program to Find K largest elements from array
5) Program to find maximum and second maximum if elements of array is space separated input
6) Program to make pair of elements alternatively
7) Remove Duplicate Elements From Array
8) Program to Read and Display entered numbers using an Array
9) Program to merge two sorted arrays
10) Program to read and display 2D array
Share Me

Leave a Reply