Question

In: Computer Science

//Java Language Read an integer number from the user. If the number is not positive, change...

//Java Language

Read an integer number from the user. If the number is not positive, change its sign.

  
1   Count the digits of the number
2   Count the odd digits of the number
3   Calculate the sum of the digit values of the number

Solutions

Expert Solution

Code:

import java.util.*;
class dig{
   public static void main(String args[]){
       Scanner s=new Scanner(System.in);
       System.out.print("Enter number:");
       int n=s.nextInt(),sum=0,od=0,t=0,r;
       if(n<0){
           n=-n;
       }
       while(n>0){
           r=n%10;
           sum=sum+r;
           t=t+1;
           if(r%2==1){
               od=od+1;
           }
           n=n/10;
       }
       System.out.println();
       System.out.println("Count of digits of number is "+t);
       System.out.println("Count of odd digits of number is "+od);
       System.out.println("Sum of digits of number is "+sum);
   }
}

Output:


Related Solutions

JAVA Language: Write a program that prompts the user to enter a positive integer n (0...
JAVA Language: Write a program that prompts the user to enter a positive integer n (0 up to 232 -1). You must write a function that takes as input n and returns a string s representing the number n in binary. For this assignment, you must use the method of successive division by 2 to convert the number to binary. Your main program must print out s. Example: If the user enters the number 66, your program must print out...
Write a MIPS assembly language program to read an arbitrary number of integer pairs from a...
Write a MIPS assembly language program to read an arbitrary number of integer pairs from a file. Each pair will then be multiplied together with the results being accumulated (added together) forming a sum-of-products operation. Submit your report and code here.
IN JAVA Write a MAIN METHOD that asks for user input of a positive integer and...
IN JAVA Write a MAIN METHOD that asks for user input of a positive integer and a negative integer validates the inputs using a loop and then calls the METHOD from Question 3 and prints the result. The MAIN should have two variables (appropriately typed), get the input from the user and store them in the variables after validating them, then call the method from question 3 (sending parameters, if necessary) and print the returned value with an appropriate descriptive...
C++ program that reads a positive integer number from a user and displays all binary numbers...
C++ program that reads a positive integer number from a user and displays all binary numbers from 0 to the number. For the program, you can assume that the input number is a number between 0 and 100. Sample Run 1: Assume that the user typed the following number. 5 This is the correct output of your program. 000 001 010 011 100 101 Sample Run 2: Assume that the user typed the following number. 0 This is the correct...
Write a Java program (name it LargestOccurenceCount) that reads from the user positive non-zero integer values,...
Write a Java program (name it LargestOccurenceCount) that reads from the user positive non-zero integer values, finds the largest value, and counts it occurrences. Assume that the input ends with number 0 (as sentinel value to stop the sentinel loop). The program should ignore any negative input and should continue to run. Hint: to remember/save entered (good) values, you can concatenate them into a string (separated by spaces) that you can output later on. Sample runs showing input prompts and...
A. Write a program 1. Prompt the user to enter a positive integer n and read...
A. Write a program 1. Prompt the user to enter a positive integer n and read in the input. 2. Print out n of Z shape of size n X n side by side which made up of *. B. Write a C++ program that 1. Prompt user to enter an odd integer and read in the value to n. 2. Terminate the program if n is not odd. 3. Print out a cross shape of size n X n...
Create a C++ program that will prompt the user to input an positive integer number and...
Create a C++ program that will prompt the user to input an positive integer number and output the corresponding number to words. Check all possible invalid input data. (Please use only switch or if-else statements. Thank you.)
Prompt the user to enter an integer Then, prompt the user to enter a positive integer...
Prompt the user to enter an integer Then, prompt the user to enter a positive integer n2. Print out all the numbers that are entered after the last occurrence of n1 and whether each one is even or odd If n1 does not occur or there are no values after the last occurrence of n1, print out the message as indicated in the sample runs below. Sample: Enter n1: -2 Enter n2: 7 Enter 7 values: -2 3 3 -2...
Java Programming Create a program that prompts the user for an integer number and searches for...
Java Programming Create a program that prompts the user for an integer number and searches for it within an array of 10 elements. What is the average number of comparisons required to find an element in the array? Your program should print the number of comparisons required to find the number or determine that the number does not exist. Try finding the first and last numbers stored in the array. Run your program several times before computing the average.
Write a java program that asks user to enter a set of positive integer values. When...
Write a java program that asks user to enter a set of positive integer values. When the user stops (think of sentinel value to stop), the program display the maximum value entered by the user. Your program should recognize if no value is entered without using counter.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT