Posts

21002

Write a loop that reads positive  integers   from standard input, printing out those  values   that are  greater than   100, each followed by a space, and that terminates when it reads an  integer   that is not positive.  Declare   any  variables   that are needed. ASSUME   the availability of a  variable  ,  stdin , that  references   a  Scanner   object   associated with standard input.  int number = 0; while (stdin.hasNextInt() && number >= 0){ number =stdin.nextInt(); if (number > 100) System.out.print(number + " "); }

21000

Given   an  int     variable    n  that has  already   been  declared  , write some code that repeatedly reads a  value   into  n   until   at last a number  between   1 and 10 (inclusive) has been entered. ASSUME   the availability of a  variable  ,  stdin , that  references   a  Scanner   object   associated with standard input. do { n = stdin.nextInt(); } while (n > 10 || n < 1);

20681

Given   an  int     variable    n  that has been  initialized   to a positive  value   and, in addition,  int     variables    k  and  total  that have  already   been  declared  , use a  while loop    to compute the  sum    of the  cubes  of the first  n  whole numbers, and  store   this  value   in  total . Use no  variables   other than  n ,  k , and  total  and do not modify the  value   of  n . total=0; k=0; do{     total+=k*k*k;     k++; } while (k<=n);

20680

Given    int     variables    k  and  total  that have  already   been  declared  , use a  while  loop   to compute the  sum    of the  squares  of the first 50 counting numbers, and  store   this  value   in  total . Thus your code should put 1*1 + 2*2 + 3*3 +... + 49*49 + 50*50 into  total . Use no  variables   other than  k  and  total . total=0; k=1;   do{     total += k*k;     k++; }while (k<=50);

20689

Write a  statement    that  increases    the  value   of the  int     variable    total  by the  value   of the  int     variable    amount . That is, add the  value   of  amount  to  total  and  assign   the result to  total . total = total + amount;

20679

Given   an  int     variable    n  that has  already   been  declared   and  initialized   to a positive  value  , use a  while  loop   to  print  a single line consisting of  n  asterisks. Use no  variables   other than  n . while (n != 0){    System.out.print("*");    n = n - 1; }

20678

Given   an  int     variable    k  that has  already   been  declared  , use a  while  loop   to  print  a single line consisting of  88  asterisks. Use no  variables   other than  k . k = 0; while (k < 88){    System.out.print("*");    k = k + 1; }