DEV Community

Jasraj Chouhan
Jasraj Chouhan

Posted on

Print Digits of a number Beginners Coding Problem [Day-01]

Hello dear my fellow friends today we discussed the how the print digits of a number in reaverse order.

Example or test cases

input : 1234
output : 4 3 2 1

input : 2390
output : 0 9 3 2

Approch

1.we take the modula of the number by 10 then we get always last digit of a number.

for example

num = 123
divide the num by 10
num % 10 = 3

num = 4562
num % 10 = 2

it means we clear about this concept if we required last digit of number then we take the modula of number by 10 and get the last digit .

  1. In second we required next digit so we reduced the num by num/10

for example
num = 123
num % 10 = 3

num = num / 10
num = 123 / 10
num = 12

ok then our num is reduced from 123 to 12

  1. We repeat both the step until num is not zero.

ok let's move for coding section I used c, cpp, java and python language if you comming from another programming language so you can convert this code into your progamming language.

java

import java.util.Scanner;
class HelloWorld {
    public static void main(String[] args) {
        System.out.println("Enter a number");
        Scanner sc = new Scanner(System.in) ;
        int n = sc.nextInt();       //take input from user

        printDigits(n) ;        // call the printDigits function which print the all digits of a number in reverse order


    }

    public static void printDigits(int n) {

        while(n != 0) {
            System.out.print(n % 10 + " ");
            n /= 10;
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

C


#include <stdio.h>

void printDigits(int n) {
    while(n != 0) {
        printf("%d ", n % 10);
        n /= 10;
    }
}

int main() {
    printf("Enter a number: ");
    int n;
    scanf("%d", &n);

    printDigits(n);

    return 0;
}


Enter fullscreen mode Exit fullscreen mode

cpp

#include <iostream>

void printDigits(int n) {
    while(n != 0) {
        std::cout << n % 10 << " ";
        n /= 10;
    }
}

int main() {
    std::cout << "Enter a number: ";
    int n;
    std::cin >> n;

    printDigits(n);

    return 0;
}

Enter fullscreen mode Exit fullscreen mode

python

def print_digits(n):
    while n != 0:
        print(n % 10, end=" ")
        n //= 10

n = int(input("Enter a number: "))
print_digits(n)

Enter fullscreen mode Exit fullscreen mode

javascript

function printDigits(n) {
    while (n !== 0) {
        process.stdout.write(n % 10 + " ");
        n = Math.floor(n / 10);
    }
}

console.log("Enter a number:");
let n = parseInt(prompt());
printDigits(n);

Enter fullscreen mode Exit fullscreen mode

if you have any doubt related to this question please comment your doubt.

if you are beggerner in coding please follow this series we covered complete dsa.

dsa #programming #daydsa Sure, here you go:

codingtips #programmerlife #techblog #learntocode #codegoals #codenewbie #algorithm #datastructures #pythonprogramming #javascript #webdevelopment #cplusplus #java #computerscience #developercommunity #codersofinstagram #codingisfun #programminghumor #codingbootcamp #codingjourney #codechef #hackerrank #codeforces #leetcode #interviewprep #softwareengineering #codingchallenge #100daysofcode #programmingquotes #codequality #debugging #codeart #codinglife #programmingmemes #womenintech #programminglanguage #codingproblems #codinginterview #frontenddevelopment #backenddevelopment #fullstackdeveloper #codebug #techcommunity #devlife #codingtutorial #codingislove #codingschool #programmingtips #opensource #programmingbooks #codingclub #codewars #programminglogic #codeaddict #codingworld #programmingthoughts #codingislife #programmerrepublic #codingforbeginners #codingwisdom #codingiscreativity #programmingeducation #codingisart #codingfromscratch #codingisawesome #programminglife #codingstudio #programming101 #codinginspiration #codered #codingmindset #programmingfun #codingisbeautiful #codingiscool #codingworld #codingtherapy #programmingjourney #codeyourfuture #codingexplained

Top comments (0)