TEST TIME ON CONDITIONAL
STATEMENTS
URL-
https://docs.google.com/forms/d/10hY3aSTNRHqoUFY
Tsfq_XTDR2vj4_7SwuJEC2NhEBDw/edit
QR code-
PROGRAMS ON CONTROL
STRUCTURES
QUESTION: 01
HCF of two numbers in Java
Here, in this section we will discuss HCF of two numbers in java.
GCD(Greatest Common Divisor) or HCF(Highest Common Factor) of two numbers is the
number which is the largest common factor of both numbers.
It is also referred as Greatest Common Factor(GCF), Greatest Common Measure(GCM),
Highest Common Divisor(HCD).
QUESTION: 01
class Main {
public static void main (String[]args) {
int num1 = 36, num2 = 60, hcf=0;
for (int i = 1; i <= num1 || i <= num2; i++) {
if (num1 % i == 0 && num2 % i == 0)
hcf = i;
}
System.out.println("The HCF: "+ hcf);
}
}
QUESTION: 02
Given an integer input, the objective is to find all the factors of a given integer input
but the number itself.
To do so we’ll use loops to iterate through the range from 2 to the number itself and
check whether there are any factors within the range. To do so, we’ll use loops to
iterate through the range and check if each number is a factor of the number or not.
Once we find that the number is divisible by any number we’ll append it to the factors
list or simply print the factors along the way. Here are a few methods to Find the
Factors of a Number in Java Language,
Method 1: Using Range as [ 2, number ]
Method 2: Using Range as [2, Sqrt( number )]
QUESTION: 02
Method 1 :
public class Main {
public static void main(String[] args) {
int num = 10;
System.out.println( "Factors of " + num + " are " );
// finding and printing factors b/w 1 to num
for(int i = 1; i <= num; i++) {
if(num % i == 0)
System.out.println(i + " ");
}
}
}
QUESTION: 02
Method 2 :(A)
class Main { // 10 should be printed just once
public static void main(String[] args) { if(i == n / i)
int num = 100; System.out.print(i + ", ");
getFactors (num); // else print both the pairs
} else
static void getFactors (int n) { System.out.print(i + ", " + n/i + ", ");
for(int i = 1; i <= Math.sqrt(n); ++i) { }
if (n % i == 0){ }
// If both pair of factors are equal then }
we just print }
// once, example for 100 : (a, b) : (10,
10)
QUESTION: 02
Method 2 :(B)
class Main { // if flag is true then we had double pairs
public static void main (String[]args) { like (10,10)
int num = 100; // we should do i-- so as not to do double
getFactors (num); printing of pair divisor
} // doing i -=2 rather than i-- as in previous
static void getFactors (int num) { for loop we exited
// Same i used in other for loop // with i++, example, i = 10 became 11
int i; and we need to start with 9
// to avoid double printing // so as to ignore 10 as its a double pair
boolean flag = false; if(flag)
for(i = 1; i <= Math.sqrt(num); i++) { i -= 2;
if (num % i == 0) // printing pairs
System.out.print(i + " "); for(; i >= 1; i--) {
// To avoid double printing of equal pairs if (num % i == 0)
// Example (10,10) we only want to print System.out.print(num/i + " ");
once }}}
if(i == num/i)
flag = true;
}
QUESTION: 03
Write a program to generate the following series --- 1,4,9,16,25, ....
Input format:
The input containing an integer which denotes 'n'
Output format:
Print the series and refer the sample output for formatting
Sample Input: Input (stdin) Input (stdin)
7 2 15
Sample Output: Output (stdout) Output (stdout)
1 4 9 16 25 36 49 14 1 4 9 16 25 36 49 64 81 100 121 144 169 196 225
QUESTION: 03
import java.util.Scanner;
class Main {
public static void main(String args[]) {
Scanner s=new Scanner(System.in);
int n=s.nextInt();
int i,nn=0;
for(i=1;i<=n;i++) {
nn=i*i;
System.out.printf("%d ",nn );
}
}
}
QUESTION: 04
Write a program to generate the following series 2, 5, 11, 23, 47, 95, 191...........
Input format: The input containing an integer which denotes 'n' Output format:
Print the series and refer the sample output for formatting
Input (stdin)
5
Output (stdout)
2 5 11 23 47
QUESTION: 04
import java.util.Scanner; t=(f*sum)+1;
sum=t;
class A { }
void fun() { }
Scanner s=new }
Scanner(System.in); }
int n=s.nextInt(); class Main {
int i; public static void main(String args[])
int sum=5,t,f=2; {
for(i=1;i<=n;i++) { A g=new A();
if(i==1) { g.fun();
System.out.print("2 "); }
} }
else {
System.out.print(sum+" ");
QUESTION: 05
Write a program to print the given pattern
Input format:
The input containing an integer which denotes the 'n'
Output format:
Refer the sample output for formatting. There is a trailing space at the end of
each line
QUESTION: 05
Sample Input: Input (stdin)
5 6
Sample Output: Output (stdout)
55555 666666
45555 566666
34555 456666
23455 345666
12345 234566
123456
QUESTION: 05
import java.util.Scanner; System.out.println("");
}
class A { }
void d() { }
Scanner s=new Scanner(System.in); class Main {
int a=s.nextInt(); public static void main(String args[]) {
int i,j,k;
for(i=a;i>=1;i--) { A c=new A();
for(j=i;j<=a;j++) { c.d();
System.out.print(j+""); }
} }
for(k=1;k<=i-1;k++) {
System.out.print(a+"");
}
QUESTION: 06
Write a program to print the given below pattern.
Input format:
The input containing an integer which denotes the value of 'n'
Output format:
Refer the sample output for formatting. There is a trailing space at the end of
each line.
QUESTION: 06
Sample Input: Input (stdin)
4 5
Sample Output: Output (stdout)
1112 11112
3222 32222
3334 33334
5444 54444
55556
QUESTION: 06
import java.util.Scanner; }
}
class A { }
void fun(int a) { class Main {
for(int i=1;i<=a;i++) { public static void main(String args[]) {
for(int j=1;j<=a;j++) { Scanner sc=new Scanner(System.in);
if(i%2!=0 && j==a || i%2==0 && j==1) int a=sc.nextInt();
{ A obj=new A();
System.out.print((i+1)); obj.fun(a);
} }
else { }
System.out.print(i);
}
}
System.out.printf("\n");
/ethnuscodemithra Ethnus Codemithra /ethnus /code_mithra
THANK YOU
https://learn.codemithra.com/
095 340