finding maximum value from a given bits [closed] - java

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
// a program tells the maximum value we can store in (unsigned integer) a given bits .( 256 for 8 bits)
int counter=0;
int last= 0b11111111;
for(int first=0b00000000;first<=last;counter++)
{
first=first + 1;//adding 1(binary addition)
}
System.out.println("for "+ variable "bits u can store "+counter values");
//variable here 8.
//(1.how to get it from user? 2.how to convert it into binary 0b00000000?)
//how to do this without 0b ,actually in previous version of java
//a program in which if you give 8 bit(in case of unsigned) then it give u maximum values u can store in it, not by using ((2*n)-1).
//code is not only for java 8
// sorry i do not have java 8 i hope the above code will compile without error
thank you in advance

Try this:
long result = 1 << numBits;
If numBits is greater than the size of long, use a double instead (and cast the "1" and "numBits" to double).

how about this
int last = 0;
for(int i = 0; i < bitNum; i++){
last = (last << 1) + 1
}

Scanner s = new Scanner(System.in);
int variable = s.nextInt();
int counter = 0;
long last = (1 << variable) - 1;// = 0b'111....111
for(int first = 0; i <= last; counter++){
first = first + 1;
}
System.out.println("for "+ variable +"bits u can store " + counter + "values");
this code is same with your code, but this code dont use 0b.

Related

How can I find minimum element of an integer in Java? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 months ago.
Improve this question
Imagine we are getting input from user as integer. I want my code to return minimum and maximum numbers in this integer value. For example, if user enters 56389, the code should display
Minimum number: 3
Maximum number: 9
If user enters single digit integer, let's say 7, the output should be:
Minimum number: 7
Maximum number: 7
I am trying to declare input as String instead of integer and compare all elements of the String with charAt(i) method. However, I cannot get the result.
I would be glad if you can help me!
This should work:
int number = Integer.valueOf(input);
int largest = 0;
int smallest = 9;
while(number != 0)
{
int rem = number % 10;
largest = Math.max(rem, largest);
smallest = Math.min(rem, smallest);
number = number / 10;
}
System.out.println(largest + " " + smallest);
You can parse the string input using wrapper classes util methods. The rest of the code you can refer below.
int[] getLargestAndSmallestDigits(String in){
int n = Integer.valueOf(in);
if (n==0) {
return new int[]{0,0};
}
int[] ans = {0,9};
while(n != 0)
{
int r = n % 10;
ans[0] = Math.min(r, ans[0]);
ans[1] = Math.max(r, ans[0]);
n = n / 10;
}
return ans;
}
If you choose to work with the number representing it as a string, that means you'll have to be using the charAt(int index) method to retrieve an individual character from the string.
Then, to convert any number character (that is, '0', '1', '2' ... '9') to the number itself, subtract character '0' from it ('0' will be explicitly converted to int (ASCII code) at runtime), and you'll get the number itself in int representation.
public static void main(String[] args) {
char charNine = '9';
int intNine = charNine - '0';
System.out.println(intNine);
}
The output is 9.

java program to loop and output 2,5,7,10,12 [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 months ago.
Improve this question
Loop
how to loop this without using array? I'm clearly confuse in this in java module I don't know the solution ;-; I'm new to java pls helppp mee
Try this.
for (int i = 5; i <= 25; i += 5)
System.out.println(i / 2);
Or
for (int i = 1; i <= 5; i++)
System.out.println(i * 5 / 2);
output:
2
5
7
10
12
Think for yourself why this produces the correct result.
public class Main {
public static void main(String[] args) {
int outputNum = 0;
for(int i = 0; i < 5; i++){
if (i % 2 == 0){
outputNum += 2;
}
else {
outputNum += 3;
}
System.out.print(outputNum + (i == 4 ? "" : " "));
}
}
}
This does exactly what you need.
Explanation:
In the loop if the i variable is even we add 2 to the output number (since you need to add 2 then 3 then 2...) and if i is uneven then we add three. At the end of the for loop we print the number without the newline using System.out.print and we separate them by a space if we aren't printing the last number using a ternary operator (i == 4 ? "" : " ") which returns a space if i is not equal to 4 and an empty string if it is, we do this to avoid having a space on the end of our output, so we get this 2 5 7 10 12 instead of this 2 5 7 10 12

How to display a line of numbers equally in java [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
Im having trouble only displaying the correct numbers, I know how to get the formula is have count-1 have 10/2 = 5 but now I want to count up from the start making it 10 15 20.
//Static void dispalySpace(double start, double end, int count){
// Display numbers between start and end inclusively
//The numbers are spaced equally
//Assume start < end and count is at least 2.
//displaySpace(10,20,3)
//The answer to the question is 10.0 15.0 20.0
while(min<max && count>=2)
for(int i=min; i<=max; i++) {
for(double j = count-1; j>=2; j++) {
System.out.print(j + " ");
}
System.out.print(i + " ");
}
System.out.println();
}
I keep just getting 10-20 displaying on a loop.
You first need to calculate the steep size, ie what do you have to add to min so that you end up with count numbers. For the first output, you won't have to add anything, so it's one less than count , ie
double step = (max-min) / count - 1;
That's what you have to add each step of the loop, starting with min , so
for (double i = 0; i <= max ; i = i + step) {
System.out.print(i) ;
}

Error Incompatible types in java [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
This is my code but it gives me this error
class MultiplyDivide {
public static void main (String args[]) {
int i = 5;
int j = 10;
System.out.println("5 is " + i);
System.out.println("10 is " + j);
int k = i/j;
System.out.println("5/10 is " + k);
k = i * j;
System.out.println("5 * 10 is " + k);
}
}
Hi,
Actually the code that you posted should not give you an error.
It will return an output like:
5 is 5
10 is 10
5/10 is 0
5 * 10 is 50
One point that can be an error from your point of view is the 5/10 equals zero. But it is a correct java behavior because you are dividing integer by integer and assign a result to an integer.
If you want to get a double-type result you need to do something like:
double k = i * 1.0 / j;
Hope this resolves your issue.

Write a program that returns number of decimal digits in the given input string [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
Write a program that returns number of decimal digits in the given input string.
ex :
Input: 2345abc423
output: 7
This is my code:
Scanner input = new Scanner(System.in);
String s = input.nextLine();
int len = s.length();
int numbers = 0 ;
for(int i=0; i < len; i++){
char c = s.charAt(i);
int num = (int) c;
while(num <= 57 && num >= 48){
System.out.println(numbers);
numbers++;
}
}
System.out.print(numbers);
/////
sorry because my question wasn't obvious
my program always return 0 , not the number of decimal digits
thanks #Keppil i tried if and it worked
thanks
Your program will never exit the while loop since the condition never changes. You should just use an if instead:
if (num <= 57 && num >= 48) {
System.out.println(numbers);
numbers++;
}
Solution that abuses regex:
System.out.println(input.replaceAll("\\D", "").length());

Categories