Conditionals and Loops PDF
Conditionals and Loops PDF
System.out.println(salary);
}
}
Output :- 27400
}
}
Output :- 28400
int max = a;
if(b>max){
max = b;
}
if (c > max){
max = c;
}
System.out.println(max);
}
}
# Approach – 2:-
import java.util.Scanner;
public class LrgestOfThree {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int a = in.nextInt();
int b = in.nextInt();
int c = in.nextInt();
int max = 0;
if(a > b){
max = a;
} else {
max = b;
}
if (c > max){
max = c;
}
System.out.println(max);
}
}
# Approach 3 :-
Using Math.max :- Math is a class present in java.lang package and max is a
function present in it which takes two number as an argument and return maximum
out of them.
import java.util.Scanner;
public class LrgestOfThree {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int a = in.nextInt();
int b = in.nextInt();
int c = in.nextInt();
Counting occurrence :-
“ Input two numbers, find that hoe many times second number
digit is present in first number”
Ex :- first number = 14458
Second number = 4
Output = 2, because 4 is present 2 times in first number.
import java.util.Scanner;
Reverse a number
“ A number I input from the keyboard and Show the output as
Reverse of that number “
Example :- Input :- 12345
Output :- 54321
import java.util.Scanner;
public class ReverseANumber {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int num = in.nextInt();
int ans = 0;
while(num > 0){
int rem = num % 10;
num /= 10;
ans = ans * 10 + rem;
}
System.out.println(ans);
}
}
Input :- 458792
Output :- 297854
Calculator Program
import java.util.Scanner;
if (op == '+') {
ans = num1 + num2;
}
if (op == '-') {
ans = num1 - num2;
}
if (op == '*') {
ans = num1 * num2;
}
if (op == '/') {
if (num2 != 0) {
ans = num1 / num2;
}
}
if (op == '%') {
ans = num1 % num2;
}
} else if (op == 'x' || op == 'X') {
break;
} else {
System.out.println("Invalid operation!!");
}
System.out.println(ans);
}
}
}