Java Programming for Beginners
Acquiring programming skills entails mastering the art of problem-solving through code. These introductory programming examples are thoughtfully designed for beginners who are new to coding. The collection comprises a series of gradually simplified exercises, catering to those at the early stages of their learning journey. By completing these examples, learners will gain proficiency in fundamental Java programming concepts and enhance their logical thinking capabilities. To make substantial advancements in programming, it is crucial to diligently test your code at an early stage and with comprehensive rigor.
How to Find the Sum of Two Numbers in Java?
public class TestClass{
public static void main(String[] args) {
int x= 10;
int y= 20;
int sum =0;
sum = x+y;
System.out.println("Sum of numbers : "+sum);
}
}
Find the average of the numbers in Java
public class TestClass{
public static void main(String[] args) {
int[] numbers = new int[]{10,20,30,40,50};
int sum = 0;
for(int i=0; i < numbers.length ; i++)
sum = sum + numbers[i];
double avg = sum / numbers.length;
System.out.println("Average of numbers : "+avg);
}
}
Check whether number is even or odd in Java?
The MODULUS Operator %
The modulus operator calculates the remainder of the division between its first operand and the second. When using the % operator, it returns the remainder of two numbers. For example, 10 % 3 equals 1 because dividing 10 by 3 results in a remainder of 1. This operator can be used similarly to more familiar operators like addition (+) or subtraction (-) in programming.
public class TestClass{
public static void main(String[] args) {
int x= 23;
if(x%2 == 0){
System.out.println("The number is Even");
}
else{
System.out.println("The number is Odd");
}
}
}
Reverse a string in Java
public class TestClass{
public static void main(String[] args) {
String str = "TUTORIAL";
char[] chr = str.toCharArray();
int begin=0;
int end=chr.length-1;
char temp;
while(end>begin){
temp = chr[begin];
chr[begin]=chr[end];
chr[end] = temp;
end--;
begin++;
}
System.out.println(new String(chr));
}
}
You can do this in another simple way by using StringBuilder Class in Java
public class TestClass{
public static void main(String[] args) {
String str = "TUTORIAL";
String resStr = new StringBuilder(str).reverse().toString();
System.out.println(resStr);
}
}
Check whether string is palindrome or not in Java
public class TestClass{
public static void main(String[] args) {
String str = "ABCBA";
boolean val = false;
int n = str.length();
for( int i = 0; i < n/2; i++ ){
if (str.charAt(i) != str.charAt(n-i-1)){
val = false;
break;
}else{
val = true;
}
}
if(val)
System.out.println("String is Palindrome");
else
System.out.println("String is not Palindrome");
}
}
You can do the same in another simple way
public class TestClass{
public static void main(String[] args) {
String str = "ABCBA";
if(str.equals(new StringBuilder(str).reverse().toString()))
System.out.println("String is Palindrome");
else
System.out.println("String is not Palindrome");
}
}
Find the sum of all the numbers in an array in Java
public class TestClass{
public static void main(String[] args) {
int intArray[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
int sum = 0;
for (int x : intArray)
sum += x;
System.out.println("Sum of array elements : "+sum);
}
}
Calculate Area of Triangle in Java
Area of a triangle can be calculated using the following formula, Area = (b*h)/2, where b is the base of the triangle and h is the vertical height of the triangle.
public class TestClass{
public static void main(String[] args) {
double tBase = 35.0;
double tHeight = 210.5;
double tArea = (tBase* tHeight)/2;
System.out.println("Area of Triangle is: " + tArea);
}
}
Fibonacci Series in Java
In fibonacci series, next number is the sum of previous two numbers for example 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55 etc. Here set the limit as 10(int limit=10), you can change it as much as you want.
public class TestClass{
public static void main(String[] args) {
int limit = 10; //set the limit
int x = -1;
int y = 1;
int z = 0;
for(int i=1; i<=limit; i++) {
z = x + y;
int temp = x;
x = y;
y = z;
System.out.println(z);
}
}
}
Find duplicate characters in a String in Java
The following example find duplicate characters in a String and count the number of occurances using Java
public class TestClass{
public static void main(String[] args) {
String input = "java tutorial";
int cnt=0;
int strLen=0;
do{
try{
char chrInput[]=input.toCharArray();
strLen=chrInput.length;
cnt=0;
for(int j=0;j<strLen;j++){
if((chrInput[0]==chrInput[j])&&((chrInput[0]>=65&&chrInput[0]<=91)(chrInput[0]>=97&&chrInput[0]<=123)))
cnt++;
}
if(cnt!=0)
System.out.println(chrInput[0]+" "+cnt+" Times");
input=input.replace(""+chrInput[0],"");
}catch(Exception ex){
ex.printStackTrace();
}
}while(strLen!=1);
}
}
Convert binary to decimal number in Java
public class TestClass{
public static void main(String[] args) {
String str = "100";
double j=0;
for(int i=0;i<str.length();i++){
if(str.charAt(i)== '1'){
j=j+ Math.pow(2,str.length()-1-i);
}
}
System.out.println("Decimal Number is :" + j);
}
}
Decimal to Binary Conversion in Java
public class TestClass{
public static void main(String[] args) {
int input = 55, i = 0, temp[] = new int[10];
int bArray[];
while (input > 0) {
temp[i++] = input % 2;
input /= 2;
}
bArray = new int[i];
int k = 0;
for (int j = i - 1; j >= 0; j--) {
bArray[k++] = temp[j];
System.out.print(temp[j]);
}
}
}
Largest and Smallest Number from an Array in Java
public class TestClass{
public static void main(String[] args) {
int input[] = new int[]{200,10,100,90,20,80,150};
int smallNum = input[0];
int largeNum = input[0];
for(int i=1; i< input.length; i++){
if(input[i] > largeNum)
largeNum = input[i];
else if (input[i] < smallNum)
smallNum = input[i];
}
System.out.println("Largest Number is : " + largeNum);
System.out.println("Smallest Number is : " + smallNum);
}
}