Code-Blocks II

Java Programs PART II :

Next level to basic programs are available here with sample inputs and outputs

1) NUMBER PALINDROME :
Java Program to Reverse a Number & Check if it is a Palindrome. 

import java.util.*;
public class Palindrome
{
    public static void main(String args[])
    {
        int n, m, a = 0,x;
        Scanner s = new Scanner(System.in);
        System.out.print("Enter any number:");
        n = s.nextInt();
        m = n;
        while(n > 0)
        {
            x = n % 10;
            a = a * 10 + x;
            n = n / 10;
        }
        if(a == m)
        {
            System.out.println("Given number "+m+" is Palindrome");
        }
        else
        {
            System.out.println("Given number "+m+" is Not Palindrome");
        }
    }
}

Sample input/output 1:
Enter any number:121
Given number 121 is Palindrome
 
Sample input/output 2:
Enter any number:145
Given number 145 is Not Palindrome
2) Java Program to Find Two Elements such that their Sum is Closest to Zero 

import java.util.*;
public class Sum_Zero 
{
    public static void main(String[] args) 
    {
        int n, min1 = 0, min2 = 1, sum, minimum;
        Scanner s = new Scanner(System.in);
        System.out.print("Enter the number of elements you want:");
        n = s.nextInt();
        int a[] = new int[n];
        System.out.println("Enter all the numbers:");
        for(int i = 0; i < n; i++)
        {
            a[i] = s.nextInt();
        }
        minimum=Math.abs(a[0] + a[1]);
        for(int i = 0; i < n; i++)
        {
            for(int j = i + 1; j < n; j++)
            {
                sum = Math.abs(a[i] + a[j]);
                if(sum < minimum)
                {
                    min1 = i;
                    min2 = j;
                    minimum = sum;
                }
            }
        }
        System.out.println("Element 1:"+a[min1]); 
        System.out.println("Element 2:"+a[min2]);
    }
}

Sample input:
Enter the number of elements you want:5
Enter all the numbers:
-2
-1
3
6
5

Sample output:
Element 1:-2
Element 2:3
3) Java Program to Find if a Given Year is a Leap Year. 

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

Sample Input/output 1:
Enter any year:1800
Year 1800 is not a Leap Year
 
Sample Input/output 2:
Enter any year:2000
Year 2000 is a Leap Year
4) Java Program to Find Arithmetic Sum of 2 or 3 or 4 Variables by Passing Argument using Method Overloading.

import java.util.*;
public class Sum_Overloading
{
    int add(int x, int y)
    {
        int sum;
        sum = x + y;
        return sum;
    }
    int add(int x, int y, int z)
    {
        int sum;
        sum = x + y + z;
        return sum;
    }
    int add(int x, int y, int z, int w)
    {
        int sum;
        sum = x + y + z + w;
        return sum;
    }
    public static void main(String[] args) 
    {    
        Sum_Overloading obj = new Sum_Overloading();
        int a = obj.add(10, 15);
        System.out.println("Sum with two arguments:"+a);
        int b = obj.add(10, 15, 20);
        System.out.println("Sum with three arguments:"+b);
        int c = obj.add(10, 15, 20, 25);
        System.out.println("Sum with four arguments:"+c);
    }
}

Sample output:
Sum with two arguments:25
Sum with three arguments:45
Sum with four arguments:70
5) Java Program to Show the Nesting of Methods to find the area,perimeter,volume.

import java.util.*;
public class Nesting_Methods 
{
    int perimeter(int l, int b)
    {
            int pr = 12 * (l + b);
            return pr;
    }
    int area(int l, int b)
    {
            int pr = perimeter(l, b);
            System.out.println("Perimeter:"+pr);
            int ar = 6 * l * b;
            return ar;
    }
    int volume(int l, int b, int h)
    {
        int ar = area(l, b);
        System.out.println("Area:"+ar);
        int vol ;
        vol = l * b * h;
        return vol;
    }
    public static void main(String[] args) 
    {
        Scanner s = new Scanner(System.in);
        System.out.print("Enter length of cuboid:");
        int l = s.nextInt();
        System.out.print("Enter breadth of cuboid:");
        int b = s.nextInt();
        System.out.print("Enter height of cuboid:");
        int h = s.nextInt();
        Nesting_Methods obj = new Nesting_Methods();
        int vol = obj.volume(l, b, h);
        System.out.println("Volume:"+vol);
    }
}

Sample input:
Enter length of cuboid:5
Enter breadth of cuboid:6
Enter height of cuboid:7

Sample output:
Perimeter:132
Area:180
Volume:210

6) java sample program to find the use of final keyword and funtions.

import java.util.*;
class Figure
{
    final int length = 5;
    final int bredth = 4;
    final void area()
    {
        int a = length * bredth;
        System.out.println("Area:"+a);
    }
}
class Rectangle extends Figure
{
    final void rect()
    {
        System.out.println("This is rectangle");
    }
}
final public class Final_Use extends Rectangle
{
    public static void main(String[] args) 
    {
        Final_Use obj = new Final_Use();
        obj.rect();
        obj.area();
    }
}

Sample Input:
This is rectangle

Sample Output:
Area:20
7) java program to perform string concatenation using funtions.

import java.util.*;
public class String_Concatenate
{
    public static void main(String[] args) 
    {
        String a, b, c;
        Scanner s = new Scanner(System.in);
        System.out.print("Enter first string:");
        a = s.nextLine();
        System.out.print("Enter second string:");
        b = s.nextLine();
        String_Concatenate obj = new  String_Concatenate();
        c = obj.concat(a, b);
        System.out.println("New String:"+c);
    }
    String concat(String x, String y)
    {
        String z;
        z = x + " " + y;
        return z;
    }
}

sample Input:
Enter first string:hello
Enter second string:world

Sample Output:
New String:hello world
8) java program to find the simple interest .

import java.util.*;
public class Simple_Interest
{
    public static void main(String args[]) 
    {
        float p, r, t;
        Scanner s = new Scanner(System.in);
        System.out.print("Enter the Principal : ");
        p = s.nextFloat();
        System.out.print("Enter the Rate of interest : ");
        r = s.nextFloat();
        System.out.print("Enter the Time period : ");
        t = s.nextFloat();
        float si;
        si = (r * t * p) / 100;
        System.out.print("The Simple Interest is : " + si);
    }
}

Sample input:
Enter the Principal : 20202
Enter the Rate of interest : 2.5
Enter the Time period : 3

Sample output:
The Simple Interest is : 1515.15
9) java program to find the amstrong number from 1 to n.

import java.util.*;
public class Armstrong
{
    public static void main(String[] args) 
     {
        int n, count = 0, a, b, c, sum = 0;
        System.out.print("Armstrong numbers from 1 to 1000:");
        for(int i = 1; i <= 1000; i++)
        {
            n = i;
            while(n > 0)
            {
                b = n % 10;
                sum = sum + (b * b * b);
                n = n / 10;
            }
            if(sum == i)
            {
                System.out.print(i+" ");
            }
            sum = 0;
        }
    }
}

sample output:
Armstrong numbers from 1 to 1000: 1 153 370 371 407
10) java program to check whether the given number is perfect number or not.

import java.util.*;
public class Perfect
{
    public static void main(String[] args) 
    {
        int n, sum = 0;
        Scanner s = new Scanner(System.in);
        System.out.print("Enter any integer you want to check:");
        n = s.nextInt();
        for(int i = 1; i < n; i++)
        {
            if(n % i == 0)
            {
                sum = sum + i;
            }
        }
        if(sum == n)
        {
            System.out.println("Given number is Perfect");
        }
        else
        {
            System.out.println("Given number is not Perfect");
        }    
    }
    int divisor(int x)
    {
       return x;
    }
}

sample input:
Enter any integer you want to check:6

Sample output:
Given number is Perfect
11) java program to check whether the given number is a amstrong number or not.

import java.util.*;
public class ArmStrong 
{
    public static void main(String[] args) 
    {
        int n, count = 0, a, b, c, sum = 0;
        Scanner s = new Scanner(System.in);
        System.out.print("Enter any integer you want to check:");
        n = s.nextInt();
        a = n;
        c = n;
        while(a > 0)
        {
            a = a / 10;
            count++;
        }
        while(n > 0)
        {
            b = n % 10;
            sum = (int) (sum+Math.pow(b, count));
            n = n / 10;
        }
        if(sum == c)
        {
            System.out.println("Given number is Armstrong");
        }
        else
        {
            System.out.println("Given number is not Armstrong");
        }    
    }
}

Sample input:
Enter any integer you want to check:153

sample output:
Given number is Armstrong
12) java program to find the given number is prime or not.

import java.util.*;
public class CheckPrime
{
    public static void main(String args[])
    {       
        int j, x, flag = 0;
        System.out.print("Enter any number :");
        Scanner s = new Scanner(System.in);
        x = s.nextInt();
        for( j = 2; j < x; j++)
        {
            if(x % j == 0)
            {
                flag = 0;
                break;
            }
            else
            {
                flag = 1;
            }
         }
         if(flag == 1)
         {
             System.out.println("The "+x+" is a prime number.");
         }
         else
         {
             System.out.println("The "+x+" is not a prime number.");
         }           
    }
}

sample input: 
Enter any number :45

sample output:
The 45 is not a prime number.
13) java program to generate  fibonacci series

import java.util.*;
public class Fibonacci 
{
    public static void main(String[] args) 
    {
        int n, a = 0, b = 0, c = 1;
        Scanner s = new Scanner(System.in);
        System.out.print("Enter value of n:");
        n = s.nextInt();
        System.out.print("Fibonacci Series:");
        for(int i = 1; i <= n; i++)
        {
            a = b;
            b = c;
            c = a + b;
            System.out.print(a+" ");
        }
    }
}
 
sample input:
Enter value of n:5

sample output:
Fibonacci Series:0 1 1 2 3
14) java program to compute the sum of two complex numbers using buffer reader.

import java.*;
public class ComplexNumbers {
    // Main function to read two complex numbers and add them
    public static void main(String[] args) {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        double i1,j1,i2,j2;
        System.out.println("Enter the real part and
                            imaginary part of the first complex number");
        try{
            i1=Double.parseDouble(br.readLine());
            j1=Double.parseDouble(br.readLine());
        }catch (Exception e){
            System.out.println("An error occurred");
            return;
        }
        System.out.println("Enter the real part and
                            imaginary part of the second complex number");
        try{
            i2=Double.parseDouble(br.readLine());
            j2=Double.parseDouble(br.readLine());
        }catch (Exception e){
            System.out.println("An error occurred");
            return;
        }
        System.out.println("The first complex number is "
                            + i1 + " + i(" + j1 + ")");
        System.out.println("The second complex number is "
                            + i2 + " + i(" + j2 + ")");
        System.out.println("The sum of the two complex numbers is "
                            + (i1 + i2) + " + i(" + (j1 + j2) + ")");
    }
}

sample input:
Enter the real part and imaginary part of the first complex number
4
6
Enter the real part and imaginary part of the second complex number
-5
2

sample output:
The first complex number is 4.0 + i(6.0)
The second complex number is -5.0 + i(2.0)
The sum of the two complex numbers is -1.0 + i(8.0)
15) java program to print the area of a trapezium

import java.io.*;
public class AreaOfATrapezium {
    // Function to find area of a trapezium
    public static void main(String[] args) {
   BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        double l1,l2,height;
        System.out.println("Enter the length of the two parallel sides 
                                     and the height of the trapezium");
            l1=Double.parseDouble(br.readLine());
            l2=Double.parseDouble(br.readLine());
            height=Double.parseDouble(br.readLine());

        if(l1<=0 || l2<=0 || height<=0){
            System.out.println("Wrong Input");
        }
        System.out.println("Area = " + (l1+l2)*height/2 );
    }
}

sample input:
Enter the length of the two parallel sides and the height of the trapezium
13.765
23.555
8.0

sample output:
Area = 149.28