1.) Suppose you have an int variable and you want to know how many digits are in
ID: 3628720 • Letter: 1
Question
1.) Suppose you have an int variable and you want to know how many digits are in it. You recall that the String class a length() method that you could use if you had a String representation of your int. Write code to print the number of digits. (You might also consider a way of doing this without the help of the String class.)2.) You require one more than a two-thirds majority of a population to vote for you in order to win an election. Suppose the number of voters is stored in an int variable named numVoters. How many of numVoters do you need? Write a variable assignment that calculates the number of whole voters you'd need. For example, if numVoters is 100, one more than a 2/3 majority is 67. Test with different numbers of voters.
Explanation / Answer
please rate - thanks
NOTE: CRAMSTER rule-1 question per post
didn't specify the language, but length is JAVA
import java.util.*;
public class Main
{public static void main(String[] args)
{int num,saved,i=0;
System.out.print("enter an integer: ");
Scanner in=new Scanner(System.in);
num=in.nextInt();
saved=num;
while(num!=0)
{num/=10;
i++;
}
System.out.println(saved+" has "+i+" digits");
}
}
------------------------
import java.util.*;
public class Main
{public static void main(String[] args)
{int numVoters,need;
System.out.print("enter the number of voters: ");
Scanner in=new Scanner(System.in);
numVoters=in.nextInt();
need=(int)(numVoters*2./3.+1);
System.out.println(need+" voters needed");
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.