You are on page 1of 6

Qs/ Scanner, While, do…while, for.

1. Write a java code to give the multiplication table for any integer number
from 1-10 using FOR loop only.
Answer:
package MultiplicationTable;
import java.util.Scanner;
public class MultiplicationTable {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int number, i, result;
System.out.println("Enter an integer number: ");
number = scan.nextInt();
for(i = 1; i <= 10; i++) {
result = i * number;
System.out.println(number + " * " + i + " = " + result);
}
}
}

2. update the code above to print all the multiplication table in arrange of 10
items per row.
Answer:
package MultiplicationTable;
import java.util.Scanner;
public class MultiplicationTable {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int number, i, result;
System.out.println("Enter an integer number: ");
number = scan.nextInt();
for(i = 1; i <= 10; i++) {
result = i * number;
System.out.println(number + " * " + i + " = " + result);
}
for(i = 1; i <= 10; i++){
for(int j = 1; j <= 10; j++){
System.out.print(i + " * " + j + " = " + (i*j) + " , ");
}
System.out.println();
}
}}

3. Give the output of the following:


public static void main(String[] args) {
int x=1, i=2;
do {
x*=i; }
while(++i<=5);
System.out.println(x);
}

Answer:
120
1
4. Give the output of the following program.
int i,j;
for (i=0; i<4; i++) {
for (j=i; j>=0; j--) {
System.out.print(j);
System.out.println();
}}

Answer:
0
10
210
3210

5. Convert following do-while loop into for loop.


int i=1;
int d=5;
do {
d=d*2
System.out.println(d);
i++;
}
while(i<=5);

Answer:
for(int i=1, d=5; i<=5; i++) {
d = d * 2;
System.out.println(d);
}

6. Convert the following while loop to the corresponding for loop


int m = 5, n = 10;
while (n>=1) {
System.out.println(m*n);
n–-;
}

Answer:
for(int m=5, n=10; n >=1; n--) {
System.out.println(m*n);
}

7. Give the output of following code:


int y,p;
for (int x=1; x<=3; x++) {
for (y=1; y<=2; y++) {
p = x * y;
System.out.print(p);
}
System.out.printin( ); }

2
Answer:
12
24
36

8. Give the output of following code:


public class test {
public static void main() {
int i=1,n=6,f=1;
while(i<=n) {
f=f*i; i++;
System.out.print(f+” “);
}
}
}

Answer:
1 2 6 24 120 720

9. Rewrite the program by using do-while loop;


Class Pattern {
public static void main(string.args[] {
int i,j;
for(i=5;i>=1;i--) {
for (j=1;j<=5;j++) {
System.out.println(i);
System.out.println();
}
}
}
}

Answer:
class Pattern {
public static void main(String args[]) {
int i=5,j;
do {
j=1;
do {
System.outprint(i);
j++; }
while(j<=5);
System.out.println();
i--;
}
while(i>=1);
}}

10.Write an equivalent while() loop for the following for() loop.


int s=0;
for(int x=1; x<=25; x+=2)
s+=x;

3
Answer:
int x=1,s=0;
while (x<=25){
s +=x;
x+=2;
}

11.Write an equivalent while() loop for the following for() loop.


for(int x=2, Sum=0;x<=30; x+=2)
Sum *=x;

Answer:
int x=2, Sum=0;
while(x<=30) {
Sum*=x;
x +=2;
}

12. Predict the output


int a,b;
for (a=1; a<=2; a++) {
for (b= (64+a); b<=70; b++) {
System.out.print((char) b);
System.out.println( );
}
}

Answer:

ABCDEF
BCDEF

13. Convert the following segment into an equivalent do loop.


int x,c;
for (x=10, c=20; c>=10; c = c – 2)
x++;

Answer:
int x=10,c=20;
do {
x++;
c=c-2;
}
while(c>=10);

4
14.Give the output of the following program segment and also mention how
many times the loop is executed:
int i;
for (i = 5 : i > 10; i ++) {
System.out.println(i);
System.out.println(i * 4);
}

Answer:
20

15.Convert following do-while loop into for loop.


int i = 1;
int d=5;
do {
d=d*2;
System.out.println(d;)
i++ ; }
while (i<=5);

Answer:
int i,d=5;
for(i=1;i<=5;i++) {
d=d*2;
System.out.println(d);
}

16. What will be the output of the following code?


public static void main(String[] args) {
int m=2, n=15;
for(int i = 1; i<5; i++);
m++; --n;
System.out.println("m = " + m);
System.out.println("n = " + n);
}
}
Answer:
m=3
n=14

17. What will be the output of the following code?


int x = 2, y = 50;
do {
++x;
y- = x++;
}
while(x <= 10);
return y;

Answer:
15

5
18. What is the final value of ctr after the iteration process given below,
executes?
int ctr = 0;
for (int i = 1 ; i < = 5 ; i++)
for (int j = 1 ; j < = 5 ; j+=2)
++ctr;

Answer:
15

You might also like