1
Basic programs
Adding of two numbers
public class Addoftwo {
public static void main(String[] args) {
int x = 5;
int y = 6;
int sum = x + y;
System.out.println(sum); // Print the sum of x + y
Average of Three Numbers using scanner class
import java.util.Scanner;
public class Averageofthree {
public static void main(String[] args) {
// Create a Scanner object to read input from the user
Scanner in = new Scanner(System.in);
// Prompt the user to input the first number
System.out.print("Input first number: ");
// Read and store the first number
int num1 = in.nextInt();
// Prompt the user to input the second number
System.out.print("Input second number: ");
// Read and store the second number
int num2 = in.nextInt();
// Prompt the user to input the third number
ISSR, ANITS, CSE(DS Department),Visakhapatnam
2
System.out.print("Input third number: ");
// Read and store the third number
int num3 = in.nextInt();
// Prompt the user to input the fourth number
System.out.print("Input fourth number: ");
// Read and store the fourth number
int num4 = in.nextInt();
// Prompt the user to input the fifth number
System.out.print("Enter fifth number: ");
// Read and store the fifth number
int num5 = in.nextInt();
// Calculate and print the average of the five numbers
System.out.println("Average of five numbers is: " + (num1 + num2 + num3 + num4 + num5) / 5);
}
}
Largest of two numbers in Java
public class Largest {
public static void main(String[] args) {
int n1 = 4, n2 = 3, n3 = 2;
if( n1 >= n2 && n1 >= n3)
System.out.println(n1 + " is the largest number.");
else if (n2 >= n1 && n2 >= n3)
System.out.println(n2 + " is the largest number.");
else
System.out.println(n3 + " is the largest number.");
}
}
Even or odd in Java
ISSR, ANITS, CSE(DS Department),Visakhapatnam
3
// Java Program to Check if Given Integer is Odd or Even
// Using Brute Forcew Approach
// Importing required classes
import java.io.*;
import java.util.Scanner;
// Main class
class Evenodd {
// Main Driver Method
public static void main(String[] args)
{
// Declaring and initializing integer variable
int num = 10;
// Checking if number is even or odd number
// via remainder
if (num % 2 == 0) {
// If remainder is zero then this number is even
System.out.println("Entered Number is Even");
}
else {
// If remainder is not zero then this number is
// odd
System.out.println("Entered Number is Odd");
}
}
}
If else in JAVA
public class IfElse {
public static void main(String[] args) {
int n = 10;
if (n > 5) {
System.out.println("The number is greater than 5.");
} else {
System.out.println("The number is 5 or less.");
}
}
ISSR, ANITS, CSE(DS Department),Visakhapatnam
4
Nested If in JAVA
public class NestedIf {
public static void main(String[] args) {
int a = 25;
double w = 65.5;
if (a >= 18) {
if (w >= 50.0) {
System.out.println("You are eligible to donate blood.");
} else {
System.out.println("You must weigh at least 50 kilograms to donate blood.");
}
} else {
System.out.println("You must be at least 18 years old to donate blood.");
}
}
}
Checking week name in JAVA
import java.util.Scanner;
public class Weekname
{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
System.out.print("Input number: ");
int day = in.nextInt();
String dayName = "";
switch (day)
{
case 1: dayName = "Monday"; break;
case 2: dayName = "Tuesday"; break;
case 3: dayName = "Wednesday"; break;
case 4: dayName = "Thursday"; break;
case 5: dayName = "Friday"; break;
case 6: dayName = "Saturday"; break;
case 7: dayName = "Sunday"; break;
ISSR, ANITS, CSE(DS Department),Visakhapatnam
5
default:dayName = "Invalid day range";
}
}}
Sum of natural numbers in Java
// from 1 to N Using For Loop and
// sum of First N Natural Number
import java.io.*;
class Naturalnumberssum {
// main function
public static void main(String[] args)
{
int N = 10;
int sum = 0;
System.out.println("Finding Sum of " + N + " numbers using for loop");
for (int i = 1; i <= N; i++) {
sum += i;
}
System.out.println("Sum of first " + N
+ " Natural Number = " + sum);
}
}
Fibonacci series in Java
// Calculate the Sum of First N Odd & Even Numbers in Java
import java.io.*;
public class Evenoddsum {
public static void main(String[] args)
{
int n = 8;
int evenSum = 0;
int oddSum = 0;
for (int i = 1; i <= 2 * n; i++) {
// check even & odd using Bitwise AND operator
ISSR, ANITS, CSE(DS Department),Visakhapatnam
6
if ((i & 1) == 0)
evenSum += i;
else
oddSum += i;
}
System.out.println("Sum of First " + n
+ " Even numbers = " + evenSum);
System.out.println("Sum of First " + n
+ " Odd numbers = " + oddSum);
}
}
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. The first two numbers of fibonacci series are 0 and 1.
There are two ways to write the fibonacci series program in java:
o Fibonacci Series without using recursion
o Fibonacci Series using recursion
Fibonacci Series in Java without using recursion
Let's see the fibonacci series program in java without using recursion.
1. class FibonacciExample1{
2. public static void main(String args[])
3. {
4. int n1=0,n2=1,n3,i,count=10;
5. System.out.print(n1+" "+n2);//printing 0 and 1
6.
7. for(i=2;i<count;++i)//loop starts from 2 because 0 and 1 are already printed
8. {
9. n3=n1+n2;
10. System.out.print(" "+n3);
11. n1=n2;
12. n2=n3;
ISSR, ANITS, CSE(DS Department),Visakhapatnam
7
13. }
14.
15. }}
Test it Now
Output:
0 1 1 2 3 5 8 13 21 34
Prime Number Program in Java
Prime number in Java: Prime number is a number that is greater than 1 and divided by 1
or itself only. In other words, prime numbers can't be divided by other numbers than itself
or 1. For example 2, 3, 5, 7, 11, 13, 17.... are the prime numbers.
Note: 0 and 1 are not prime numbers. The 2 is the only even prime number because all the
other even numbers can be divided by 2.
Let's see the prime number program in java. In this java program, we will take a number
variable and check whether the number is prime or not.
1. public class PrimeExample{
2. public static void main(String args[]){
3. int i,m=0,flag=0;
4. int n=3;//it is the number to be checked
5. m=n/2;
6. if(n==0||n==1){
7. System.out.println(n+" is not prime number");
8. }else{
9. for(i=2;i<=m;i++){
10. if(n%i==0){
11. System.out.println(n+" is not prime number");
12. flag=1;
13. break;
ISSR, ANITS, CSE(DS Department),Visakhapatnam
8
14. }
15. }
16. if(flag==0) { System.out.println(n+" is prime number"); }
17. }//end of else
18. }
19. }
Test it Now
Output:
3 is prime number
Palindrome Program in Java
Palindrome number in java: A palindrome number is a number that is same after reverse.
For example 545, 151, 34543, 343, 171, 48984 are the palindrome numbers. It can also be
a string like LOL, MADAM etc.
Palindrome number algorithm
o Get the number to check for palindrome
o Hold the number in temporary variable
o Reverse the number
o Compare the temporary number with reversed number
o If both numbers are same, print "palindrome number"
o Else print "not palindrome number"
1. class PalindromeExample{
2. public static void main(String args[]){
3. int r,sum=0,temp;
4. int n=454;//It is the number variable to be checked for palindrome
5.
6. temp=n;
7. while(n>0){
ISSR, ANITS, CSE(DS Department),Visakhapatnam
9
8. r=n%10; //getting remainder
9. sum=(sum*10)+r;
10. n=n/10;
11. }
12. if(temp==sum)
13. System.out.println("palindrome number ");
14. else
15. System.out.println("not palindrome");
16. }
17. }
Or
1. import java.util.*;
2. class PalindromeExample2
3. {
4. public static void main(String args[])
5. {
6. String original, reverse = ""; // Objects of String class
7. Scanner in = new Scanner(System.in);
8. System.out.println("Enter a string/number to check if it is a palindrome");
9. original = in.nextLine();
10. int length = original.length();
11. for ( int i = length - 1; i >= 0; i-- )
12. reverse = reverse + original.charAt(i);
13. if (original.equals(reverse))
14. System.out.println("Entered string/number is a palindrome.");
15. else
16. System.out.println("Entered string/number isn't a palindrome.");
17. }
18. }
ISSR, ANITS, CSE(DS Department),Visakhapatnam
10
Factorial Program in Java
Factorial Program in Java: Factorial of n is the product of all positive descending integers.
Factorial of n is denoted by n!. For example:
1. 4! = 4*3*2*1 = 24
2. 5! = 5*4*3*2*1 = 120
Here, 4! is pronounced as "4 factorial", it is also called "4 bang" or "4 shriek".
The factorial is normally used in Combinations and Permutations (mathematics).
There are many ways to write the factorial program in java language. Let's see the 2 ways
to write the factorial program in java.
o Factorial Program using loop
o Factorial Program using recursion
Factorial Program using loop in java
Let's see the factorial Program using loop in java.
1. class FactorialExample{
2. public static void main(String args[]){
3. int i,fact=1;
4. int number=5;//It is the number to calculate factorial
5. for(i=1;i<=number;i++){
6. fact=fact*i;
7. }
8. System.out.println("Factorial of "+number+" is: "+fact);
9. }
10. }
Output:
Factorial of 5 is: 120
ISSR, ANITS, CSE(DS Department),Visakhapatnam
11
Armstrong Number in Java
In this section, we will discuss what is Armstrong number and also create Java
programs to check if the given number is an Armstrong number or not. The Armstrong
number program frequently asked in Java coding interviews and academics.
Armstrong Number
An Armstrong number is a positive m-digit number that is equal to the sum of the
mth powers of their digits. It is also known as pluperfect, or Plus Perfect,
or Narcissistic number. It is an OEIS sequence A005188. Let’s understand it through an
example.
Armstrong Number Example
1: 11 = 1
2: 21 = 2
3: 31 = 3
153: 13 + 53 + 33 = 1 + 125+ 27 = 153
ISSR, ANITS, CSE(DS Department),Visakhapatnam
12
125: 13 + 23 + 53 = 1 + 8 + 125 = 134 (Not an Armstrong Number)
1634: 14 + 64 + 34 + 44 = 1 + 1296 + 81 + 256 = 1643
Similarly, we can check other number also.
The first few Armstrong numbers between 0 to 999 are 1, 2, 3, 4, 5, 6, 7, 8, 9, 153, 370,
371, 407. Some other Armstrong numbers are 1634, 8208, 9474, 54748, 92727, 93084,
548834, 1741725, 4210818, 9800817, 9926315, 24678050, 24678051, 88593477,
146511208, 472335975, 534494836, 912985153, 4679307774, 32164049650,
32164049651.
Note that there is no two-digit Armstrong number.
Armstrong Number Java Program
The following Java program prints all the Armstrong numbers up to the specified limit.
ArmstrongNumberExample1.java
1. import java.util.Scanner;
2. import java.lang.Math;
3. public class ArmstsrongNumberExample
4. {
5. //function to check if the number is Armstrong or not
6. static boolean isArmstrong(int n)
7. {
8. int temp, digits=0, last=0, sum=0;
9. //assigning n into a temp variable
10. temp=n;
11. //loop execute until the condition becomes false
12. while(temp>0)
13. {
14. temp = temp/10;
15. digits++;
16. }
17. temp = n;
18. while(temp>0)
ISSR, ANITS, CSE(DS Department),Visakhapatnam
13
19. {
20. //determines the last digit from the number
21. last = temp % 10;
22. //calculates the power of a number up to digit times and add the resultant to the sum v
ariable
23. sum += (Math.pow(last, digits));
24. //removes the last digit
25. temp = temp/10;
26. }
27. //compares the sum with n
28. if(n==sum)
29. //returns if sum and n are equal
30. return true;
31. //returns false if sum and n are not equal
32. else return false;
33. }
34. //driver code
35. public static void main(String args[])
36. {
37. int num;
38. Scanner sc= new Scanner(System.in);
39. System.out.print("Enter the limit: ");
40. //reads the limit from the user
41. num=sc.nextInt();
42. System.out.println("Armstrong Number up to "+ num + " are: ");
43. for(int i=0; i<=num; i++)
44. //function calling
45. if(isArmstrong(i))
46. //prints the armstrong numbers
47. System.out.print(i+ ", ");
48. }
49. }
Output:
ISSR, ANITS, CSE(DS Department),Visakhapatnam
14
Enter the limit: 999
Armstrong Number up to 999 are:
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 153, 370, 371, 407
ISSR, ANITS, CSE(DS Department),Visakhapatnam