Computer Science, asked by alexa9680, 4 months ago

Write a program in Java to input a number and count the number of digits. The program further checks

whether the number contains odd number of digits or even number of digits.​

Answers

Answered by anindyaadhikari13
6

Required Answer:-

Question:

  • Write a program in Java to input a number and count the number of digits. The program further checks whether the number contains odd number of digits or even number of digits.

Approach:

This is a very easy question. Check out below for the program.

package com.student;

import java.util.*;

public class NumberOfDigits

{

public static void main(String s[])

{

/*

* Answered by: @AnindyaAdhikari.

*/

Scanner sc = new Scanner(System.in);

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

int n = sc.nextInt();

int c = 0; //for storing the number of digits.

while(n!=0)

{

c++;

n/=10;

}

System.out.println("Number of Digits: "+ c);

if(c%2==0)

System.out.println("Number has even number of digits.");

else

System.out.println("Number has odd number of digits.");

sc.close();

}

}

How to Solve?

In this program, we have to count the number of digits and also, we have to check whether the number of digits of the number is even or odd. So, at first, we will import the scanner class. Scanner class allows us to take input from the keyboard. It is present in utility package of Java. Now, I have created a class, main() method. Inside the method, I have created objects of Scanner class. Now, ask the user to take the number as input from the keyboard. A loop is created that will iterate until the number becomes zero by repeated division by 10. Each time, the number is divided by 10 and the value of c is increased. After iteration of loop is completed, we have found the total number of digits of the entered number and it's stored in variable c. Now, we will display message using println() statement. If the number of digits is even, then a message is displayed that the number of digits of the number is even. The same thing happens in case of odd number of digits.

Variable Description:

  1. n (int):- To store the entered number.
  2. c (int):- To store the number of digits of the number (n).

Output is attached. Try to run the program multiple times with different values of n.

Attachments:
Answered by BrainlyProgrammer
2

Question:

  • To input and count the no. of digits
  • To check if no. contains even no. of digits or odd no. of digits

_______________________

Answer:

import java.util.*;

class Code

{

public static void main(String ar [])

{

Scanner sc=new Scanner (System.in);

System.out.print("Enter a no.:-");

int n=sc.nextInt();

c=0;

while (n!=0)

{

c++;

n/=10;

} //loop ends

System.out.println("No. of digits in a number:"+c);

if(c%2==0)

System.out.println("Even no. of digits");

else

System.out.println("Odd no. of digits");

} //end of main

} //end of class

___________________

Sample Input:-

Enter a no.:-1011023

__________________

Sample Output:-

No. of digits in a number:7

Odd no. of digits

Similar questions