0% found this document useful (0 votes)
19 views

Java Program-Predict The Output-Iteration - New

Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views

Java Program-Predict The Output-Iteration - New

Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

1.

Write the output of the following codes:


(a) int j = 0, result = 1;
for(j = 3; j < = 5; j++)
{
result = j;
for(g = 5; g >=3; g - -)
{
result = result * g;
}
System.out.print(“ \ n Result is “ + result);
}
System.out.print (“ \ n Out Of The Loop Value of (j,g) are “ + j + “ , “ + g);

(b) int cnt = 10;


while (cnt > = 1)
{
System.out.print(cnt - - + “ “)
cnt - = 2;
}

(c) int cnt = 10;


for (int j = 1; j < = cnt; j++)
{
System.out.print (j + “ “);
if ( j % 3 = = 0)
j++;
}

(d) int cnt = 10;


for ( int j = 1, k = 1; j < = cnt; j++, k++)
{
System.out.print (k + “ “ );
if ( j % 2 = = 0)
{
k = j + 1;
System.out.println( );
}
}

(e ) for (int j = 1; j < = cnt; j++)


{
System.out.print(j + “ \ t”);
if (k = = j && j ! = cnt)
{
j = 0;
k++;
System.out.println ( );
} }

(f) double dbl = 10.5;


for (double d = 1; d <= dbl; d+= 0.5 )
{
if (d % 2.5 == 0)
{
System.out.print (d + “ \ t”);
} }
(g) double db2 = 123.45;
int p = 0;
double d1 = db2;
while (db2 – (int) db2 > 0)
{
db2 = db2 * 10;
p - -;
System.out.println (“ ## “ + db2);
}
System.out.println (“ \n Finally :” + db1
+ “ is \ t” + db2 + “ x 10^”
+ p);

(h) int b = 56478;


int b2 = b;
int r = 0;
while (b2 ! = 0)
{
System.out.println (b2 + “ \t” + r);
r = r * 10 + (b2 % 10 );
b2 = b2 / 10;
}

2. In the following code snippets, identify if there is any error or not:


a) double d1 = 19.2, d2 = 42.5;
int t1 = d1;
System.out.print (“ d1 = “ + d1 + “ \t and t1 =” + t1);

b) boolean pm= true;


int n = 77;
for (int j = 2; j < = n/2; j++)
{
if (n % j = = 0)
pm = false;
break;
}
c) for(int j = 1; j < = n; j++)
{
int c = 0;
if(n % j = = 0)
c++;
}
d) System.out.print(“Enter a char.....”);
vh = sca.next Line( ).charAt (0);
System.out.print(“vh =” + vh);

e) //input a,b
d = a/b;
r = a%b;
System.out.print(“Quotient =” +d+ “Remainder =” +r );

You might also like