// WAP TO TAKE A NUMBER AS AN INPUT AND FIND ITS MULTIPLICATION TABLE. // 6 /* 6 X 1 = 6 6 X 2 = 12 */ import java.util.Scanner; // for importing the scanner class class multiplicationTable{ public static void main(String[] args) { // creation of an object Scanner sco = new Scanner(System.in); System.out.println("Enter a number:"); int num = sco.nextInt(); int result; System.out.println("The Multiplication table is = "); for(int i = 1;i <= 10;++i) { result = num * i; System.out.println(num+" X "+i+" = "+result); } } } ============================================ // WAP TO PRINT ALL MULTIPLICATION TABLES FOR GIVEN RANGE OF NUMBERS. // RANGE ==> 1 TO 10 // 1 TABLE, 2 TABLE, 3 TABLE, .... 10 TABLES // Nested loops // outer loop ==> for defining the range of numbers // inner loop ==> for multiplication table import java.util.Scanner; class MultiplicationTables{ public static void main(String[] args) { Scanner obj = new Scanner(System.in); System.out.println("Enter the range for defining the multiplication tables:"); int n1 = obj.nextInt(); int n2 = obj.nextInt(); // n1 < n2 System.out.println("All Multiplication Tables are:"); for(int i = n1;i <= n2;i++) { int result = 0; for(int j = 1;j <= 10;++j) { result = i * j; System.out.println(i + " X " + j + " = " + result); } } } } ================================================ // WAP TO PRINT ALL ARMSTRONG NUMBERS FROM THE RANGE 1000 TO 9999. // WAP TO PRINT ALL FOUR DIGIT ARMSTRONG NUMBERS. // 1634, 8208, and 9474 // we need nested loops // outer loop for ==> iterating over 4-digit numbers // inner loop for ==> checking of armstrong number. class PrintingArmStrongNumbers{ public static void main(String args[]) { // outer loop for iterating over the 4-digit numbers. for(int i1 = 1000;i1 <= 9999;i1 += 1) { // inner loop for checking of armstrong numbers int i2 = i1; // initialization int ind_dig; int powers; int sum_dig = 0; while(i2 != 0) // condition { ind_dig = i2 % 10; // remainder 1234 % 10 => 4 powers = ind_dig * ind_dig * ind_dig * ind_dig; sum_dig += powers; i2 = i2 / 10; // quotient 1234 / 10 ==> 123 update } if(sum_dig == i1) { System.out.print(i1+"\t"); } } System.out.println(); } } ========================================================= break statement: ================ ==> to stop the iteration/loop immediately, we can use the break statement. ==> break is the keyword. Syntax: break; // WAP TO CHECK WHETHER THE GIVEN NUMBER IS PRIME NUMBER OR NOT. // Prime number ==> the number must be have factors 1 and itself only // 2 ==> smallest prime number 1, 2 // 3, 5, 7, 11 etc. // 6 ==> 1, 2, 3, 6 (factors) // divide the given number from 2 to till the given number // or divide that given number from 2 to given number/2 // 66 ==> 1, 2, 3, 6, 11, 22, 33, 66 // 80 ==> 1, 2, 4, 5, 8, 10, 16, 20, 40, 80 import java.util.Scanner; class primeNumber{ public static void main(String[] args) { Scanner obj = new Scanner(System.in); System.out.println("Enter a number to check whether it is prime number or not:"); int num = obj.nextInt(); int i,m = 0, f = 0; m = num/2; if(num == 0 || num == 1) { System.out.println("The Given number is not a prime number."); } else { for(i = 2;i <= m;++i) { if(num % i == 0) { System.out.println("The given number is not a prime number."); f = 1; break; } } } if(f == 0) { System.out.println("The Given number is Prime number."); } } } ========================================= // understanding of break class BreakStatement{ public static void main(String[] args) { for(int i = 1;i <= 10;i++){ if(i == 7) { break; } System.out.println(i); } } } Assignment: ========== 1) WAP IN JAVA TO PRINT ALL MULTILICATION TABLE FOR THE GIVEN RANGE OF VALUES IN HORIZONTAL. 2) WAP IN JAVA TO PRINT ALL PRIME NUMBERS FROM THE GIVEN RANGE. (101 TO 200)