diff --git a/.classpath b/.classpath index fb50116..fceb480 100644 --- a/.classpath +++ b/.classpath @@ -1,6 +1,6 @@ - + diff --git a/src/Notes/Conditional Statements b/src/Notes/Conditional Statements new file mode 100644 index 0000000..42faf1b --- /dev/null +++ b/src/Notes/Conditional Statements @@ -0,0 +1,114 @@ +Java Conditional Statements – Study Material +Overview +This session covers Java conditional statements, a key concept used to control the flow of execution in Java programs based on certain conditions. These are crucial when automating test cases using Selenium. + +Types of Conditional Statements in Java +1. if Statement +Syntax: + + java +CopyEdit +if (condition) { + // code to execute if condition is true +} + + +Usage: Executes a block only if the condition is true. + + +2. if-else Statement +Syntax: + + java +CopyEdit +if (condition) { + // true block +} else { + // false block +} + + +Usage: Provides an alternate block of code if the condition is false. + + +3. if-else-if Ladder +Syntax: + + java +CopyEdit +if (condition1) { + // block 1 +} else if (condition2) { + // block 2 +} else { + // default block +} + + +Usage: Checks multiple conditions sequentially. + + +4. Nested if +Usage: if statement inside another if block. + + +Allows more complex decision structures. + + + +Switch Statement +Syntax: + + java +CopyEdit +switch (expression) { + case value1: + // code block + break; + case value2: + // code block + break; + default: + // default block +} + + +Usage: Best used when evaluating one variable against many possible constant values. + + +Limitations: Works only with byte, short, char, int, enum, and String (Java 7+). + + + +Comparison and Logical Operators +Used in condition checks: +Comparison Operators: ==, !=, <, >, <=, >= + + +Logical Operators: && (AND), || (OR), ! (NOT) + + + +Practical Examples +Age Validation: Check if a user is eligible to vote. + + +Grade System: Use of if-else-if ladder to assign grades. + + +Menu Selection: Switch case to select options like login, register, etc. + + + +Key Points +Conditional statements allow branching logic in programs. + + +Use if when there's only one condition. + + +Use switch for cleaner code when checking a single variable against multiple constants. + + +Always test boundary conditions. + diff --git a/src/controlStatements/IfCondition.java b/src/controlStatements/IfCondition.java new file mode 100644 index 0000000..4974ed7 --- /dev/null +++ b/src/controlStatements/IfCondition.java @@ -0,0 +1,14 @@ +package controlStatements; + +public class IfCondition { + + public static void main(String[] args) { + int person_age=25; + if(person_age>=18) // always boolean expression + { + System.out.println("Person is eligible for Voting"); + } + + } + +} diff --git a/src/controlStatements/IfCondition2.java b/src/controlStatements/IfCondition2.java new file mode 100644 index 0000000..35124b8 --- /dev/null +++ b/src/controlStatements/IfCondition2.java @@ -0,0 +1,25 @@ +package controlStatements; + +public class IfCondition2 { + + public static void main(String[] args) { + + int num=1; + + if(num>0) + { + System.out.println("Positive Number"); + } + else if(num<0) + { + System.out.println("Negative Number"); + } + else + { + System.out.println("Zero"); + } + + + } + +} diff --git a/src/controlStatements/IfCondition3.java b/src/controlStatements/IfCondition3.java new file mode 100644 index 0000000..228e632 --- /dev/null +++ b/src/controlStatements/IfCondition3.java @@ -0,0 +1,24 @@ +package controlStatements; + +public class IfCondition3 { + + public static void main(String[] args) { + + int a=10, b=200, c=30; + + if(a>b && a>c) + { + System.out.println("a is Largest number:"+a); + } + else if(b>a && b>c) + { + System.out.println("b is largest number:"+b); + } + else + { + System.out.println("c is largest number:"+c); + } + + } + +} diff --git a/src/controlStatements/IfCondition4.java b/src/controlStatements/IfCondition4.java new file mode 100644 index 0000000..8f7f5e9 --- /dev/null +++ b/src/controlStatements/IfCondition4.java @@ -0,0 +1,20 @@ +package controlStatements; + +public class IfCondition4 { + + public static void main(String[] args) { + //Smallest of numbers + + int a = 10, b = 20, c = 30; + + if (a <= b && a <= c) { + System.out.println("Smallest number is: " + a); + } else if (b <= a && b <= c) { + System.out.println("Smallest number is: " + b); + } else { + System.out.println("Smallest number is: " + c); + } + + } + +} diff --git a/src/controlStatements/IfCondition5.java b/src/controlStatements/IfCondition5.java new file mode 100644 index 0000000..1883523 --- /dev/null +++ b/src/controlStatements/IfCondition5.java @@ -0,0 +1,32 @@ +package controlStatements; + + +public class IfCondition5 { + + public static void main(String[] args) { + //Two Largest Numbers + + int a = 10, b = 20, c = 30; + + if (a >= b && a >= c) { + if (b >= c) { + System.out.println("Two largest numbers: " + a + " and " + b); + } else { + System.out.println("Two largest numbers: " + a + " and " + c); + } + } else if (b >= a && b >= c) { + if (a >= c) { + System.out.println("Two largest numbers: " + b + " and " + a); + } else { + System.out.println("Two largest numbers: " + b + " and " + c); + } + } else { + if (a >= b) { + System.out.println("Two largest numbers: " + c + " and " + a); + } else { + System.out.println("Two largest numbers: " + c + " and " + b); + } + } + } + +} diff --git a/src/controlStatements/IfElseCondition1.java b/src/controlStatements/IfElseCondition1.java new file mode 100644 index 0000000..b77e122 --- /dev/null +++ b/src/controlStatements/IfElseCondition1.java @@ -0,0 +1,19 @@ +package controlStatements; + +public class IfElseCondition1 { + + public static void main(String[] args) { + + int a=10; + + if(a%2==0) + { + System.out.println("Even number"); + } + else + { + System.out.println("Odd number"); + } + } + +} diff --git a/src/controlStatements/IfElseCondtion.java b/src/controlStatements/IfElseCondtion.java new file mode 100644 index 0000000..0e1743a --- /dev/null +++ b/src/controlStatements/IfElseCondtion.java @@ -0,0 +1,19 @@ +package controlStatements; + +public class IfElseCondtion { + + public static void main(String[] args) { + int person_age=17; + if(person_age>=18) // always boolean expression + + { + System.out.println("Person is eligible for Voting"); + } + else + { + System.out.println("Person is not eligible for Voting"); + } + + } + +} diff --git a/src/controlStatements/Multiplecondtions.java b/src/controlStatements/Multiplecondtions.java new file mode 100644 index 0000000..be117fd --- /dev/null +++ b/src/controlStatements/Multiplecondtions.java @@ -0,0 +1,46 @@ +package controlStatements; + +public class Multiplecondtions { + + @SuppressWarnings("unused") + public static void main(String[] args) { + /* + if(false) + { + System.out.println(1); + } + else + { + System.out.println(2); + } + */ + + /* if(1==2) + { + System.out.println(1); + } + else + { + System.out.println(2); + } + */ + + if(true) + { + if(true) + { + System.out.println("abc"); + } + else + { + System.out.println("xyz"); + } + } + else + { + System.out.println("123"); + } + + } + +} diff --git a/src/controlStatements/PrintWeekNames.java b/src/controlStatements/PrintWeekNames.java new file mode 100644 index 0000000..17b46eb --- /dev/null +++ b/src/controlStatements/PrintWeekNames.java @@ -0,0 +1,44 @@ +package controlStatements; + +public class PrintWeekNames { + + public static void main(String[] args) { + + int weekno=0; + + if(weekno==1) + { + System.out.println("Sunday"); + } + else if(weekno==2) + { + System.out.println("Monday"); + } + else if(weekno==3) + { + System.out.println("Tuesday"); + } + else if(weekno==4) + { + System.out.println("Wednesday"); + } + else if(weekno==5) + { + System.out.println("Thursday"); + } + else if(weekno==6) + { + System.out.println("Friday"); + } + else if(weekno==7) + { + System.out.println("Saturday"); + } + else + { + System.out.println("invalid week number"); + } + + } + +} diff --git a/src/controlStatements/SwitchCase.java b/src/controlStatements/SwitchCase.java new file mode 100644 index 0000000..ef61b82 --- /dev/null +++ b/src/controlStatements/SwitchCase.java @@ -0,0 +1,30 @@ +package controlStatements; + +public class SwitchCase { + + public static void main(String[] args) { + + int weekno=1; + switch(weekno) + { + case 1: System.out.println("Sunday"); + break; + case 2: System.out.println("Monday"); + break; + case 3: System.out.println("Tuesday"); + break; + case 4: System.out.println("Wednesday"); + break; + case 5: System.out.println("Thursday"); + break; + case 6: System.out.println("Friday"); + break; + case 7: System.out.println("Saturday"); + default: System.out.println("Invalid week"); + } + + + + } + +} diff --git a/src/controlStatements/SwitchCase1.java b/src/controlStatements/SwitchCase1.java new file mode 100644 index 0000000..7c13bf7 --- /dev/null +++ b/src/controlStatements/SwitchCase1.java @@ -0,0 +1,29 @@ +package controlStatements; + +public class SwitchCase1 { + + public static void main(String[] args) { + + String weekName="Sunday"; + + switch(weekName) + { + case "Sunday": System.out.println(1); + break; + case "Monday": System.out.println(2); + break; + case "Tuesday": System.out.println(3); + break; + case "Wednesday": System.out.println(4); + break; + case "Thursday": System.out.println(5); + break; + case "Friday": System.out.println(6); + break; + case "Saturday": System.out.println(7); + default : System.out.println("invalid Week"); + } + + } + +} diff --git a/src/controlStatements/TwoLargestNumbersWithTernary.java b/src/controlStatements/TwoLargestNumbersWithTernary.java new file mode 100644 index 0000000..fdbb220 --- /dev/null +++ b/src/controlStatements/TwoLargestNumbersWithTernary.java @@ -0,0 +1,49 @@ +package controlStatements; + +public class TwoLargestNumbersWithTernary { + + public static void main(String[] args) { + + int a=10, b=20, c=30; + + int largest = (a >=b && a>=c) ? a:(b >=a && b>=c) ? b:c; + + int secondLargest = (largest == a) ? (b>=c?b:c) : (largest == b) ? (a>=c?a:c) : (a>=b?a:b); + + System.out.println("Two LArgest Numbers: "+largest+ "and"+secondLargest); + + } + +} + +/* Explanation: + + Finding the largest: + + int largest = (a >= b && a >= c) ? a : (b >= a && b >= c) ? b : c; + + Checks if a is the largest. + + If not, checks if b is the largest. + + Otherwise, c must be the largest. + + Finding the second largest: + + int secondLargest = (largest == a) ? (b >= c ? b : c) : + (largest == b) ? (a >= c ? a : c) : + (a >= b ? a : b); + + Based on who the largest is, compares the other two to find the second largest. + + Printing the result: + + + System.out.println("Two largest numbers: " + largest + " and " + secondLargest); + + ✅ Output for a = 10, b = 20, c = 30: + + + Two largest numbers: 30 and 20 + +*/ \ No newline at end of file diff --git a/src/coreJava/AssignmentOperators.java b/src/coreJava/AssignmentOperators.java new file mode 100644 index 0000000..f8c11c7 --- /dev/null +++ b/src/coreJava/AssignmentOperators.java @@ -0,0 +1,34 @@ +package coreJava; + +public class AssignmentOperators { + + public static void main(String[] args) { + //5. Assignment = += -+ *= /= %= + + int a=10; + int b=10; + a+=5; //a=a+5; + b=b-5; //b=b-5; + System.out.println(a); + System.out.println(b); + + int c=10; + a*=2; //c=c*2; + System.out.println(c); + + int d=10; + d/=2; //d=d/2; + System.out.println(d); + + int e=10; + e%=2; //e=e%2; + System.out.println(e); + + /* difference b/w == = ? + * == is relational operators it compares the value + * = is assignment operator , it assigns the value + */ + + } + +} diff --git a/src/coreJava/DataTypes.java b/src/coreJava/DataTypes.java index 6f0e47c..101ae51 100644 --- a/src/coreJava/DataTypes.java +++ b/src/coreJava/DataTypes.java @@ -1,134 +1,134 @@ -package coreJava; - -public class DataTypes { - - public static void main(String[] args) { + package coreJava; - //variables - - //int s; //declaration - //s=100; //assignment - - int s=100; // declaration+assignment - System.out.println(s); - - s=200; - System.out.println(s); + public class DataTypes { + + public static void main(String[] args) { + //variables + + //int s; //declaration + //s=100; //assignment + + int s=100; // declaration+assignment + System.out.println(s); + + s=200; + System.out.println(s); + /* // approach1 // if all the variables belongs to different data types int p=1; int o=2; int i=3; */ - - /* approach2 // only if allthe variable are belongs to same data type + + /* approach2 // only if all the variable are belongs to same data type int p,o,i; p=1; o=2; i=3; */ - + // approach3 //only if all the variable are belongs to same data type - int p=1,o=2,i=3; - + int p=1,o=2,i=3; + //for meaningful message in print statement we can use concatenation method - System.out.println("The Value of p is :"+p); - System.out.println("The Value of o is :"+o); - System.out.println("The Value of i is :"+i); + System.out.println("The Value of p is :"+p); + System.out.println("The Value of o is :"+o); + System.out.println("The Value of i is :"+i); // in single line we printed all variable values - System.out.println(p+""+o+""+i+""); - + System.out.println(p+""+o+""+i+""); + // in print statement we used join method for each value with "" -//-------------------------------------------------------------------------- + //-------------------------------------------------------------------------- // Data types - - // Numeric Data Types - - int a=100, b=200; - System.out.println("The Value of a is :"+a); - System.out.println("The Value of b is :"+b); - System.out.println(a+b); - System.out.println("The sum of an and b is : "+(a+b)); - - byte by=125; - System.out.println(by); - - short sh=32767; - System.out.println(sh); - - long l=21212121234443534L; // literal is needed - // L should add, it can be lower or uppercase - System.out.println(l); - - // Decimal numbers - float, double - - float item_price=15.5f; // literal is needed - //f should add, it can be lower or uppercase - System.out.println(item_price); - - double dbl=1234.4343412; - System.out.println(dbl); - - // characters - - char grade='A'; // single character in single quotes - '' - System.out.println(grade); - - String name="Harish"; // String is non-premitive type and multiple characters with double quotes - "" - System.out.println(name); - - //char ch='abc'; //invalid - //Stirng ch='abc';//invalid - //String ch='A';//invalid - String ch="A"; //valid - System.out.println(ch); - - boolean bl=true; //allows only true or false - System.out.println(bl); - - boolean bl1=false;//allows only true or false - System.out.println(bl1); - - //boolean bl="true";//invalid - //boolean bl="false";//invalid - - //String bl=true;//invalid - String bl2="true";//valid - - //---------------------------------------------------------------------- - - //difference between variable and const/final - int x=100; - System.out.println(x); - x=200; - System.out.println(x); - - final int y=300; - System.out.println(y); - // y=400;// not allowed - //System.out.println(y); - - // variables declaration without final keyword we can change the values but wiht final keyword we cnat chaneg the value. in javascript instea dof final, const keyword is used. - - //java is statistically typed programming language - //python is dynamically typed programming language - //javascript is dynamically typed scripting language - - //Example : - - int q=100; - //x="welcome";//it is not allowed in java but in javascript and python it is allowed - //in python or javascript we no need to specify the data types explicitly. - - /* - r=100; - r="welcome"; - */ - //it is allowed in dynamically typed programming language - javascript / python + + // Numeric Data Types + + int a=100, b=200; + System.out.println("The Value of a is :"+a); + System.out.println("The Value of b is :"+b); + System.out.println(a+b); + System.out.println("The sum of an and b is : "+(a+b)); + + byte by=125; + System.out.println(by); + + short sh=32767; + System.out.println(sh); + + long l=21212121234443534L; // literal is needed + // L should add, it can be lower or uppercase + System.out.println(l); + + // Decimal numbers - float, double + + float item_price=15.5f; // literal is needed + //f should add, it can be lower or uppercase + System.out.println(item_price); + + double dbl=1234.4343412; + System.out.println(dbl); + + // characters + + char grade='A'; // single character in single quotes - '' + System.out.println(grade); + + String name="Harish"; // String is non-premitive type and multiple characters with double quotes - "" + System.out.println(name); + + //char ch='abc'; //invalid + //Stirng ch='abc';//invalid + //String ch='A';//invalid + String ch="A"; //valid + System.out.println(ch); + + boolean bl=true; //allows only true or false + System.out.println(bl); + + boolean bl1=false;//allows only true or false + System.out.println(bl1); + + //boolean bl="true";//invalid + //boolean bl="false";//invalid + + //String bl=true;//invalid + String bl2="true";//valid + +//---------------------------------------------------------------------- + + //difference between variable and const/final + int x=100; + System.out.println(x); + x=200; + System.out.println(x); + + final int y=300; + System.out.println(y); + // y=400;// not allowed + //System.out.println(y); + + // variables declaration without final keyword we can change the values but wiht final keyword we cnat chaneg the value. in javascript instea dof final, const keyword is used. + + //java is statistically typed programming language + // python is dynamically typed programming language + //javascript is dynamically typed scripting language + + //Example : + + int q=100; + //x="welcome";//it is not allowed in java but in javascript and python it is allowed + //in python or javascript we no need to specify the data types explicitly. + + /* + r=100; + r="welcome"; + */ + //it is allowed in dynamically typed programming language - javascript / python } diff --git a/src/coreJava/DecrementOperators.java b/src/coreJava/DecrementOperators.java new file mode 100644 index 0000000..7540ed5 --- /dev/null +++ b/src/coreJava/DecrementOperators.java @@ -0,0 +1,39 @@ +package coreJava; + +public class DecrementOperators { + + public static void main(String[] args) { + //5.Decrement operators -- + + //case1 + int a=10; + System.out.println(a); + + // a=a-1; + //instead of writing 2 times "a" we can use below code + a--; + System.out.println(a); + + //case2 post decrement + + int b=10; + int res=b--; + System.out.println(res); + + //expected o/p is 11 but we received 10 because first 10 is assigned to res then value is decrement with a++ + //first assignment is happen then decrement will happen + + System.out.println(a); + + //case3 Pre increment + + int c=10; + int res1=--c; + System.out.println(res1); + // first decrement the value then assignment is happen + + System.out.println(c); + + } + +} diff --git a/src/coreJava/IncrementOperator.java b/src/coreJava/IncrementOperator.java new file mode 100644 index 0000000..de2d49e --- /dev/null +++ b/src/coreJava/IncrementOperator.java @@ -0,0 +1,40 @@ +package coreJava; + +public class IncrementOperator { + + public static void main(String[] args) { + + //4.Increment & Decrement operators ++ -- + + //case1 + int a=10; + System.out.println(a); + + // a=a+1; + //instead of writing 2 times "a" we can use below code + a++; + System.out.println(a); + + //case2 post increment + + int b=10; + int res=b++; + System.out.println(res); + + //expected o/p is 11 but we received 10 because first 10 is assigned to res then value is incremented with a++ + //first assignment is happen then increment will happen + + System.out.println(a); + + //case3 Pre increment + + int c=10; + int res1=++c; + System.out.println(res1); + // first increment the value then assignment is happen + + System.out.println(c); + + } + +} diff --git a/src/coreJava/Operators.java b/src/coreJava/Operators.java new file mode 100644 index 0000000..0781f2b --- /dev/null +++ b/src/coreJava/Operators.java @@ -0,0 +1,67 @@ +package coreJava; + +public class Operators { + + public static void main(String[] args) { + + //1. Arithmetic operators +-*/% + // work on numeric type data only + + int a=20, b=10; + + int result=a+b; + + System.out.println("Sum of a and b:"+result); + System.out.println("Sub of a and b:"+(a-b)); + System.out.println("Multiply of a and b:"+(a*b)); + System.out.println("Div of a and b:"+(a/b)); + System.out.println("Percentage of a and b:"+(a%b)); + + //2. Relational/comparison operators > >= < <= != == + // return boolean value + //we can use all kinds data types + + System.out.println(a>b); + System.out.println(a=b); + System.out.println(a<=b); + + b=20; + + System.out.println(a>=b); + System.out.println(a<=b); + + System.out.println(a!=b); + System.out.println(a==b); + + boolean res=a>b; + System.out.println(res); + + //3.Logical operators && || ! + //returns boolean value - true/false + //works between 2 boolean values + + boolean x=true; + boolean y=false; + System.out.println(x && y);// false + System.out.println(x || y); //true + System.out.println(!x);//true + System.out.println(!y);//false + + + boolean b1=10>20; + System.out.println(b1);//false + + boolean b2=20>10; + System.out.println(b2);//true + + System.out.println(b1 && b2);//false + System.out.println(b1 || b2);//true + + //combination of relational and logical operators + + System.out.println((10<20) && (20>10));//true + + } + +} diff --git a/src/coreJava/TernaryOperator.java b/src/coreJava/TernaryOperator.java new file mode 100644 index 0000000..c2efb24 --- /dev/null +++ b/src/coreJava/TernaryOperator.java @@ -0,0 +1,28 @@ +package coreJava; + +public class TernaryOperator { + + public static void main(String[] args) { + + //Syntax: var=exp ? result1 : result2; + + //ex1: + int a=200, b=100; + int x=(a>b)? a:b; + System.out.println(x); + + //ex2: + int y=(1==1)?100:200; + System.out.println(y); + + //ex3: + int z=(1==2)?100:200; + System.out.println(z); + + //ex4: + int person_age=30; + String res=(person_age>=18)?"Eligible":"Not Eligible"; + System.out.println(res); + } + +} diff --git a/src/loopingORIterativeStatements/BreakStatement.java b/src/loopingORIterativeStatements/BreakStatement.java new file mode 100644 index 0000000..128a6cf --- /dev/null +++ b/src/loopingORIterativeStatements/BreakStatement.java @@ -0,0 +1,18 @@ +package loopingORIterativeStatements; + +public class BreakStatement { + + public static void main(String[] args) { + + for(int i=1; i<=10; i++) + { + if(i==5) + { + break; + + } + System.out.println(i); + } + } + +} diff --git a/src/loopingORIterativeStatements/ContinueStatement1.java b/src/loopingORIterativeStatements/ContinueStatement1.java new file mode 100644 index 0000000..11f566c --- /dev/null +++ b/src/loopingORIterativeStatements/ContinueStatement1.java @@ -0,0 +1,19 @@ +package loopingORIterativeStatements; + +public class ContinueStatement1 { + + public static void main(String[] args) { + + for(int i=1; i<=10; i++) + { + if(i==3 || i==4 || i==5) + { + continue; + + } + System.out.println(i); + } + + } + +} diff --git a/src/loopingORIterativeStatements/ContniueStament.java b/src/loopingORIterativeStatements/ContniueStament.java new file mode 100644 index 0000000..49e9d59 --- /dev/null +++ b/src/loopingORIterativeStatements/ContniueStament.java @@ -0,0 +1,17 @@ +package loopingORIterativeStatements; + +public class ContniueStament { + + public static void main(String[] args) { + for(int i=1; i<=10; i++) + { + if(i==5) + { + continue; + + } + System.out.println(i); + } + } + +} diff --git a/src/loopingORIterativeStatements/DoWhileLoop1.java b/src/loopingORIterativeStatements/DoWhileLoop1.java new file mode 100644 index 0000000..6a636a6 --- /dev/null +++ b/src/loopingORIterativeStatements/DoWhileLoop1.java @@ -0,0 +1,17 @@ +package loopingORIterativeStatements; + +public class DoWhileLoop1 { + + public static void main(String[] args) { + + // 10...1 numbers + + int i=10; + + do { + System.out.println(i); + i--; + }while(i>0); // i>=1 + } + +} diff --git a/src/loopingORIterativeStatements/DowhileLoopo.java b/src/loopingORIterativeStatements/DowhileLoopo.java new file mode 100644 index 0000000..8ff6cca --- /dev/null +++ b/src/loopingORIterativeStatements/DowhileLoopo.java @@ -0,0 +1,18 @@ +package loopingORIterativeStatements; + +public class DowhileLoopo { + + public static void main(String[] args) { + + // 1...10 numbers + + int i=1; + do + { + System.out.println(i); + i++; + }while(i<=10); + + } + +} diff --git a/src/loopingORIterativeStatements/ForLoop.java b/src/loopingORIterativeStatements/ForLoop.java new file mode 100644 index 0000000..376ec09 --- /dev/null +++ b/src/loopingORIterativeStatements/ForLoop.java @@ -0,0 +1,14 @@ +package loopingORIterativeStatements; + +public class ForLoop { + + public static void main(String[] args) { + + //1....10 numbers + + for(int i=1; i<=10; i++) { + System.out.println(i); + } + } + +} diff --git a/src/loopingORIterativeStatements/ForLoop1.java b/src/loopingORIterativeStatements/ForLoop1.java new file mode 100644 index 0000000..50c108f --- /dev/null +++ b/src/loopingORIterativeStatements/ForLoop1.java @@ -0,0 +1,14 @@ +package loopingORIterativeStatements; + +public class ForLoop1 { + + public static void main(String[] args) { + // 1...10 even numbers + for(int i=2; i<=10; i+=2) { + System.out.println(i); + + } + + } + +} diff --git a/src/loopingORIterativeStatements/ForLoop2.java b/src/loopingORIterativeStatements/ForLoop2.java new file mode 100644 index 0000000..603f1f3 --- /dev/null +++ b/src/loopingORIterativeStatements/ForLoop2.java @@ -0,0 +1,23 @@ +package loopingORIterativeStatements; + +public class ForLoop2 { + + public static void main(String[] args) { + + // 1....10 even or odd numbers + + for(int i=1; i<=10; i++) + { + if(i%2==0) + { + System.out.println(i + "Even"); + } + else + { + System.out.println(i + "Odd"); + } + } + + } + +} diff --git a/src/loopingORIterativeStatements/ForLoop3.java b/src/loopingORIterativeStatements/ForLoop3.java new file mode 100644 index 0000000..c20da9b --- /dev/null +++ b/src/loopingORIterativeStatements/ForLoop3.java @@ -0,0 +1,16 @@ +package loopingORIterativeStatements; + +public class ForLoop3 { + + public static void main(String[] args) { + + // 1.....10 in descending order + + for(int i=10; i>0; i--) + { + System.out.println(i); + } + } + +} + \ No newline at end of file diff --git a/src/loopingORIterativeStatements/WhileLoop.java b/src/loopingORIterativeStatements/WhileLoop.java new file mode 100644 index 0000000..fa88aea --- /dev/null +++ b/src/loopingORIterativeStatements/WhileLoop.java @@ -0,0 +1,28 @@ +package loopingORIterativeStatements; + +public class WhileLoop { + + public static void main(String[] args) { + + int i=1; + + while(i<=10) // condition + { + System.out.println(i); //statement + i++; // increment + } + + + } + +} + + /* condition is true that while loop is executed , condition is false loop will be exited + * if we are not increment the condition i value always 1 is printed in infinite loop + * + * + * + * */ + + + diff --git a/src/loopingORIterativeStatements/WhileLoop1.java b/src/loopingORIterativeStatements/WhileLoop1.java new file mode 100644 index 0000000..4956f03 --- /dev/null +++ b/src/loopingORIterativeStatements/WhileLoop1.java @@ -0,0 +1,16 @@ +package loopingORIterativeStatements; + +public class WhileLoop1 { + public static void main(String[] args) { + // Hello message 10 times + int i=1; + + while(i<=10) + { + System.out.println("Hello"); + i++; + } + } + +} + diff --git a/src/loopingORIterativeStatements/WhileLoop2.java b/src/loopingORIterativeStatements/WhileLoop2.java new file mode 100644 index 0000000..258f26d --- /dev/null +++ b/src/loopingORIterativeStatements/WhileLoop2.java @@ -0,0 +1,33 @@ +package loopingORIterativeStatements; + +public class WhileLoop2 { + + public static void main(String[] args) { + + // print even numbers between 1...10 + + //Approach1: + + /* int i=2; + while(i<=10) + { + System.out.println(i); + i+=2; // i=i+2; + } */ + + //Approach2: + + int i=2; + while(i<=10) // how many times we need to run the loop + { + if(i%2==0) // it is filter the while condition value + { + System.out.println(i); + } + i++; + } + + + } + +} diff --git a/src/loopingORIterativeStatements/WhileLoopWithoutCondition.java b/src/loopingORIterativeStatements/WhileLoopWithoutCondition.java new file mode 100644 index 0000000..39de174 --- /dev/null +++ b/src/loopingORIterativeStatements/WhileLoopWithoutCondition.java @@ -0,0 +1,22 @@ +package loopingORIterativeStatements; + +public class WhileLoopWithoutCondition { + + public static void main(String[] args) { + + int i=1; + + while(true) + { + System.out.println("Hello"); + i++; + + if(i==10) + { + break; + } + } + + } + +} diff --git a/src/loopingORIterativeStatements/Whileloop4.java b/src/loopingORIterativeStatements/Whileloop4.java new file mode 100644 index 0000000..03acee9 --- /dev/null +++ b/src/loopingORIterativeStatements/Whileloop4.java @@ -0,0 +1,17 @@ +package loopingORIterativeStatements; + +public class Whileloop4 { + + public static void main(String args[]) { + + // print 10.......1 in descending order + + int i=10; + while(i>0)// (i>=1) + { + System.out.println(i); + i--; + } + + } +} diff --git a/src/loopingORIterativeStatements/Whilelopp3.java b/src/loopingORIterativeStatements/Whilelopp3.java new file mode 100644 index 0000000..acf5fca --- /dev/null +++ b/src/loopingORIterativeStatements/Whilelopp3.java @@ -0,0 +1,25 @@ +package loopingORIterativeStatements; + +public class Whilelopp3 { + + public static void main(String[] args) { + /* odd ..even..odd..even..odd */ + + int i=1; + while(i<=10) + { + if(i%2==0) + { + System.out.println(i + "Even"); + } + else + { + System.out.println(i + "Odd"); + } + i++; + } + + + } + +} diff --git a/src/module-info.java b/src/module-info.java deleted file mode 100644 index 46803c2..0000000 --- a/src/module-info.java +++ /dev/null @@ -1,9 +0,0 @@ -/** - * - */ -/** - * @author uvhar - * - */ -module coreJava { -} \ No newline at end of file diff --git a/src/programs/CountEvenAndOddDigits.java b/src/programs/CountEvenAndOddDigits.java new file mode 100644 index 0000000..db1280c --- /dev/null +++ b/src/programs/CountEvenAndOddDigits.java @@ -0,0 +1,31 @@ +package programs; + +public class CountEvenAndOddDigits { + + public static void main(String[] args) { + + int num=12345; + int even_count=0; + int odd_count=0; + + while(num>0) + { + int rem=num%10; + + if(rem%2==0) + { + even_count++; + } + else + { + odd_count++; + } + + num=num/10; + } + System.out.println("number of even numbers:"+ even_count); + System.out.println("number of odd numbers:"+ odd_count); + + } + +} diff --git a/src/programs/CountNumberOfDigits.java b/src/programs/CountNumberOfDigits.java new file mode 100644 index 0000000..4c21832 --- /dev/null +++ b/src/programs/CountNumberOfDigits.java @@ -0,0 +1,18 @@ +package programs; + +public class CountNumberOfDigits { + + public static void main(String[] args) { + + int num=123456789; + int count=0; + + while(num>0) + { + num=num/10; + count++; + } + System.out.println("Number of Digits in a Nuumber:"+ count); + } + +} diff --git a/src/programs/PalindromeNumber.java b/src/programs/PalindromeNumber.java new file mode 100644 index 0000000..1b23c50 --- /dev/null +++ b/src/programs/PalindromeNumber.java @@ -0,0 +1,28 @@ +package programs; + +public class PalindromeNumber { + + public static void main(String[] args) { + + int num=121; + int org_num=num; + + int rev=0; + + while(num!=0) + { + rev =rev*10+num%10; + num=num/10; + } + if(org_num==rev) + { + System.out.println(org_num+ "Palindrome Number"); + } + else + { + System.out.println(org_num+ "Not a Palindrome Number"); + } + + } + +} diff --git a/src/programs/ReverseANumber.java b/src/programs/ReverseANumber.java new file mode 100644 index 0000000..bcd7971 --- /dev/null +++ b/src/programs/ReverseANumber.java @@ -0,0 +1,20 @@ +package programs; + +public class ReverseANumber { + + public static void main(String[] args) { + + int num=98765; + int rev=0; + + while(num!=0) + { + + rev = rev*10 + num%10; + num = num/10; + } + + System.out.println("Reverse number is:"+ rev); + } + +} diff --git a/src/programs/ReverseNumberWithStringBuffer.java b/src/programs/ReverseNumberWithStringBuffer.java new file mode 100644 index 0000000..448b957 --- /dev/null +++ b/src/programs/ReverseNumberWithStringBuffer.java @@ -0,0 +1,15 @@ +package programs; + +public class ReverseNumberWithStringBuffer { + + public static void main(String[] args) { + + int num=1234; + + StringBuffer sb=new StringBuffer(String.valueOf(num)); + StringBuffer rev=sb.reverse(); + System.out.println("Reverse Number is:"+rev); + + } + +} diff --git a/src/programs/SumOfDigitsInANumber.java b/src/programs/SumOfDigitsInANumber.java new file mode 100644 index 0000000..f46f803 --- /dev/null +++ b/src/programs/SumOfDigitsInANumber.java @@ -0,0 +1,19 @@ +package programs; + +public class SumOfDigitsInANumber { + + public static void main(String[] args) { + + int num=1234; + int sum=0; + + while(num>0) + { + sum=sum+num%10; + num=num/10; + } + System.out.println("Sum of digitis in a Number:"+ sum); + + } + +}