Professional Documents
Culture Documents
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();
}
}}
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
Answer:
for(int i=1, d=5; i<=5; i++) {
d = d * 2;
System.out.println(d);
}
Answer:
for(int m=5, n=10; n >=1; n--) {
System.out.println(m*n);
}
2
Answer:
12
24
36
Answer:
1 2 6 24 120 720
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);
}}
3
Answer:
int x=1,s=0;
while (x<=25){
s +=x;
x+=2;
}
Answer:
int x=2, Sum=0;
while(x<=30) {
Sum*=x;
x +=2;
}
Answer:
ABCDEF
BCDEF
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
Answer:
int i,d=5;
for(i=1;i<=5;i++) {
d=d*2;
System.out.println(d);
}
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