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 6 years ago.
Improve this question
I want to write a java program to find the largest square number which is less than or equal to a given number.
For example when I insert 10 as the inputted number, the answer is 9 because 9is the largest square up to 10
I understand loops, but I can't figure out the logic behind this one.
How about something like the following to get you going ...
double x = 20;
double root = Math.sqrt(x);
int t = (int)root;
System.out.println("Root is:" + root + " and answer is :" + t*t);
Here is how to do this with loops
int n=10; //This is your Number
int i = 0;
for(i=n;i>=1;i--)
if((int)Math.sqrt(i)*(int)Math.sqrt(i)==i)
break;
System.out.println(i);
Below is how it works:-
The Loop runs from the n, which is your number, to 1. It then checks whether, the square root of i, which is running through n to 1, is a perfect square. If it is, it breaks the loop, and prints the value of i on the screen.
public static void main(String argv[]){
System.out.println(largestSquareLessOrEqualTo(145));
}
public static int largestSquareLessOrEqualTo(int limit){
int i = 0;
for (; i <(int)Math.sqrt(limit); i++){
}
return(i*i);
}
Related
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 2 years ago.
Improve this question
Example:
Input:
140
Output:
1 4 0
I wanted to make it divide by 100 so that the outcome will be 1, and then divide by 10 and the outcome will be 4 and then by 1 and the answer will be 0. But I am not sure how I am able to achieve it. I also want to use recursion in the method.
You can see an int as a String
int n = 140;
String s = String.valueOf(n);
for(int i = 0; i<s.length(); i++){
System.out.println(s.charAt(i));
}
With no need of recursion.
With recursion it could be something like (i haven't tried it so it could not work):
public String separateInteger(int n){
if(n < 10){
return String.valueOf(n);
}
else{
int mod = n%10;
int quot = n/10;
return String.valueOf(mod) + separateInteger(quot);
}
}
I hope have answered your question. :)
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 2 years ago.
Improve this question
I am learning Java, at a very beginning level. I am trying to print the Dice result, so from 1 to 6.
//instance of random class
Random DICE = new Random();
int DiceRange = 7;
int RIGHT = DICE.nextInt(DiceRange);
int LEFT = DICE.nextInt(DiceRange);
System.out.println("Right Dice = " + RIGHT);
System.out.println("Left Dice = " + LEFT);
The issue with this code that "Zero" is also printed sometimes. I want to keep the range from 1 to 6, instead of 0 to 6.
I tried to do the following, but did not work:
int i = 0;
while (DiceRange == 0); {
i++;
}
The CPU went to 100% :)
So how to exclude zero from this?
From Random documentation:
public int nextInt(int bound)
Returns a pseudorandom, uniformly distributed int value between 0
(inclusive) and the specified value (exclusive)
So starting from your code you can solve your problem using :
Random DICE = new Random();
int DiceRange = 6;
int RIGHT = DICE.nextInt(DiceRange) + 1;
int LEFT = DICE.nextInt(DiceRange) + 1;
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
So I was trying to make the following program:
The user gives two integers as an input (first and last). The program is supposed to give the sum between those two numbers, but when I run the program I don't get an output. However, when I put for the first input a bigger integer than the last input, I get the first input as result. This is my code:
public class TheSumBetweenTwoNumbers {
public static void main(String[] args) {
Scanner reader = new Scanner(System.in);
System.out.println("First: ");
int first = Integer.parseInt(reader.nextLine());
System.out.println("Last: ");
int last = Integer.parseInt(reader.nextLine());
int sum = 0;
while (first <= last); {
sum += first;
first++;
}
System.out.println("The sum is: " + sum);
}
}
your while loop runs without making a change to the block that it contains. you have closed the while loop before it even enters the block.
while (first <= last) {
sum += first;
first++;
}
try this
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 5 years ago.
Improve this question
Taylor series for sine and cosineI'll post my code here:
public double MiSeno(long n, int t) //n es x, t es en
{
double s = 0, x;
int sig = 1;
for(int i = 1; i < t; i++)
{
x = Math.pow(n,i) / Factorial(i) * sig;
sig *= -1;
s += -x;
}
return s;
}
I know im doing it wrong for now but im getting confused.
Sine Series has power and factorial moving by two i.e. odd numbers, so your for loop should be:
for(int i = 1; i < t; i+=2)
Couple of things:
Make sure you type cast output of Factorial to double to get precise float value.
Make sure your integer doesn't overflow by factorial calculation too.
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
Can someone explain to me how to store the previous two fibbonnaci numbers it would help alot in this problem.
public static void main(String[] args) {
int k = 0;
for (int x = 1; x < 13; x++) {
if (k > 2) {
k = (k - 1) + (k - 2);
}
System.out.print(k+" ");
k++;
}
}
when you got number 5 as the printed out put you will set k++ , that will make k=6.
after that k = (k - 1) + (k - 2); output k = (6-1)+(6-2) = 5+4 = 9 , (note : the next should be 8 so your algorithm is wrong)
You have mistaken the Idea of Fibonacci numbers.
the nth Fibonacci number is equal to the sum of previous two Fibonacci numbers. not to the (Fn-1)+(Fn-2)
Edited :
So as you can see if we know the first 2 Fibonacci numbers we can calculate the third by adding those two. and the fourth one will be the summation of second one and third one and it goes ..... to n.
Okay here is a way that you don't need a recursive approach ( you need to store the found Fibonacci numbers in an Array)
okay assume you want to find first n Fibonacci numbers. then create an array of size n and set first and second elements to one (1) since first two Fibonacci numbers are 1 and 1. now loop through the array from 2 to n. at each iteration add the previous two element to the next element.
go through the code. you will find it very easy to do.
public static void fib(int n){
int Fibonacci [] = new int[n];
Fibonacci [0]=1;
Fibonacci [1]=1;
for (int i = 2; i < n; i++) {
Fibonacci [i]=Fibonacci [i-1]+Fibonacci [i-2];
}
System.out.println(Arrays.toString(Fibonacci ));
}