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
Also Read Our Trending Articles >>
- Best Udemy python course | Udemy python for data science | python 3
- Who is the best dancer in Tollywood
- Buy Headphones & Earphones Online at Best Price
- Upcoming Telugu movies on amazon prime | Upcoming Telugu movies | Amazon prime movies list
- Pega 8.5 Features | What’s New in Pega Platform | Pega New Features
- Download eclipse IDE | Eclipse Downloads | How do I download and install Eclipse IDE
- How to type Indian Rupee symbol from the keyboard in Windows | How do you insert the currency symbol in Word
- How many emails can you send from Gmail | Gmail Sending Limits
- How to Factory Reset your Gmail Account | How to reset my Gmail settings
- How can I find a person by email | How to Find the Person Name Behind Gmail Address
8 comments
iam very happy to learn this website for java learner bro
Hi Bro
Thank You, I will provide more concepts very soon.
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.
Hi Leonard,
Thanks for your support.
It’s very helpful for me bro, keep posting more programs
Thanks for your support.
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.
Thanks for your support Somasekhar.I will come up with more useful content to help my users.