WINSEM2024-25 BCSE103E ELA VL2024250501226 2025-02-12 Reference-Material-I
WINSEM2024-25 BCSE103E ELA VL2024250501226 2025-02-12 Reference-Material-I
Loop
Switch Statement in Java
04/06/2025 2
• The switch statement is Java's multi-way branch statement.
• It is used to take the place of long if-else chains, and make them
more readable.
• unlike if statements, one may not use inequalities; each value must
be concretely defined.
04/06/2025 3
• There are three critical components to the switch statement:
• Case: This is the value that is evaluated for equivalence with the
argument to the switch statement.
• Default: This is an optional, catch-all expression, should none of the
case statements evaluate to true.
• Abrupt completion of the case statement; usually break: This is
required to prevent the undesired evaluation of further case
statements
04/06/2025 4
Syntax:
switch ( expression )
{
// Block of Statement
case 1 : break;
// Block of Statement .
break; .
default :
case 2 :
// Block of Statement
// Block of Statement break;
break; }
case 3 :
// Block of Statement
break;
case 4 :
04/06/2025 5
import java.util.Scanner; break;
public class switch_demo { case 2:
public static void main(String args[]) { c=a-b;
int a,b,c,ch; System.out.println("Subtraction :
System.out.println("1.Addition");
"+c);
break;
System.out.println("2.Subtraction");
case 3:
System.out.println("3.Multiplication"); c=a*b;
System.out.println("4.Division");
System.out.println("Enter Your Choice : "); System.out.println("Multiplication : "+c);
Scanner in =new Scanner(System.in); break;
case 4:
ch=in.nextInt();
c=a/b;
System.out.println("Enter 2 Nos : "); System.out.println("Division :
a=in.nextInt(); "+c);
b=in.nextInt(); switch (ch)
break;
default:
{ System.out.println("Invalid
case 1: Selection");
c=a+b; break;
}
System.out.println("Addition : "
+c); }
04/06/2025 } 6
public class group_switch {
public static void main(String args[]) {
case 'A':
char c;
case 'E':
System.out.println("Enter The Character : ");
case 'I':
Scanner in =new Scanner(System.in); case 'O':
c=in.next().charAt(0); case 'U':
System.out.println(c + " is a
switch (c) Vowels");
{ break;
case 'a':
default:
System.out.println(c + " is
case 'e':
Consonant");
case 'i':
break;
case '0': }
case 'u':
}
}
7
Sample Questions
• Write a program to read a weekday number and print weekday name using switch statement
• Write a program to read gender(M/F) and print the corresponding gender using a switch
statement
• Write a program to Check whether a character is a vowel or consonant using switch
statement
• Write a program to Check whether the number is even or odd using switch statement
• Write a program to Find the number of days in a month using a switch statement
• Write a program to create simple calculator using switch Statement
• Write a program to print remark according to the grade obtained using switch statement
• Write a program to Menu driven program using switch statement ( Find addition,
subtraction, multiplication and division of to integer numbers )
• Write a program to check whether a person is eligible to vote or Not using switch statement
• Write a program to find the Maximum of Two Numbers using switch statement
For Loop
The for loop is an example of an iterative code, i.e. this statement will cause the program to repeat a
particular set of code for a particular number of times. In the following example we will be using a
counter which starts at 0 and ends when it is smaller than 5, i.e. 4. Therefore the code following the
for loop will iterate for 5 times.
int count;
for(count = 0;count < 5; count = count+1)
System.out.println("This is count: " + count);
System.out.println("Done!");
}
}
04/06/2025 9
The Math Class
• In order to perform certain mathematical operations like square root (sqrt), or power
(pow);
• Java has a built in class containing a number of methods as well as static constants,
• e.g. Pi = 3.141592653589793 and E = 2.718281828459045.
• All the methods involving angles use radians and return a double
• (excluding the Math. Round ()).
04/06/2025 10
• package src;
• public class hypot {
• public static void main(String[] args) {
• // TODO Auto-generated method stub
• double x, y, z; x = 3;
• y = 4;
• z = Math.sqrt (x*x + y*y);
• System.out.println("Hypotenuse is " +z); } }
04/06/2025 11
• Math.round( ); where Math is the class name and round is the method name. If a particular method accepts
parameters, these are placed in brackets, e.g. Math.max(2.8, 12.9) – in this case it would return 12.9 as being
the larger number.
04/06/2025 12
Problems with For loop
04/06/2025 13
import java.util.Scanner;
04/06/2025 15
import java.util.Scanner;
class Ans{
public static void main(String[] args){
Scanner s = new Scanner(System.in);
int sum = 0;
for(int i = 0; i<10;i++){
System.out.println("Enter a number");
sum = sum+s.nextInt();
}
System.out.println("Sum is "+sum);
}
}
04/06/2025 16
int sum = 0;
int n = 1000;
// for loop
for (int i = 1; i <= n; ++i) {
// body inside for loop
sum += i; // sum = sum + i
}
04/06/2025 17
Write a Java program to calculate factorial of a number.
04/06/2025 18
package src;
import java.util.Scanner;
public class A {
public static void main(String args[])
{
Scanner s = new Scanner(System.in);
System.out.println("Enter a number");
int x = s.nextInt();
int fact = 1;
for(int i = x; i>=1 ;i--){
fact = fact*i;
}
System.out.println("Factorial is "+fact);
}}
04/06/2025 19
Write a program that prompts the user to input a positive integer. It
should then print the multiplication table of that number.
04/06/2025 20
package src;
import java.util.Scanner;
public class A {
public static void main(String args[])
{
Scanner s = new Scanner(System.in);
System.out.print("Enter number:");
int n=s.nextInt();
for(int i=1; i <= 10; i++)
{
System.out.println(n+" * "+i+" = "+n*i);
04/06/2025 21
Two numbers are entered by the user. Write a program to find the
value of one number raised to the power of another.
04/06/2025 22
Type Casting and Conversions
Casting is the term used when a value is converted from one data type to another, except for Boolean data types which cannot
be converted to any other type.
Usually conversion occurs to a data type which has a larger range or else there could be loss of precision Example18
{ //long to double automatic conversion
public static void main(String args[]) {
long L;
double D;
L = 100123285L;
D = L;
// L = D is impossible
04/06/2025 23
Example19
public static void main(String args[]) {
double x, y;
byte b;
int i;
char ch;
x = 10.0;
y = 3.0;
i=(int) (x / y);
// cast double to int
System.out.println("Integer outcome of x / y: " + i);
i = 100;
b = (byte) i;
System.out.println("Value of b: " + b);
i = 257;
b = (byte) i;
System.out.println("Value of b: " + b);
b = 88; // ASCII code for X
ch = (char) b;
System.out.println("ch: " + ch); } }
04/06/2025 24
Multiple Loop Control Variable
04/06/2025 25
Interesting For Loop Variations
04/06/2025 28
No ‘Body’ Loops
04/06/2025 30
Enhanced For Loop
• The enhanced style for can only cycle through an
array sequentially, from start to finish. It is also
known as the enhanced for loop. The enhanced for
loop was introduced in Java 5 as a simpler way to
iterate through all the elements of a Collection
(Collections are not covered in these pages). It can
also be used for arrays, as in the above example, but
this is not the original purpose.
• Enhanced for loops are simple but inflexible. They
can be used when you wish to step through the
elements of the array in first-to-last order, and you do
not need to know the index of the current element.
• Syntax:
for( Datatype item : array )
{
// body of loop;
}
04/06/2025 31
public class Enhanced_for {
public static void main(String args[])
{
int numbers[]={10,20,30,40,50,60,70};
for(int n : numbers)
{
System.out.println(n);
}
}
}
04/06/2025 32
Print Pattern using For Loop
*****
*****
*****
*****
*****
04/06/2025 33
package src;
import java.util.Scanner;
public class A {
public static void main(String args[])
{
int size = 5;
// outer loop
for (int i = 0; i < size; i++) {
// inner loop
for (int j = 0; j < size; j++) {
System.out.print("*");
}
System.out.println();
}
}}
04/06/2025 34
*****
* *
* *
* *
*****
04/06/2025 35
package src;
import java.util.Scanner;
else {
public class A {
public static void main(String args[]) // print star only at
{ first and last position row
int size = 5;
if (j == 0 || j == size - 1) {
// outer loop
for (int i = 0; i < size; i++) { System.out.print("*");
// inner loop }
for (int j = 0; j < size; j++) {
else {
// print only star in first and
last row
System.out.print(" ");
if (i == 0 || i == size - 1) { }
System.out.print("*"); }
} }
System.out.println();
}}}
04/06/2025 36
*
**
***
****
*****
04/06/2025 37
public class leftTrianlge {
public static void main(String[] args) {
04/06/2025 38
04/06/2025 39
package src;
import java.util.Scanner;
public class A {
public static void main(String args[])
{
int size = 5; for (int k = 0; k <= i; k++) {
System.out.print("*");
for (int i = 0; i < size; i++) {
}
System.out.println();
}
for (int j = 1; j < size - i; j++) {
}}
System.out.print(" ");
}
04/06/2025 40
04/06/2025 41
package src;
import java.util.Scanner;
public class A {
public static void main(String args[])
{
int rows = 5;
// loop to iterate for the given number of rows
for (int i = 1; i <= rows; i++) {
// loop to print the number of spaces before the star
for (int j = rows; j >= i; j--) {
System.out.print(" ");
}
// loop to print the number of stars in each row
for (int j = 1; j <= i; j++) {
System.out.print("* ");
}
// for new line after printing each row
System.out.println(); }}}
04/06/2025 42
04/06/2025 43
package src;
import java.util.Scanner;
public class A {
public static void main(String args[])
{
int size = 5;
for (int i = 0; i < size; i++) {
// print stars
for (int j = 0; j < size - i; j++) {
System.out.print("*");
}
System.out.println();
}
}
}
04/06/2025 44
04/06/2025 45
package src;
import java.util.Scanner;
public class A {
public static void main(String args[])
{
int size = 5;
for (int i = 0; i < size; i++) {
// print spaces
for (int j = 0; j < i; j++) {
System.out.print(" ");
}
// print stars
for (int j = size; j > i; j--) {
System.out.print("*");
}
System.out.println();
}
}
}
04/06/2025 46
Hollow Triangle
04/06/2025 47
package src;
import java.util.Scanner;
// print star otherwise print space
public class A {
if(k == 1 || k == 2*i -1 || i == size)
public static void main(String args[]) System.out.print("*");
{ else
int size = 5; System.out.print(" ");
}
for (int i = 1; i <= size; i++) {
System.out.println();
// first inner loop for spaces }
for (int j = 1; j <= size - i; j++) { }
System.out.print(" "); }
}
// second inner loop for stars and
intermediate spaces
for (int k = 1; k <= 2*i -1; k++) {
// check if its first and last column or last
row
04/06/2025 48
04/06/2025 49
package src;
import java.util.Scanner; for (int k = 1; k <= 2 * (size - i) + 1; k+
+) {
public class A {
// print star for first and last
public static void main(String args[]) column and for first row
{ if (k == 1 || k == 2 * (size - i) +
int size = 5; 1 || i == 1) {
System.out.print("*");
} else {
for (int i = 1; i <= size; i++) { System.out.print(" ");
// first inner loop for spaces }
for (int j = 1; j <= i - 1; j++) { }
System.out.println();
System.out.print(" ");
}
} }
// second inner loop for stars and spaces }
04/06/2025 50
04/06/2025 51
public static void main(String args[]) {
int n = 5;
//Loop to iterate over each row
for (int i = 1; i <= n; i++) {
//Loop to iterate over each column of the ith row
for (int j = 1; j <= i; j++) {
System.out.print(i + " ");
}
System.out.println();
}
}
}
04/06/2025 52
1
12
123
1234
12345
04/06/2025 53
public static void main(String args[]) {
int n = 5;
//Loop to iterate over each row
for (int i = 1; i <= n; i++) {
//Loop to iterate over each column of the ith row
for (int j = 1; j <= i; j++) {
System.out.print(j + " ");
}
System.out.println();
}
}
}
04/06/2025 54
1
12
123
1234
12345
1234
123
12
1
04/06/2025 55
public static void main(String args[]) {
int n = 5; //Printing Lower Half for n-1 rows
//Printing Upper Half for n rows //Loop to iterate over each row in
//Loop to iterate over each row reverse order
for (int i = n - 1; i >= 1; i--) {
for (int i = 1; i <= n; i++) {
//Loop to iterate over each column of
//Loop to iterate over each column of the ith row the ith row
for (int j = 1; j <= i; j++) { for (int j = 1; j <= i; j++) {
System.out.print(j + " "); System.out.print(j + " ");
}
}
System.out.println();
System.out.println(); }
} }
04/06/2025 56
1
23
456
7 8 9 10
11 12 13 14 15
int i, j, k = 1;
for (i = 1; i <= 5; i++) {
for (j = 1; j< i+1; j++) {
System.out.print(k++ + " ");
}
System.out.println();
}
}
}
12345
12345
12345
12345
12345
int size = 5;
2.We will have nested loops where the first internal loop will
numbers.
3.Use the first internal loop and print spaces for the times as
// printing square
for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
System.out.print((char)(alpha+j));
}
System.out.println();
}
}
}
int size = 5;
int alpha = 65; } else {
// print alphabets at first and last positon of row
if (j == 0 || j == size - 1) {
// external loop System.out.print((char)(alpha+j));
} else {
for (int i = 0; i < size; i++) { System.out.print(" ");
}
// internal loop
}
for (int j = 0; j < size; j++) { }
// num = 1;
// print only alphabets in first and last row System.out.println();
if (i == 0 || i == size - 1) { }
}
System.out.print((char)(alpha+j)); }
int size = 5;
int alpha = 65;
// printing pattern
for (int i = 0; i < size; i++) {
for (int j = 0; j <= i; j++) {
System.out.print((char)(alpha+j));
}
System.out.println();
}
}
}
int size = 5;
int alpha = 65;
// printing pattern
for (int i = 0; i < size; i++) {
// print spaces
for (int j = 1; j < size - i; j++) {
System.out.print(" ");
}
// print alphabets
for (int k = 0; k <= i; k++) {
System.out.print((char)(alpha+k));
}
System.out.println();
}
}
}
int size = 5;
int alpha = 65;
04/06/2025 78
While loop
04/06/2025 79
The do-while Loop
• This conditional statement checks the Boolean expression after going at least one
time through the loop. The do-while is declared as follows:
• do
• { statements;
• }
• while(condition);
• Braces are used if there is more than one statements and to improve program
readability.
04/06/2025
80
Compute integer powers of 2
int e;
int result;
for(int i=0; i < 10; i++)
{ result = 1;
e = i;
while(e > 0)
{ result *= 2;
e--; }
System.out.println("2 to the power of " +i+ " is " + result); } }}
04/06/2025 81
Type Casting
04/06/2025 82
• String s="200";
• //Converting String into int using Integer.parseInt()
• int i=Integer.parseInt(s);
• //Printing value of i
• System.out.println(i);
• }}
04/06/2025 83
• String s="200";
• //Converting String into int using Integer.parseInt()
• int i=Integer.parseInt(s);
• System.out.println(s+100);//
200100, because "200"+100, here + is a string concatenation operator
• System.out.println(i+100);//
300, because 200+100, here + is a binary plus operator
• }}
04/06/2025 84
04/06/2025 85
• String.valueOf()
• The String.valueOf() method converts int to String. The valueOf() is
the static method of String class. The signature of valueOf() method is
given below:
• int i=10;
• String s=String.valueOf(i);//Now it will return "10"
04/06/2025 86
• int i=200;
• String s=String.valueOf(i);
• System.out.println(i+100);//300 because + is binary plus operator
• System.out.println(s+100);//
200100 because + is string concatenation operator
• }}
04/06/2025 87
• int i=200;
• String s=Integer.toString(i);
• System.out.println(i+100);//300 because + is binary plus operator
• System.out.println(s+100);//200100 because + is string
concatenation operator
• }}
04/06/2025 88
The String.format() method is used to format given arguments into
String.
public static String format(String format, Object... args)
• int i=200;
• String s=String.format("%d",i);
• System.out.println(s);
• }}
04/06/2025 89
04/06/2025 90
public static long parseLong(String s)
long l=Long.parseLong("200");
• String s="9990449935";
• long l=Long.parseLong(s);
• System.out.println(l);
• }}
04/06/2025 91
Long to String
• long i=9993939399L;
• String s=String.valueOf(i);
• System.out.println(s);
• }}
04/06/2025 92
04/06/2025 93
• String s="23.6";
• float f=Float.parseFloat("23.6");
• System.out.println(f);
• }}
04/06/2025 94
Float to string
• float f=12.3F;//F is the suffix for float
• String s=String.valueOf(f);
• System.out.println(s);
• }}
04/06/2025 95
04/06/2025 96
String to Date
• package src;
• import java.util.Date;
• import java.text.ParseException;
• import java.text.SimpleDateFormat;
• public class str {
04/06/2025 97
Date to String
package src;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Calendar;
}
}
04/06/2025 98
Program to print numbers from 1 to
5
while(i <= n) {
System.out.println(i);
i++;
}
}}
04/06/2025 99
int sum = 0;
Scanner input = new Scanner(System.in);
System.out.println("Enter a number");
int number = input.nextInt();
while (number >= 0) {
sum += number;
System.out.println("Enter a number");
number = input.nextInt();
}
System.out.println("Sum = " + sum);
}
}
04/06/2025 100
While Practice Problems
04/06/2025 101
• Write a program to print multiplication table using while loop
04/06/2025 102
Write a Java Program that prompts the user to input an integer and then
outputs the number with the digits in reversed order
04/06/2025 103
Convert the following while loop
to the corresponding for loop
• int m = 5, n = 10;
• while (n>=1)
•{
• System.out.println(m*n);
• n--;
•}
04/06/2025 104
Java Do while loop
do
{
//code to be executed
}while(condition);
04/06/2025 105
• do while to Print “Hello world” for 5 times
04/06/2025 106
Do while to choose a number greater than 10 and to find the sum of
numbers between the chosen number and 10
04/06/2025 107
While loop Problems
Write a program to print all natural numbers from 1 to n
package src;
import java.util.Scanner;
public class A {
public static void main(String args[])
{
Scanner input = new Scanner(System.in);
System.out.print("Enter The Starting Number : ");
int s =input.nextInt();
System.out.print("Enter The Ending Number : ");
int e =input.nextInt();
while(s<=e)
{
System.out.println(s);
s++;
04/06/2025 }}} 108
Write a program to print all natural numbers in
reverse
04/06/2025 109
Write a program to print tables
04/06/2025 110
Write a program that prompts the user to input an integer and then outputs the number with the digits reversed. For example, if the input is 12345, the output should be 54321.
int number;
int reverse = 0;
System.out.print("Enter the number ");
number = console.nextInt();
int temp = number;
int remainder = 0;
while(temp>0)
{
remainder = temp % 10;
reverse = reverse * 10 + remainder;
temp /= 10;
}
System.out.println("Reverse of " + number + " is " + reverse);
04/06/2025 } 111
Write a program that reads a set of integers, and then prints the sum of the even and odd integers.
package src;
import java.util.Scanner; temp = temp / 10;
digit2 = temp % 10;
public class A {
temp = temp / 10;
public static void main(String args[]) digit3 = temp % 10;
{ int digit1,digit2, digit3; if(digit1*digit1*digit1 +
int number=1; digit2*digit2*digit2 + digit3*digit3*digit3
== number)
while(number <= 500) {
{ System.out.println(number);
number++; }
}}}
int temp = number;
digit1 = temp % 10;
04/06/2025 113