// Problem1 java code
import java.util.Scanner;
// Java code to print all possible strings of letter L and R
class Problem1 {
static void print(char set[], int length) {
int setLength = set.length;
printRecursion(set, "", setLength, length);
}
static void printRecursion(char set[], String prefixset, int setLength, int length) {
// Base case: length is 0
if (length == 0) {
System.out.println(prefixset);
return;
}
for (int i = 0; i < setLength; ++i) {
String newPrefixset = prefixset + set[i];
printRecursion(set, newPrefixset, setLength, length - 1);
}
}
public static void main(String[] args) {
Scanner scan=new Scanner(System.in);
System.out.println("Enter length: ");
int length = scan.nextInt();
char set[] = {'L', 'R'};
print(set, length);
}
}
/*
output:
Enter length:
3
LLL
LLR
LRL
LRR
RLL
RLR
RRL
RRR
*/
// Problem2 java code
import java.util.Scanner;
// Java code to print all possible strings of letter L and R
class Problem2 {
static void print(char set[], int length) {
int setLength = set.length;
printRecursion(set, "", setLength, length);
}
static void printRecursion(char set[], String prefixset, int setLength, int length) {
// Base case: length is 0
if (length == 0) {
System.out.print(prefixset + " ");
return;
}
for (int i = 0; i < setLength; ++i) {
String newPrefixset = prefixset + set[i];
printRecursion(set, newPrefixset, setLength, length - 1);
}
}
public static void main(String[] args) {
Scanner scan=new Scanner(System.in);
System.out.println("Enter length: ");
int length = scan.nextInt();
char set[] = {'1', '3', '5', '7', '9'};
print(set, length);
System.out.println();
}
}
/*
output:
Enter length:
3
111 113 115 117 119 131 133 135 137 139 151 153 155
157 159 171 173 175 177 179 191 193 195 197 199 311
313 315 317 319 331 333 335 337 339 351 353 355 357
359 371 373 375 377 379 391 393 395 397 399 511 513
515 517 519 531 533 535 537 539 551 553 555 557 559
571 573 575 577 579 591 593 595 597 599 711 713 715
717 719 731 733 735 737 739 751 753 755 757 759 771
773 775 777 779 791 793 795 797 799 911 913 915 917
919 931 933 935 937 939 951 953 955 957 959 971 973
975 977 979 991 993 995 997 999
*/
// Problem3 java code
import java.util.Scanner;
// Java code to multiply 2 numbers
class Problem3 {
public static int multiply(int a, int b)
{
int temp = b;
for (int i = 1; i < a; i++ ) {
b = b + temp;
}
return b;
}
public static void main(String[] args) {
Scanner scan=new Scanner(System.in);
System.out.println("Enter a: ");
int a = scan.nextInt();
System.out.println("Enter b: ");
int b = scan.nextInt();
System.out.println(a + "X" + b + " = " + multiply(a,b));
}
}
/*
output:
Enter a:
7
Enter b:
6
7X6 = 42
*/
// Problem4 java code
import java.util.Scanner;
// Java code to find gcd 2 numbers
class Problem4 {
public static int gcd(int a, int b)
{
if (b!=0)
return gcd(b, a%b);
else
return a;
}
public static void main(String[] args) {
Scanner scan=new Scanner(System.in);
System.out.println("Enter a: ");
int a = scan.nextInt();
System.out.println("Enter b: ");
int b = scan.nextInt();
System.out.println("gcd(" + a + "," + b + ") = " + gcd(a,b));
}
}
/*
output:
Enter a:
10
Enter b:
12
gcd(10,12) = 2
*/
Solution
// Problem1 java code
import java.util.Scanner;
// Java code to print all possible strings of letter L and R
class Problem1 {
static void print(char set[], int length) {
int setLength = set.length;
printRecursion(set, "", setLength, length);
}
static void printRecursion(char set[], String prefixset, int setLength, int length) {
// Base case: length is 0
if (length == 0) {
System.out.println(prefixset);
return;
}
for (int i = 0; i < setLength; ++i) {
String newPrefixset = prefixset + set[i];
printRecursion(set, newPrefixset, setLength, length - 1);
}
}
public static void main(String[] args) {
Scanner scan=new Scanner(System.in);
System.out.println("Enter length: ");
int length = scan.nextInt();
char set[] = {'L', 'R'};
print(set, length);
}
}
/*
output:
Enter length:
3
LLL
LLR
LRL
LRR
RLL
RLR
RRL
RRR
*/
// Problem2 java code
import java.util.Scanner;
// Java code to print all possible strings of letter L and R
class Problem2 {
static void print(char set[], int length) {
int setLength = set.length;
printRecursion(set, "", setLength, length);
}
static void printRecursion(char set[], String prefixset, int setLength, int length) {
// Base case: length is 0
if (length == 0) {
System.out.print(prefixset + " ");
return;
}
for (int i = 0; i < setLength; ++i) {
String newPrefixset = prefixset + set[i];
printRecursion(set, newPrefixset, setLength, length - 1);
}
}
public static void main(String[] args) {
Scanner scan=new Scanner(System.in);
System.out.println("Enter length: ");
int length = scan.nextInt();
char set[] = {'1', '3', '5', '7', '9'};
print(set, length);
System.out.println();
}
}
/*
output:
Enter length:
3
111 113 115 117 119 131 133 135 137 139 151 153 155
157 159 171 173 175 177 179 191 193 195 197 199 311
313 315 317 319 331 333 335 337 339 351 353 355 357
359 371 373 375 377 379 391 393 395 397 399 511 513
515 517 519 531 533 535 537 539 551 553 555 557 559
571 573 575 577 579 591 593 595 597 599 711 713 715
717 719 731 733 735 737 739 751 753 755 757 759 771
773 775 777 779 791 793 795 797 799 911 913 915 917
919 931 933 935 937 939 951 953 955 957 959 971 973
975 977 979 991 993 995 997 999
*/
// Problem3 java code
import java.util.Scanner;
// Java code to multiply 2 numbers
class Problem3 {
public static int multiply(int a, int b)
{
int temp = b;
for (int i = 1; i < a; i++ ) {
b = b + temp;
}
return b;
}
public static void main(String[] args) {
Scanner scan=new Scanner(System.in);
System.out.println("Enter a: ");
int a = scan.nextInt();
System.out.println("Enter b: ");
int b = scan.nextInt();
System.out.println(a + "X" + b + " = " + multiply(a,b));
}
}
/*
output:
Enter a:
7
Enter b:
6
7X6 = 42
*/
// Problem4 java code
import java.util.Scanner;
// Java code to find gcd 2 numbers
class Problem4 {
public static int gcd(int a, int b)
{
if (b!=0)
return gcd(b, a%b);
else
return a;
}
public static void main(String[] args) {
Scanner scan=new Scanner(System.in);
System.out.println("Enter a: ");
int a = scan.nextInt();
System.out.println("Enter b: ");
int b = scan.nextInt();
System.out.println("gcd(" + a + "," + b + ") = " + gcd(a,b));
}
}
/*
output:
Enter a:
10
Enter b:
12
gcd(10,12) = 2
*/

Problem1 java codeimport java.util.Scanner; Java code to pr.pdf

  • 1.
    // Problem1 javacode import java.util.Scanner; // Java code to print all possible strings of letter L and R class Problem1 { static void print(char set[], int length) { int setLength = set.length; printRecursion(set, "", setLength, length); } static void printRecursion(char set[], String prefixset, int setLength, int length) { // Base case: length is 0 if (length == 0) { System.out.println(prefixset); return; } for (int i = 0; i < setLength; ++i) { String newPrefixset = prefixset + set[i]; printRecursion(set, newPrefixset, setLength, length - 1); } } public static void main(String[] args) { Scanner scan=new Scanner(System.in); System.out.println("Enter length: "); int length = scan.nextInt(); char set[] = {'L', 'R'}; print(set, length); } } /* output: Enter length: 3
  • 2.
    LLL LLR LRL LRR RLL RLR RRL RRR */ // Problem2 javacode import java.util.Scanner; // Java code to print all possible strings of letter L and R class Problem2 { static void print(char set[], int length) { int setLength = set.length; printRecursion(set, "", setLength, length); } static void printRecursion(char set[], String prefixset, int setLength, int length) { // Base case: length is 0 if (length == 0) { System.out.print(prefixset + " "); return; } for (int i = 0; i < setLength; ++i) { String newPrefixset = prefixset + set[i]; printRecursion(set, newPrefixset, setLength, length - 1); } } public static void main(String[] args) { Scanner scan=new Scanner(System.in); System.out.println("Enter length: "); int length = scan.nextInt(); char set[] = {'1', '3', '5', '7', '9'};
  • 3.
    print(set, length); System.out.println(); } } /* output: Enter length: 3 111113 115 117 119 131 133 135 137 139 151 153 155 157 159 171 173 175 177 179 191 193 195 197 199 311 313 315 317 319 331 333 335 337 339 351 353 355 357 359 371 373 375 377 379 391 393 395 397 399 511 513 515 517 519 531 533 535 537 539 551 553 555 557 559 571 573 575 577 579 591 593 595 597 599 711 713 715 717 719 731 733 735 737 739 751 753 755 757 759 771 773 775 777 779 791 793 795 797 799 911 913 915 917 919 931 933 935 937 939 951 953 955 957 959 971 973 975 977 979 991 993 995 997 999 */ // Problem3 java code import java.util.Scanner; // Java code to multiply 2 numbers class Problem3 { public static int multiply(int a, int b) { int temp = b; for (int i = 1; i < a; i++ ) { b = b + temp; } return b; } public static void main(String[] args) { Scanner scan=new Scanner(System.in);
  • 4.
    System.out.println("Enter a: "); inta = scan.nextInt(); System.out.println("Enter b: "); int b = scan.nextInt(); System.out.println(a + "X" + b + " = " + multiply(a,b)); } } /* output: Enter a: 7 Enter b: 6 7X6 = 42 */ // Problem4 java code import java.util.Scanner; // Java code to find gcd 2 numbers class Problem4 { public static int gcd(int a, int b) { if (b!=0) return gcd(b, a%b); else return a; } public static void main(String[] args) { Scanner scan=new Scanner(System.in); System.out.println("Enter a: "); int a = scan.nextInt(); System.out.println("Enter b: "); int b = scan.nextInt(); System.out.println("gcd(" + a + "," + b + ") = " + gcd(a,b));
  • 5.
    } } /* output: Enter a: 10 Enter b: 12 gcd(10,12)= 2 */ Solution // Problem1 java code import java.util.Scanner; // Java code to print all possible strings of letter L and R class Problem1 { static void print(char set[], int length) { int setLength = set.length; printRecursion(set, "", setLength, length); } static void printRecursion(char set[], String prefixset, int setLength, int length) { // Base case: length is 0 if (length == 0) { System.out.println(prefixset); return; } for (int i = 0; i < setLength; ++i) { String newPrefixset = prefixset + set[i]; printRecursion(set, newPrefixset, setLength, length - 1); } } public static void main(String[] args) {
  • 6.
    Scanner scan=new Scanner(System.in); System.out.println("Enterlength: "); int length = scan.nextInt(); char set[] = {'L', 'R'}; print(set, length); } } /* output: Enter length: 3 LLL LLR LRL LRR RLL RLR RRL RRR */ // Problem2 java code import java.util.Scanner; // Java code to print all possible strings of letter L and R class Problem2 { static void print(char set[], int length) { int setLength = set.length; printRecursion(set, "", setLength, length); } static void printRecursion(char set[], String prefixset, int setLength, int length) { // Base case: length is 0 if (length == 0) { System.out.print(prefixset + " "); return;
  • 7.
    } for (int i= 0; i < setLength; ++i) { String newPrefixset = prefixset + set[i]; printRecursion(set, newPrefixset, setLength, length - 1); } } public static void main(String[] args) { Scanner scan=new Scanner(System.in); System.out.println("Enter length: "); int length = scan.nextInt(); char set[] = {'1', '3', '5', '7', '9'}; print(set, length); System.out.println(); } } /* output: Enter length: 3 111 113 115 117 119 131 133 135 137 139 151 153 155 157 159 171 173 175 177 179 191 193 195 197 199 311 313 315 317 319 331 333 335 337 339 351 353 355 357 359 371 373 375 377 379 391 393 395 397 399 511 513 515 517 519 531 533 535 537 539 551 553 555 557 559 571 573 575 577 579 591 593 595 597 599 711 713 715 717 719 731 733 735 737 739 751 753 755 757 759 771 773 775 777 779 791 793 795 797 799 911 913 915 917 919 931 933 935 937 939 951 953 955 957 959 971 973 975 977 979 991 993 995 997 999 */ // Problem3 java code import java.util.Scanner;
  • 8.
    // Java codeto multiply 2 numbers class Problem3 { public static int multiply(int a, int b) { int temp = b; for (int i = 1; i < a; i++ ) { b = b + temp; } return b; } public static void main(String[] args) { Scanner scan=new Scanner(System.in); System.out.println("Enter a: "); int a = scan.nextInt(); System.out.println("Enter b: "); int b = scan.nextInt(); System.out.println(a + "X" + b + " = " + multiply(a,b)); } } /* output: Enter a: 7 Enter b: 6 7X6 = 42 */ // Problem4 java code import java.util.Scanner; // Java code to find gcd 2 numbers class Problem4 { public static int gcd(int a, int b) {
  • 9.
    if (b!=0) return gcd(b,a%b); else return a; } public static void main(String[] args) { Scanner scan=new Scanner(System.in); System.out.println("Enter a: "); int a = scan.nextInt(); System.out.println("Enter b: "); int b = scan.nextInt(); System.out.println("gcd(" + a + "," + b + ") = " + gcd(a,b)); } } /* output: Enter a: 10 Enter b: 12 gcd(10,12) = 2 */