Java basic Programs with Explanation

Java Basic Program, Here the lists of programs

1. Java basic Programs- To Find the Even numbers up to given input value.



Explanation: Suppose you want to find a list of even numbers up to given input lets say 100, So this program will display even numbers till 100.
Logic: if No%2==0 then its even
let say No=100
100%2==0 (remainder =0)
Hence it is even Number!

/*
list of Even Number Java Example*/
import java.util.*;
import java.lang.*;
import java.Util.Scanner;

public class EvenNumberList
{
  public static void main(String args[])
  {
  int limit;// upto limit
  System.out.println("Enter your Limit value -");
  Scanner Sc=new Scanner(System.in);// used to scan the input from user
  limit=Sc.nextInt();
  System.out.println("Even Number List Between 1 to" +limit);
  for(int i=1;i<=limit;i++)
    {
      if(i%2==0)
     {
      System.out.print(i +" " + "|");
     }
    }
  }
}
Output:
Enter the limit value
50
Even Number List Between 1 to 50
2|4|6|8|10|12|14|16|18|20|22|24|26|28|30|32|34|36|38|40|42|44|46|48|50|

2. Java basic Programs To Find the Odd numbers up to given input value?

Explanation: Suppose you want to find list of Odd number up to given input lets say 100,So this program will display Odd numbers till 100.
Logic: if No%2!=0 then its even
let say No=99
99%2!=0 (remainder =1)
Hence it 99 is odd Number!

/*
list of Odd Number Java Example*/
import java.util.*;
import java.lang.*;
import java.Util.Scanner;

public class OddNumberList
{
  public static void main(String args[])
  {
  int limit;// upto limit
  System.out.println("Enter your Limit value -");
  Scanner Sc=new Scanner(System.in);// used to scan the input from user
  limit=Sc.nextInt();
  System.out.println("Odd Number List Between 1 to" +limit);
  for(int i=1;i<=limit;i++)
    {
      if(i%2!=0)// odd logic
     {
      System.out.print(i +" " + "|");
     }
    }
  }
}
Output:
Enter your Limit value -
50
Odd Number List Between 1 to50
1 |3 |5 |7 |9 |11 |13 |15 |17 |19 |21 |23 |25 |27 |29 |31 |33 |35 |37 |39 |41 |43 |45 |47 |49 |

3. Java Program To Calculate Percentage of Student?

Explanation: Suppose you want to calculate percentage of Students in examination.
Logic: percentage=(marks obtained/total marks)*100
In this example, we have taken array to enter marks and thus calculated array sum to find total marks obtained, then we used percentage logic to calculate percentage!

/* 
Program to calculate percentage obtained */
import java.util.Scanner;
import java.util.*;
public class Main {
public static void main(String[] args)
     {
        int size, sum = 0;
        System.out.print("Enter no. of Subjects:-");
        Scanner s = new Scanner(System.in);
        size = s.nextInt();
         int a[] = new int[size];
        System.out.println("Enter marks of all Subjects-:");

        for(int i = 0; i < size; i++)
        {     
            a[i] = s.nextInt();
            sum = sum + a[i];
         }
        System.out.println("Sum:"+sum);
        int xyz=sum;
        float percentage;
        System.out.println("Enter the total marks :");
        percentage=(xyz/size)*100;
        float  et=percentage/100;
        System.out.println("Percentage:"+et +"%");
    }
}
Output:
Enter no. of Subjects:-:5
Enter marks of all Subjects:-:
100 90 85 80 95
Sum:450
Enter the total marks :
Percentage:90.0

4. Java Program to determine Palindrome Number?

Palindrome Number are the Numbers when we read them from left to right or vice versa they appears same
For 121, 141, 232 etc…

/* 
Java program to determine Palindrome*/
import java.util.*;
import java.util.Scanner;
public class palindrome
{
  public static void main(String args[])
  { 
   int remainder,sum=0,temp;
   System.out.println("Enter the Number:");
   Scanner sc=new Scanner(System.in);
   int number=sc.nextInt();
   temp=number;//storing our actual number 
   while(number>0)
   {
    remainder=number%10;
    sum=(sum*10)+remainder;
    number=number/10;
   }
   if(temp==sum)
   System.out.println("Number is palindrome");
   else 
   System.out.println("Number is Not Palindrome");
   }
   
  }

Output:
Enter the Number:
898
Number is Palindrome

5. Java Program to find Maximum and Minimum Number?

We will be using min,max method defined in java Math class to find minimum & maximum number between three input numbers for more numbers we can use array and then find maximum and minimum number in array, for simplicity we will be using min & max method.

/*
Java Program to find Min max Number*/
import java.util.Scanner;
public class Main
{ 
    int a,b,c;
    Scanner sc=new Scanner(System.in);
    System.out.println("Enter the First Number:");
    a=sc.nextInt();
    System.out.println("Enter the Second Number:");
    b=sc.nextInt();
    System.out.println("Enter the Third Number:");
    c=sc.nextInt();
    int z=math.max(a,b);
    int y=math.max(z,c);
    System.out.println("Maximum Number:"+y);
    int p=math.min(a,b);
    int q=math.min(p,c);
    System.out.println("Minimum Number:"+q);
	}
}

Output:
Enter the First Number:
100
Enter the Second Number:
90
Enter the Third Number:
500
Maximum Number:500
Minimum Number:90

6. Java basics Programs- To Find Factorial of Given Number?

Factorial of Number ‘n’ is the product of all positive Integers in their descending order
let say 5!=54321
5!=120
Their can be many ways to calculate factorial of number in java:

  • Using Recursion
  • By Reverse Loop
  • Using Forward Loop

In this Example we will be using forward and Reverse loop technique only!

/*
Java Program to Calculate Factorial of Number Using Reverse Loop*/
import java.util.Scanner;
import java.util.*;
public class Factorial
{
 public static void main(String args[])
 {
  int a;fact=1;
  Scanner sc=new Scanner(System.in);
  System.out.println(Enter the Number:);
  a=sc.nextInt();
  for(i=a;i>1;i++)//reverse loop
  fact=fact*i;
  System.out.println("Factorial of Given Number:"+fact);
 }
}

Output:
Enter the Number:
6
Factorial of Given Number:720
Forward Loop Technique:

/*
Java Program to Calculate Factorial of Number Using Forward Loop*/
import java.util.Scanner;
import java.util.*;
public class Factorial
{
 public static void main(String args[])
 {
  int a;fact=1;
  Scanner sc=new Scanner(System.in);
  System.out.println(Enter the Number:);
  a=sc.nextInt();
  for(i=1;i<=a;i++)//forward loop
  fact=fact*i;
  System.out.println("Factorial of Given Number:"+fact);
 }
}
Output:
Enter the Number:
6
Factorial of Given Number:720

7. Java basic Programs- To Implement Fibonacci Series/Sequence

Fibonacci series or sequence of number in which next upcoming series number is sum of previous two numbers.
let take example:
0,1,1,2,3,5,8,13,21,34……
here 2 is sum of previous two numbers 1+1
Similarly 5 is sum of 3+2

/*
Java Program to implement Fibonacci Series */
import java.util.*;
import java.util.Scanner;
public class fiboimplementation
{
  public static void main(String args[])
  {
    int number1=0,number2=1,number3,upto;
    System.out.println("Enter the Number upto You want to print fibo sequence!");
    Scanner sc=new Scanner(System.in);
    upto=sc.nextInt();
    System.out.println(number1+""+number2);//printing start 2 number 0,1
    for(int i=2;i<upto;i++)
    {
     number3=number2+number1;
     System.out.print("|"+number3);
     number1=number2;
     number2=number3;
    }

  }
}

Output:
Enter the Number upto You want to print fibo sequence
10
01
|1|2|3|5|8|13|21|34

Related Posts:

Click Here to go to Main Index [Click]

Leave a Comment