Question
please answer all qustions use this input(6587)
Problem Description: Consider afour digit number corresponding to number of letters in your full name. Input this number in y
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Here is the solution to your problem, if you are satisfied with the answer please give an upvote, else do ask your doubts in the comments.

Main.java B saved 1 import java.util.Scanner; 2 class Main // a) static int countdigit(int n) int i=1; while(n/10/0) n/=10; i

Main.java B saved 28 // c) static int reversedigit(int n) int rn=0; while(n>0) rn=rn*10+n%10; n/=10; return rn; // d) static

saved Main.java B int cnt=0; while(n>0) if((n%10)%2==1) cnt==1; n/=10; return cnt; public static void main(String[] args) Sca

import java.util.Scanner;

class Main

{

// a)

static int countdigit(int n)

{

int i=1;

while(n/10>0)

{

n/=10;

i+=1;

}

return i;

}

// b)

static int sumdigit(int n)

{

int sum=0;

while(n>0)

{

sum+=n%10;

n/=10;

}

return sum;

}

// c)

static int reversedigit(int n)

{

int rn=0;

while(n>0)

{

rn=rn*10+n%10;

n/=10;

}

return rn;

}

// d)

static int odddigit(int n)

{

int cnt=0;

while(n>0)

{

if((n%10)%2==1)

cnt+=1;

n/=10;

}

return cnt;

}

public static void main(String[] args)

{

Scanner sc=new Scanner(System.in);

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

int n=sc.nextInt();

System.out.print(n+" contains "+countdigit(n)+" digit.\n");

System.out.print(n+" of digits of "+sumdigit(n)+" is %d.\n");

System.out.print("Reverse of "+n+" is "+reversedigit(n)+".\n");

System.out.print("count of odd digits of "+n+" is "+odddigit(n)+" digit.\n");

}

}

> javac -classpath .:/run_dir/junit-4.12.jar:target/dependency/* -d. Main.java > java -classpath .:/run dir/junit-4.12.jar:ta
1. Algorithms

Algorithm countdigit(n):

1. set i=1, (as the lowest amount of digits count of any number)

2. while n/10>0, repeat steps 3 and 4

3. n=n/10

4. i=i+1

5 return i

Algorithm sumdigit(n):

1. set sum=0, (as the lowest sum of digits of any number)

2. while n>0, repeat steps 3 and 4

3. sum=sum+n%10

4. n=n/10

5 return sum

Algorithm reversedigit(n):

1. set rn=0

2. while n>0, repeat steps 3 and 4

3. rn=rn*10+n%10

4. n=n/10

5 return rn

Algorithm odddigit(n):

1. set cnt=0

2. while n>0, repeat steps 3 and 4

3. if (n%10)%2==1, then cnt=cnt+1

4. n=n/10

5 return cnt

The complexity of all function is O(logn).

Add a comment
Know the answer?
Add Answer to:
please answer all qustions use this input(6587) Problem Description: Consider afour digit number corresponding to number...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a C program that counts the number of each digit entered by the user (Input...

    Write a C program that counts the number of each digit entered by the user (Input process should be ended using EOF character). You should use switch to compute the number of each digits. For each digit your program should print a line containing that number of adjacent asterisks. Sample Input1: 18218342850056D855599 Sample Outputi: 0** 3° 5*** 6° 7 g*** Sample Input: 77777445521056 Sample Output2: 0* 1 2° 3 45 S. 6° 8 9 Sample Output 3: Sample Input3: 782222231567799988001332092555...

  • Description: An ISBN-10 (International Standard Book Number) consists of 10 digits: didzdzdad5d6d7d8d9d1o. The last digit, dio,...

    Description: An ISBN-10 (International Standard Book Number) consists of 10 digits: didzdzdad5d6d7d8d9d1o. The last digit, dio, is a checksum, which is calculated from the other nine digits using the following formula: (d, x 1 + d2 x 2 +d3 x 3 + da x4 + ds x 5 + de x 6 + d7 x 7+ d3 x 8+dex 9) % 11 If the checksum is 10, the last digit is denoted as X according to the ISBN-10 convention. Write...

  • This is Python The program should accept input from the user as either 7-digit phone number...

    This is Python The program should accept input from the user as either 7-digit phone number or 10-digit. If the user enters 7 characters, the program should automatically add "512" to the beginning of the phone number as the default area code. Dash (hyphen) characters do not count in input character count, but must not be random. If the user enters dashes the total character count must not exceed 12. The program should not crash if the user enters invalid...

  • Description For this program, you are going to convert decimal (integer) numbers into their octal number...

    Description For this program, you are going to convert decimal (integer) numbers into their octal number (integer) equivalents. Make sure that you create a new Project and Java class file for this assignment. Your Repl.It file should be named “Main.java”. You can read about octal-to-decimal number conversions from wikepedia or another website Instructions The input to the program will be a single non-negative integer number. If the number is less than or equal to 2097151, convert the number to its...

  • Write a C program which calculates how many digits a number has and prints each digit...

    Write a C program which calculates how many digits a number has and prints each digit in reserve order with space in between(no space after the last digit). The number > 0 and has no more than 5 digits. (optional] It is better to write two functions. One is designed for printing number in reverse order and the other is for counting the number of digits. Given that the value of number (which is an int variable) is 12345, the...

  • Please write c++ (windows) in simple way and show all the steps with the required output

    please write c++ (windows) in simple way and show all the steps with the required output Write a C++ program that simulates the operation of a simple online banking system The program starts by displaying its main menu as shown in Figure1 splay Account nformatson verity Your credit Card elect vour choice Figure 1 Main menu of the program The menu is composed of the following choices 1. When choice 1 is selected (Display Account information), the program should display...

  • Please I need help with this c++ code. please show all steps , write comments and...

    Please I need help with this c++ code. please show all steps , write comments and show sample runs. Thank you. 1. The Federal Bureau of Investigation (FBI) has recently changed its Universal Control Numbers (UCN) for identifying individuals who are in the FBI's fingerprint database to an eight-digit base 27 value with a ninth check digit. The digits used are: 0123456789ACDE FHJKLMNPRTVWX Some letters are not used because of possible confusion with other digits: B->8, G->C, I- >1, 0->0,...

  • build a phone number from digits entered by your user. Your program will loop until 10...

    build a phone number from digits entered by your user. Your program will loop until 10 valid digits have been entered. It will then use those digits to display the phone number entered using the format: XXXXXXXXXX (or (XXX) XXX – XXXX for extra credit). The program will ask for a digit, it will check to see if the digit is actually between 0 and 9 inclusively. If so, the digit will be stored as the next number in the...

  • PROGRAMMING IN C. 1) The first program will compute compounded interest. Suppose you invest $1000.00 at...

    PROGRAMMING IN C. 1) The first program will compute compounded interest. Suppose you invest $1000.00 at an interest rate of 2% per year. If the interest is computed at the end of each day, it is added to your account (i.e., the interest is compounded daily). Print what the account value will be at the end of each year for 20 years. Use data type double for money. For 365 days per year (ignore leap year), the daily interest rate...

  • Banks issue credit cards with 16 digit numbers. If you've never thought about it before you...

    Banks issue credit cards with 16 digit numbers. If you've never thought about it before you may not realize it, but there are specific rules for what those numbers can be. For example, the first few digits of the number tell you what kind of card it is - all Visa cards start with 4, MasterCard numbers start with 51 through 55, American Express starts with 34 or 37, etc. Automated systems can use this number to tell which company...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT