[go: up one dir, main page]

0% found this document useful (0 votes)
453 views59 pages

Java DSA Kunal Kushwaha

The document provides an overview of programming concepts including conditional statements (if-else, multiple if-else) and loops (for, while, do-while) in Java. It includes examples of how to implement these concepts, such as finding the largest of three numbers, checking the case of a character, and calculating Fibonacci numbers. Additionally, it features practical applications like counting occurrences of a digit, reversing a number, and creating a simple calculator program.

Uploaded by

souravmishra1103
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
453 views59 pages

Java DSA Kunal Kushwaha

The document provides an overview of programming concepts including conditional statements (if-else, multiple if-else) and loops (for, while, do-while) in Java. It includes examples of how to implement these concepts, such as finding the largest of three numbers, checking the case of a character, and calculating Fibonacci numbers. Additionally, it features practical applications like counting occurrences of a digit, reversing a number, and creating a simple calculator program.

Uploaded by

souravmishra1103
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 59

10

Assize
Max
10/4
=
A =

"
b =
20
isffb > max :

C =
30 b
Max =

if e > max :

max =
8


0 1st
i s 4 S
6 7

ii.
%%!I:÷
,

. -

2 '
.
.

'

3 .

%5
?⃝
n =
13 -85705708079 A
13890/10 h % to
lastdigit
=

n =

10313897138

-1380¥
N __
13839 ¥2 13839 %w=q
I
count -0
138 =3
? 1-
while # e) {
Krem
✗ last digit
-

1,38--8
n -1.10

3) 2 v
iyfrem
__ =

13
=
3
wwrttti
It
v
}
/
1 -
Ho _③
n=n
j
' •

M
3 ✓ ① As
0

.
Q . h= 23597
Ay79S3①

|

" " " + " "
"
"

=
79×-101-5--795

= 795*10 -13--7953

= 7953×-10+2
-_79s①⑥
1

Conditional and loops


Condition:- It provide check for the statement.
1. If-else statement → Used to check the condition, it checks the
Boolean condition True or False.
Syntax :-
if (boolean expression True or false){
//Body
} else{
// Do this
}
Example:-
public class IfElse {
public static void main(String[] args) {
int salary = 25400;
if (salary> 10000) {
salary = salary + 2000;
}
else {
salary = salary + 1000;
}

System.out.println(salary);

}
}

Output :- 27400

Community Classroom Kunal Kushwaha


2

2. Multiple if-else statement


→ It executes one condi on from mul ple statements.
Syntax :-
if (condition 1){
// code to be executed if condition 1 is true
} else if (condition 2) {
// code to be executed if condition 2 is true
} else if (condition 3){
// code to be executed if condition 3 is true
} else {
// code to be executed if all conditions are false
}
Example :-
public class MultipleIfElse {
public static void main(String[] args) {
int salary = 25400;
if (salary<= 10000) {
salary +=1000;
}
else if (salary <= 20000) {
salary += 2000;
}
else {
salary += 3000;
}
System.out.println(salary);

}
}

Output :- 28400

Community Classroom Kunal Kushwaha


3

Loop → Loops are used to iterate a part of program several times.


1. for loop :- It is generally used when we know how many times
loop will iterate.
Syntax :-
for (initialization; condition; increment/decrement){
// body
}
Example:- print numbers from 1 to 5
public class forloop {
public static void main(String[] args) {
for (int num=1;num<=5;num+=1){
System.out.println(num);
}
}
}
Output :- 1
2
3
4
5
Example 2 :- print numbers from 1 to n
import java.util.Scanner;
public class forloop {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
for (int num=1;num<=n;num+=1){
System.out.print(num + " ");
}
}
}
Input : 6
Output :- 1 2 3 4 5 6

Community Classroom Kunal Kushwaha


4

2. While Loop :- It is used when we don’t know how many time


the loop will iterate.
Syntax :-
while (condition){
// code to be executed
// increment/decrement
}
Example :-
public class whileloop {
public static void main(String[] args) {
int num = 1;
while (num <=5){
System.out.println(num);
num += 1;
}
}
}
Output :- 1
2
3
4
5

Community Classroom Kunal Kushwaha


5

3. do while loop :- It is used when we want to execute our


statement at least one time.
→ It is called exit control loop because it checks the condition
after execution of statement.
Syntax :-
do{
// code to be executed
// update statement -> increment/decrement
}while (condition);
Example :-
public class doWhileloop {
public static void main(String[] args) {
int n = 1;
do{
System.out.println(n);
n++;
} while(n<=5);
}
}
Output :- 1
2
3
4
5
While Loop Do while loop
→ used when no. of itera on is not → used when we want to execute
fixed the statement at least ones

→ Entry controlled loop → Exit controlled loop


→ no semicolon required at the → semicolon is required at the end
end of while (condition) of while (condition)

Community Classroom Kunal Kushwaha


6

 Program to find largest of three numbers.


“Take 3 integer input from keyboard, Find the largest numbers
among them “.
# Approach -1 :-
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 = 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 {

Community Classroom Kunal Kushwaha


7

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();

int max = Math.max(c,Math.max(a,b));


System.out.println(max);
}
}
Input :- 3 6 5
Output :- 6

Community Classroom Kunal Kushwaha


8

 Alphabet case check


“ Take an input character from keyboard and check weather it
is Upper case alphabet or lower case alphabet”
import java.util.Scanner;
public class AlphabetCaseCheck {
public static void main(String[] args) {
Scanner in = new Scanner (System.in);
char ch = in.next().trim().charAt(0);
if (ch > 'a' && ch <= 'z'){
System.out.println("Lowercase");
}
else {
System.out.println("Uppercase");
}
}
}
Input :- a
Output :- Lowercase
Input :- Z
Output :- Uppercase

Community Classroom Kunal Kushwaha


9

 Fibonacci Numbers :- a series of numbers in which each number


( Fibonacci number ) is the sum of the two preceding numbers.
Ex :- 0,1,1,2,3,5,8,13…
→ Find the nth Fibonacci number.
“ Given three input a, b, n a is the starting number of Fibonacci series and
b is the next number after a, n is an number to find the nth Fibonacci
number”
import java.util.Scanner;
public class FibonacciNumbers{
public static void main(String[] args) {
Scanner in = new Scanner (System.in);
int n = in.nextInt();
int a = in.nextInt();
int b = in.nextInt();
int count = 2;

while(count <= n){


int temp = b;
b = b+a;
a = temp;
count++;
}
System.out.println(b);
}
}
Input :- 0 1 7
Output :- 8.

Community Classroom Kunal Kushwaha


10

 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;

public class CountingOccurence {


public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int count = 0;
int Fn = in.nextInt();
int Sn = in.nextInt();
while (Fn>0){
int rem = Fn % 10;
if (rem == Sn){
count++;
}
Fn = Fn/10;
}
System.out.println(count);
}
}
Input :- 45535 5
Output :- 3

Community Classroom Kunal Kushwaha


11

 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

Community Classroom Kunal Kushwaha


12

Calculator Program
import java.util.Scanner;

public class Calculator {


public static void main(String[] args) {
Scanner in = new Scanner(System.in);
// Take input from user till user does not press X or x
int ans = 0;
while (true) {
// take the operator as input
System.out.print("Enter the operator: ");
char op = in.next().trim().charAt(0);

if (op == '+' || op == '-


' || op == '*' || op == '/' || op == '%') {
// input two numbers
System.out.print("Enter two numbers: ");
int num1 = in.nextInt();
int num2 = in.nextInt();

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);
}
}
}

Community Classroom Kunal Kushwaha


13

Input and output:-

Community Classroom Kunal Kushwaha

You might also like