Java Basic Programs JAVA Tutorials

JAVA Basic Programs With Solutions

JAVA Basic Programs

Hello friends, welcome to Tech Naandi Solutions. Java has become the most popular and best programming language in the software industry. In this post, we will learn the top 5 JAVA Basic Programs.

We are here to help you to learn JAVA programming language, on our website we will provide all tutorials that you need to learn JAVA.

In this post, I am providing you the most important and basic programs for you, which help you to get the basic knowledge about JAVA programming.

I restricted copying of programs from my website because if you copy and paste these programs you won’t get knowledge of programming, try to type and learn programs šŸ˜Š

The sum of two numbers in JAVA using Scanner Class

Example 1: Sum of Two Numbers

public class AddTwoNumbers
{
public static void main(String[] args)
{
int num1 = 5, num2 = 15, sum;
sum = num1 + num2;
System.out.println("Sum of these numbers: "+sum);
}
}

Output : Sum of two numbers is : 50

Example 2: Sum of Two Numbers Using Scanner Class

import java.util.Scanner; 
public class AddTwoNumbers2 
{ 
public static void main(String[] args)
{ 
int num1, num2, sum;
Scanner sc = new Scanner(System.in);
System.out.println("Enter First Number: ");
num1 = sc.nextInt();
System.out.println("Enter Second Number: ");
num2 = sc.nextInt();
sc.close();
sum = num1 + num2;
System.out.println("Sum of these numbers: "+sum);
}
}

Output :Ā 

Enter Number 1 : 75

Enter Number 2: 25

Sum of two numbers is : 100

JAVA Program to check Even or Odd Number

What is Even NumberĀ 

Even numbers are the numbers, that are exactly divisible by 2.All even numbers ends with 2,4,6,8 numbers, we can say that the number 234567894 is an even number because its ends with 2.šŸ˜Š

What is Odd NumberĀ 

Odd numbers are the numbers, that are not exactly divisible by 2.All odd numbers ends with 1,3,7,9, numbers, we can say that the number 2345678949 is an even number because it ends with 9.šŸ˜Š

Example Program

import java.util.Scanner;

class CheckEvenOdd
{
  public static void main(String args[])
  {
    int num;
    System.out.println("Enter any number:");

    //The given number by the user will store in num

    Scanner input = new Scanner(System.in);
    num = input.nextInt();

    /* If the number is divisible by 2 
     *we can say that it is an even number 
     * else it is an odd number*/

    if ( num % 2 == 0 )
        System.out.println("The Given number is even");
     else
        System.out.println("The Given number is odd");
  }
}

Output 1:

Enter Any Number: 344568

The Given number is Even

Output 2:

Enter Any Number: 3445689

The Given number is ODD

JAVA Program to check Leap Year

What is Leap Year

We all know that a common year has 365 days, but a leap year contains 366 days by adding a 1 extra day to the month of February. A leap year comes once every 4 years.

We use simple logic to find a leap year is that if any year that is exactly divided by 4, then we can call it as a leap year.

Leap Year Program

import java.util.Scanner;
public class TechNaandiSolutions
{
    public static void main(String[] args)
	{
    	int year;
    	Scanner scan = new Scanner(System.in);
    	System.out.println("Enter any Year:");
    	year = scan.nextInt();
    	scan.close();
        boolean isLeap = false;
        if(year % 4 == 0)
        {
        if( year % 100 == 0)
        {
        if ( year % 400 == 0)
            isLeap = true;
        else
            isLeap = false;
        }
        else
        isLeap = true;
        }
        else
		{
        isLeap = false;
        }
        if(isLeap==true)
            System.out.println(year + " is a Leap Year.");
        else
            System.out.println(year + " is not a Leap Year.");
    }
}

Output 1:

Enter Any Year: 2020

2020 is a leap year

Output 2 :

Enter Any Year: 2002

2002 is not a leap year

Java program to find Vowel or consonant

What are Vowel and consonant

Before going to program let us see what are these vowels and consonants. In an English Alphabets A E I O U are called as vowels and remaining alphabets are called as consonants

Now we are going to write a Java program to find the given alphabet is vowel or consonant by using switch case.

Program to find Vowel or Consonant

import java.util.Scanner;
class TechNaandiSolutions
{
   public static void main(String[ ] arg)
   {
	boolean isVowel=false;;
	Scanner scanner=new Scanner(System.in);
	System.out.println("Enter any Alphabet : ");
	char ch=scanner.next().charAt(0); 
	scanner.close();
	switch(ch)
	{
	   case 'a' :
	   case 'e' :
       case 'i' :
	   case 'o' :
	   case 'u' :
	   case 'A' :
	   case 'E' :
	   case 'I' :
	   case 'O' :
	   case 'U' : isVowel = true;
	}
	if(isVowel == true)
	{
	   System.out.println(ch+" is  a Vowel");
	}
	else
	{
	   if((ch>='a'&&ch<='z')||(ch>='A'&&ch<='Z'))
		System.out.println(ch+" is a Consonant");
	   else
		System.out.println("Please Enter only Alphabets");		
    }
   }
}

Output 1:

Enter Any Alphabet: A

A is a Vowel

Output 2:

Enter Any Alphabet: N

N is a Consonant

Output 3:

Enter Any Alphabet: 4

Please Enter only Alphabets

Write a JAVA program to find given STRING is Palindrome or not

What is PalindromeĀ 

A palindrome is nothing but a string or a number will remain the same even if you reverse the string/number.

Examples

121,141,191,………………………

Madam, Level, POP,………………………….

import java.util.Scanner;
public class Palindrome
{
static void checkPalindrome(String input)
{
boolean res = true; // to store result
int length = input.length();
for(int i=0; i<= length/2; i++)
{
if(input.charAt(i) != input.charAt(length-i-1))
{
res = false;
break;
}
}
System.out.println(input + " is palindrome = "+res);
}
public static void main(String[] args) 
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter Any Text: ");
String str = sc.nextLine();
//Calling Function
checkPalindrome(str);
}
}

OutPut :

Enter Any String: MADAM

MADAM is a Palindrome = true

Related posts

Overview Of Java Programming Language | Java 11th Edition concepts

Narendra Anja

8 comments

GODUGUCHINTHA VIKKY August 2, 2020 at 4:57 PM

iam very happy to learn this website for java learner bro

Reply
Narendra Anja August 2, 2020 at 7:00 PM

Hi Bro

Thank You, I will provide more concepts very soon.

Reply
Leonrad Garcia July 10, 2020 at 4:45 AM

Hi, this is Leonard.

Today I have Visited your site, very nice design and contain very useful Content, keep posting more topics.

Thank you
Leonard Garcia.

Reply
Narendra Anja July 10, 2020 at 10:15 AM

Hi Leonard,

Thanks for your support.

Reply
Vikram July 4, 2020 at 3:07 PM

It’s very helpful for me bro, keep posting more programs

Reply
Narendra Anja July 4, 2020 at 3:46 PM

Thanks for your support.

Reply
Somasekhar masala July 3, 2020 at 1:57 PM

Seriously it was nice & proud of you Narendra good website with and simple programs & algorithms, keep it up, and all the best for more concepts coming up.

Reply
Narendra Anja July 3, 2020 at 2:05 PM

Thanks for your support Somasekhar.I will come up with more useful content to help my users.

Reply

Leave a Comment

error: Sorry Friend , you are not allowed to copy my content. ~ Narendra Anja šŸ„°
Open chat
How can i help you ?