Skip to main content

Python print() Function

When you start learning Python, one of the first things you need to learn is how to display information on the screen. For example, you might want to display: A welcome message. Your name and age. The result of a mathematical calculation. Student marks and results. Information generated by a Python program. Python provides a built-in function called print() for displaying information. The print() function is one of the simplest and most frequently used functions in Python. It helps programmers display information while testing programs, solving problems, and developing applications. In this article, we will learn how to use print() with practical examples and their outputs. 1. What Is the print() Function in Python? The print() function is a built-in Python function that displays information in the output area, usually the terminal or console. Let's look at a simple example. print("Hello, World!") Output: Hello, World! In this example, Python displays the text Hello,...

Java Program to Count Digits in a Number

๐Ÿง  What Does "Counting Digits" Mean?

Counting digits means finding how many individual digits a number has.
For example:

  • The number 12345 has 5 digits.

  • The number 9 has 1 digit.

  • The number 0 has 1 digit.


๐Ÿ”น Method 1: Using While Loop (Division Method)

This method repeatedly divides the number by 10 until it becomes 0.
Each division removes one digit, and we count how many times this happens.

✅ Java Program

import java.util.Scanner;

public class CountDigitsLoop {
    public static void main(String[] args) {
        int number, count = 0;
        Scanner sc = new Scanner(System.in);

        System.out.print("Enter a number: ");
        number = sc.nextInt();

        int temp = number; // store the original number

        // handle zero separately
        if (number == 0) {
            count = 1;
        } else {
            // convert negative number to positive
            if (number < 0)
                number = -number;

            // count digits
            while (number > 0) {
                number = number / 10;
                count++;
            }
        }

        System.out.println("The number of digits in " + temp + " is: " + count);
        sc.close();
    }
}

๐Ÿงฉ How It Works

  1. Take a number from the user.

  2. If it’s negative, convert it to positive.

  3. Divide the number by 10 repeatedly until it becomes 0.

  4. Count the number of divisions — that’s the total digits.

๐Ÿงพ Example Output

Enter a number: 45217
The number of digits in 45217 is: 5
Enter a number: -98
The number of digits in -98 is: 2

๐Ÿ”น Method 2: Using String Conversion

This method is simpler and doesn’t use loops.
We just convert the number into a string and count how many characters it has.

✅ Java Program

import java.util.Scanner;

public class CountDigitsString {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        System.out.print("Enter a number: ");
        int number = sc.nextInt();

        // Convert to string and take absolute value for negatives
        String numStr = Integer.toString(Math.abs(number));

        // Length of the string = number of digits
        int count = numStr.length();

        System.out.println("The number of digits in " + number + " is: " + count);
        sc.close();
    }
}

๐Ÿงฉ How It Works

  1. The user enters a number.

  2. The number is converted to a string using Integer.toString().

  3. We find the length of that string using .length().

  4. The result gives the total number of digits.

๐Ÿงพ Example Output

Enter a number: 98765
The number of digits in 98765 is: 5
Enter a number: -123
The number of digits in -123 is: 3

⚖️ Comparison Between Both Methods

Feature Loop Method String Method
Logic Type Mathematical String-based
Uses Loop ✅ Yes ❌ No
Handles Negative Numbers ✅ Yes ✅ Yes
Code Length Medium Short
Best For Beginners learning loops Quick and clean code

๐Ÿ Conclusion

Both methods are correct — the loop method helps understand number operations, while the string method is more concise.

๐Ÿ‘‰ Use the loop method for learning and the string method for quick coding tasks.

Comments

Popular posts from this blog

Understanding Comments in Java: A Beginner’s Guide

When learning Java programming, one of the first concepts you’ll come across is comments . Comments may not affect how a program runs, but they play a crucial role in making code more understandable, readable, and maintainable. Let’s dive deeper into what comments are, why they’re important, and the different types of comments in Java. What are Comments in Java? A comment in Java is a piece of text written within a program that is ignored by the compiler . In other words, comments are notes for humans, not instructions for machines. They help developers explain the purpose of code, describe logic, or temporarily disable certain lines of code without deleting them. Why are Comments Important? ๐Ÿ“Œ Readability – Comments make code easier to understand for you and others. ๐Ÿ“Œ Maintenance – When you revisit your code after months, comments remind you what you were doing. ๐Ÿ“Œ Collaboration – In team projects, comments explain code to fellow developers. ๐Ÿ“Œ Debugging – You c...

Java Program to Check Voting Eligibility

In this program, we’ll take the user’s age as input and check whether the person is eligible to vote or not. In India, the minimum age required to vote is 18 years . ๐Ÿ’ป Program Code // Java Program to Check Voting Eligibility import java.util.Scanner; public class VotingEligibility { public static void main(String[] args) { // Create Scanner object to take input from user Scanner sc = new Scanner(System.in); // Ask user to enter age System.out.print("Enter your age: "); int age = sc.nextInt(); // Check eligibility if (age >= 18) { System.out.println("You are eligible to vote."); } else { System.out.println("You are not eligible to vote yet."); System.out.println("You can vote after " + (18 - age) + " year(s)."); } sc.close(); // Close the scanner } } ๐Ÿง  Explanation import java.util.Scanner; → Imp...

Java Program to Calculate Total Marks and Average of Five Subjects

๐Ÿ’ก Program Explanation In this program: The user will input marks of five subjects . The program will calculate the total marks and average . It will then display both results clearly. ๐Ÿง‘‍๐Ÿ’ป Java Code: // Java Program to Calculate Total Marks and Average of Five Subjects import java.util.Scanner; // Importing Scanner class for user input public class TotalAndAverage { public static void main(String[] args) { // Creating Scanner object to take input from user Scanner sc = new Scanner(System.in); // Declaring variables for five subjects double subject1, subject2, subject3, subject4, subject5; // Taking marks input for each subject System.out.print("Enter marks of Subject 1: "); subject1 = sc.nextDouble(); System.out.print("Enter marks of Subject 2: "); subject2 = sc.nextDouble(); System.out.print("Enter marks of Subject 3: "); subject3 = sc.nextDo...