Hi!
I can not solve this JAVA problem.
I tried to write:
final int a=100;

int nbr=Input.readInt();// from keyboard

I try to find (nbr*nbr)>a

ex: nbr=11 and 11*11=121
12*12=144
10*10=100

Output must be 121, not 144 but 144 is also >100
thanks
Peter

Dani AI

Generated

Short answer: if the goal is "the smallest integer n whose square is greater than a", compute it directly with the square‑root instead of trying lots of keyboard inputs. For non‑negative a the formula is

n = floor(sqrt(a)) + 1

so you only need one computation, not a stream of candidate inputs. This also explains the behavior saw: a loop that prints the first user‑entered n whose square passes the test will naturally print whichever qualifying value the user typed first, not the mathematically smallest one.

A safe, practical Java implementation uses Math.sqrt and then adjusts the candidate to guard against floating‑point rounding:

import java.util.Scanner;

public class SmallestN {
  static long smallestN(long a) {
    if (a < 0) return 1; // choose 1 if you require positive n; use 0 if n>=0 is allowed
    long r = (long) Math.sqrt(a);
    while (r * r <= a) r++;     // fixes any rounding that left r too small
    while ((r - 1) * (r - 1) > a) r--; // defensive: ensure minimality
    return r;
  }

  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    long a = sc.nextLong();
    System.out.println(smallestN(a));
    sc.close();
  }
}

Notes and pitfalls: use long if a can be large (avoid int overflow). The r*r checks are safe here because r is at most floor(sqrt(a)), so r*r fits in long. For numbers beyond Long.MAX_VALUE or if exact large‑integer math is required, compute the integer square root with a BigInteger binary search. ’s flag/compare idea will work in limited interactive situations, but the sqrt approach is simpler, faster, and less error‑prone.

Recommended Answers

All 8 Replies

what exactly are you trying to do ... you can use simple if statements to check whether nbr is greater than zero or not.

Hi again!
The Question is:
I try to write a program which find and print out

The smallest  n and  (n*n) must be greater than a special number.
Ex: n = 11 is the smallest  and (n*n) is 121 and is greater than 100.
But  n = 12 is also smallest  and (n*n) is 144 and is greater than 100.
The program must print out 11 not 12
If we write The number = 500 the out put must be 23 not 24 or 25 ...
I mean    n=22 and(n*n=484)
               n=23 and (n*n =529) I want this one.
               n=24 and(n*n=576)

Code

class Test{

  public static void main(String[]args){
      final int number=100;
      System.out.println("write n:");
        int n;
    do{

       n  = Input.readInt();


     }while(n*n<= number);


     System.out.println(n);

    }
}
// I want 11 not 12 or 13 ....I changed the program many times but every time the out put is not that "n" which I want.

Thanks

Peter

Hi!
please help me whit this question.
Thanks
Peter

I dont understand your question ... why dont you want to print 12 and you want to print 11 ??? both are greater than 100.

Hi again!


Yes,both (11,and 12,13) are greater than 100. But the question is that the program must print out the first greater n, not the second or third greater n.
Again:
we have a nbr "nbr=3000"
we have n=54 //from keyboard
n=55 //from keyboard
n=56 //from keyboard

when we multiply n we get:
54x54=2916
55x55=3025
56x56=3136
the program must print 55 because it is the first greater n, we don not need that the program prints out the second greater n(56).
Regards
peter

okay I give you a raw idea about your program ... if still you cant understand ... post again...

input n1 from keyboard
check if its square is greater than nbr
if yes put flag1 = n1
if no put flag1 = 0

input n2 from keyboard
check if its square is greater than nbr
if yes put flag2 = n2
if no put flag2 = 0

input n3 from keyboard
check if its square is greater than nbr
if yes put flag3 = n3
if no put flag3 = 0

compare flag1, flag2 and flag3 for the highest number
eliminate the highest
compare the other two left for the highest number
eliminate the highest

the last number left is what should be printed.

Hi!
Yes yuor solution is OK, but if we have nbr=100;
and in the sametime our input are
13, 14, 15, or 50,70,91.....and if we compare these(n) we can not
find 11. the smallest one is not 11.
I mean if we have a nbr=12548796821
how many times we test each n.
Ex: the nbr 100 is between (10x10) and (11x11)
and we take the greater one.
thanks
Peter

owww ! I misunderstood you ... okay let me try n then I'll post the solution for you

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.