Java Program to Convert Inch to Kilometer and Kilometer to Inch

Java Program to Convert Inch to Kilometer and Kilometer to Inch

In the previous article we have discussed about Java Program to Convert Kilogram to Pound and Pound to Kilogram

In this article we will see how to convert Inch to Kilometer and Kilometer to Inch by using Java programming language.

Java Program to Convert Inch to Kilometer and Kilometer to Inch

Before jumping into the program let’s know the relationship between Inch and Kilometer and how we can convert Inch to Kilometer and vice versa.

Generally Inch and Kilometer are used as unit in case of length/distance measurement.

1 Inch =  Kilometer
1 Kilometer =  Inch

Formula to convert Kilometer to Inch.

Inch = Kilometer * 39370.1

Formula to convert Inch to Kilometer.

Kilometer  = Inch * 2.54 * .00001 (OR)  Inch / 39370.1

Let’s see different ways to convert Inch to Kilometer and Kilometer to Inch.

Method-1: Java Program to Convert Inch to Kilometer and Kilometer to Inch By Using Static Input Value

Approach:

  • Declare Inch and Kilometer value.
  • Then convert Inch to Kilometer and Kilometer to Inch by using the formula.
  • Print result.

Program:

public class Main 
{
   public static void main(String args[])
   {
        //value of inch declared  
        double inch = 1;
        //value of kilometer declared
        double kilometer = 1;
        
        //converting inch to kilometer
        double km = inch * 2.54 * .00001;
        //converting kilometer to inch
        double in = kilometer * 39370.1;
        //printing result
        System.out.println("Value of "+kilometer+" kilometer in inch: "+ in);   
        System.out.println("Value of "+inch+" inch in kilometer: "+ km);   
   }
}
Output:

Value of 1.0 kilometer in inch: 39370.1
Value of 1.0 inch in kilometer: 2.54E-5

Method-2: Java Program to Convert Inch to Kilometer and Kilometer to Inch By Using User Input Value

Approach:

  • Take user input of Inch and Kilometer value.
  • Then convert Inch to Kilometer and Kilometer to Inch by using the formula.
  • Print result.

Program:

import java.util.*;
public class Main 
{
   public static void main(String args[])
   {
        //Scanner class object created
        Scanner sc=new Scanner(System.in);
        //Taking the value input of double variable kilometer
        System.out.println("Enter value of kilometer: ");  
        double kilometer = sc.nextDouble();
        //Taking the value input of double variable inch
        System.out.println("Enter value of inch: ");  
        double inch = sc.nextDouble();
        
        //converting inch to kilometer
        double km = inch * 2.54 * .00001;
        //converting kilometer to inch
        double in = kilometer * 39370.1;
        //printing result
        System.out.println("Value of "+kilometer+" kilometer in inch: "+ in);   
        System.out.println("Value of "+inch+" inch in kilometer: "+ km);   
   }
}
Output:

Enter value of kilometer: 
2
Enter value of inch: 
20000
Value of 2.0 kilometer in inch: 78740.2
Value of 20000.0 inch in kilometer: 0.508

Method-3: Java Program to Convert Inch to Kilometer and Kilometer to Inch Using User Defined Method

Approach:

  • Take user input of Inch and Kilometer value.
  • Call a user defined method by passing Inch and Kilometer value as parameter.
  • Inside method convert Inch to Kilometer and Kilometer to Inch by using the formula.
  • Print result.

Program:

import java.util.*;
public class Main 
{
   public static void main(String args[])
   {
        //Scanner class object created
        Scanner sc=new Scanner(System.in);
        //Taking the value input of double variable kilometer
        System.out.println("Enter value of kilometer: ");  
        double kilometer = sc.nextDouble();
        //Taking the value input of double variable inch
        System.out.println("Enter value of inch: ");  
        double inch = sc.nextDouble();
        //calling user defined method convert()
        convert(kilometer, inch);
   }
   
   //convert() method to convert kilometer to inch and vice versa
   public static void convert(double kilometer, double inch)
   {
        //converting inch to kilometer
        double km = inch / 39370.1;
        //converting kilometer to inch
        double in = kilometer * 39370.1;
        //printing result
        System.out.println("Value of "+kilometer+" kilometer in inch: "+ in);   
        System.out.println("Value of "+inch+" inch in kilometer: "+ km);   
   }
}
Output:

Enter value of kilometer: 
2
Enter value of inch: 
20000
Value of 2.0 kilometer in inch: 78740.2
Value of 20000.0 inch in kilometer: 0.507

The best and excellent way to learn a java programming language is by practicing Simple Java Program Examples as it includes basic to advanced levels of concepts.

Revise these:

  1. Write a java program to convert centimeter to inch meter and kilometer?
  2. Write a java program that reads a number in inches converts it to meters?
  3. Write a program to convert kilometers to meters in java?
  4. C program to convert centimeters to feet and inches?
  5. C program to convert centimeters to inches?
  6. Java program to convert inches to centimeters?
  7. Java program to convert meter to kilometer?

Related Java Programs:

Java Program to Print Series 10 20 30 40 40 50 …N

Java Program to Print Series 10 20 30 40 40 50 …N

In the previous article, we have discussed about Java Program to Print the Series 1 2 9 28 65 N

In this article we are going to see how to print the series 10 20 30 40 40 50 …N by using Java programming language. How to print in java and how to print 1 to 20 in java? We have provided all the details here.

Java Program to Print Series 10 20 30 40 40 50 …N

In this series it can be seen that

  • Numbers at each position ‘i‘, the term is calculated as 10×i OR
  • First element of series is 10 and next elements are calculated as previous element + 10

For example, if the series has 3 terms, the output will be

10×1   10×2   10×3

10        20        30

For example, if the series has 5 terms, the output will be

10×1  10×2  10×3  10×4  10×5

10       20       30       40      50

Let’s see different ways to find the series.

Method-1: Java Program to Print Series 10 20 30 40 40 50 …N By Using Multiplication

Approach:

  • Create Scanner class object.
  • Declare an integer variable ‘n‘ which holds the value of number of terms in the series.
  • Prompt the user to enter the value of variable ‘n
  • Run a for loop from i=0 to i<=n.
  • Inside the loop, print 10*i

Program:

import java.util.Scanner;
public class Main 
{
    public static void main(String[] args)
    {
        //create a Scanner object
        Scanner sc = new Scanner(System.in);
        //prompt the user to enter the number of terms
        System.out.print("Enter the number of terms: ");
        int n = sc.nextInt();
        //print the series
        System.out.print("The series is: ");
        for (int i = 1; i <= n; i++)
        {
            System.out.print(10*i + " ");
        }
    }
}
Output:

Enter the number of terms: 10
The series is: 10 20 30 40 50 60 70 80 90 100

Method-2: Java Program to Print Series 10 20 30 40 40 50 …N By Using Addition

Approach:

  • Create Scanner class object.
  • Declare an integer variable ‘n‘ which holds the value of number of terms in the series.
  • Declare an integer variable say ‘value‘ and initialize it with value 10.
  • Prompt the user to enter the value of variable ‘n
  • Print ‘value‘ which is having value 10.
  • Run a for loop from i=0 to i<n-1.
  • Inside the loop, print value+10 , as by using addition logic when we will add previous element with 10 then we will get next element of the series.

Program:

import java.util.Scanner;
public class Main 
{
    public static void main(String[] args)
    {
        // create a Scanner object
        Scanner sc = new Scanner(System.in);
        // prompt the user to enter the number of terms
        System.out.print("Enter the number of terms: ");
        int n = sc.nextInt();
        int value=10;
        // print the series
        System.out.print("The series is: ");
        System.out.print(value+" ");
        for (int i = 0; i < n-1; i++)
        {
            value=value+10;
            System.out.print(value+" ");
        }
    }
}
Output:

Enter the number of terms: 5
The series is: 10 20 30 40 50

Method-3: Java Program to Print Series 10 20 30 40 40 50 …N By Using User Defined Method

Approach:

  • Create Scanner class object.
  • Declare an integer variable ‘n‘ which holds the value of number of terms in the series.
  • Prompt the user to enter the value of variable ‘n
  • Then call a user defined method by passing n as parameter.
  • Inside method run a for loop from i=0 to i<=n.
  • Inside the loop, print 10*i

Program:

import java.util.Scanner;
public class Main
{
    public static void main(String[] args)
    {
        //create a Scanner object
        Scanner sc = new Scanner(System.in);
        //prompt the user to enter the number of terms
        System.out.print("Enter the number of terms: ");
        int n = sc.nextInt();
        //call the method to print the series
        printSeries(n);
    }
    
    //user defined method printSeries() to print series
     private static void printSeries(int n) 
    {
        System.out.println("The series is: ");
        for (int i = 1; i <= n; i++) 
        {
            System.out.print(10*i + " ");
        }
    }
}
Output:

Enter the number of terms: 8
The series is: 
10 20 30 40 50 60 70 80

Have you mastered basic programming topics of java and looking forward to mastering advanced topics in a java programming language? Go with these ultimate Advanced java programs examples with output & achieve your goal in improving java coding skills.

Read Also: Java Program to Print Series 2 5 10 17 26 37 50…N

Java how to print using java program hopes you will understand and solve this to know very well.

  1. How To Print Sequence Of Numbers In Java?
  2. What Will Be The Output Of Print(‘*’ * 10)?

Related Java Programs:

5700 m to km – Java Program to Convert Kilometer to Meter and Meter to Kilometer

Java Program to Convert Kilometer to Meter and Meter to Kilometer

5700 m to km: In the previous article we have discussed about Java Program to Convert Inch to Yard and Yard to Inch

In this article we will see how to convert Kilometer to Meter and Meter to Kilometer by using Java programming language.

Java Program to Convert Kilometer to Meter and Meter to Kilometer

5700 meters to kilometers: Before jumping into the program let’s know the relationship between Kilometer and Meter and how we can convert Kilometer to Meter and vice versa.

Generally Kilometer and Meter are used as unit in case of length/distance measurement.

1 Kilometer =  1000 Meter
1 Meter = 0.001 Kilometer

Formula to convert Kilometer to Meter.

Meter = Kilometer*1000

Formula to convert Meter to Kilometer.

Kilometer = Meter/1000 (OR) Meter*0.001

Let’s see different ways to convert Kilometer to Meter and Meter to Kilometer.

Method-1: Java Program to Convert Kilometer to Meter and Meter to Kilometer By Using Static Input Value

Approach:

  • Declare Kilometer and Meter value.
  • Then convert Kilometer to Meter and Meter to Kilometer by using the formula.
  • Print result.

Program:

import java.util.*;
public class Main 
{
   public static void main(String args[])
   {
        //Scanner class object created
        Scanner sc=new Scanner(System.in);
        //value of meter declared
        double meter = 1;
        // value of kilometer declared  
        double kilometer = 1;

        //converting kilometer to meter
        double m = kilometer*1000;
        //converting meter to kilometer
        double km = meter*0.001;
        //printing result
        System.out.println("Value of "+meter+" meter in kilometer: "+ km);   
        System.out.println("Value of "+kilometer+" kilometer in meter: "+ m);   
   }
}
Output:

Value of 1.0 meter in kilometer: 0.001
Value of 1.0 kilometer in meter: 1000.0

Method-2: Java Program to Convert Kilometer to Meter and Meter to Kilometer By Using User Input Value

Approach:

  • Take user input of Kilometer and Meter value.
  • Then convert Kilometer to Meter and Meter to Kilometer by using the formula.
  • Print result.

Program:

import java.util.*;
public class Main 
{
   public static void main(String args[])
   {
        //Scanner class object created
        Scanner sc=new Scanner(System.in);
        //Taking the value input of double variable meter
        System.out.println("Enter value of meter: ");  
        double meter = sc.nextDouble();
        //Taking the value input of double variable kilometer
        System.out.println("Enter value of kilometer: ");  
        double kilometer = sc.nextDouble();

        //converting kilometer to meter
        double m = kilometer*1000;
        //converting meter to kilometer
        double km = meter*0.001;
        //printing result
        System.out.println("Value of "+meter+" meter in kilometer: "+ km);   
        System.out.println("Value of "+kilometer+" kilometer in meter: "+ m);   
   }
}
Output:

Enter value of meter: 
5700
Enter value of kilometer: 
4
Value of 5700.0 meter in kilometer: 5.7
Value of 4.0 kilometer in meter: 4000.0

Method-3: Java Program to Convert Kilometer to Meter and Meter to Kilometer By Using User Defined Method

Approach:

  • Take user input of Kilometer and Meter value.
  • Call a user defined method by passing Kilometer and Meter value as parameter.
  • Inside method convert Kilometer to Meter and Meter to Kilometer by using the formula.
  • Print result.

Program:

import java.util.*;
public class Main 
{
   public static void main(String args[])
   {
        //Scanner class object created
        Scanner sc=new Scanner(System.in);
        //Taking the value input of double variable meter
        System.out.println("Enter value of meter: ");  
        double meter = sc.nextDouble();
        //Taking the value input of double variable kilometer
        System.out.println("Enter value of kilometer: ");  
        double kilometer = sc.nextDouble();
         //calling user defined method convert()
        convert(meter, kilometer);
   }
   
   //convert() method to convert meter to kilometer and vice versa
   public static void convert(double meter, double kilometer)
   {
        //converting kilometer to meter
        double m = kilometer*1000;
        //converting meter to kilometer
        double km = meter*0.001;
        //printing result
        System.out.println("Value of "+meter+" meter in kilometer: "+ km);   
        System.out.println("Value of "+kilometer+" kilometer in meter: "+ m);   
   }
}
Output:

Enter value of meter: 
1200
Enter value of kilometer: 
3.4
Value of 1200.0 meter in kilometer: 1.2
Value of 3.4 kilometer in meter: 3400.0

The best and excellent way to learn a java programming language is by practicing Simple Java Program Examples as it includes basic to advanced levels of concepts.

Test Yourself:

  1. Write a program to convert kilometers to meters in c?
  2. Write a program to convert meter into centimeter in java?
  3. Write a java program to convert centimeter to inch meter and kilometer?
  4. java program to convert kilometer to meter and meter to kilometer?
  5. Convert meter to kilometer in java?
  6. Program to convert meters into kilometers?
  7. Python program to convert meter to kilometer?
  8. Program to convert meters into kilometers?
  9. Meter to kilometer formula?
  10. Meter to kilometer chart?

Related Java Programs:

Java Program to Print Heart Star Pattern

Java Program to Print Heart Star Pattern

Program to Print Heart Star Pattern

In this article we are going to see how to print the Heart star pattern.

Example-1

When row value is 7
    ***    ***
  *****  *****
**************
 *************
  ***********
   *********
    *******
     *****
      ***
       *

Practice Java programming from home without using any fancy software just by tapping on this Simple Java Programs for Beginners tutorial.

Approach:

  • Enter total row and store it in an integer variable say row.
  • Take a inner loop to print the column values.
  • Take a nested for loop to print left semi-circle at the beginning.
  • Take a nested for loop to print right semi-circle beginning.
  • Take another nested loop to print a inverted pyramid below the semi-circles.

 JAVA Code:

Method-1 : Static Star Character

import java.util.Scanner;
public class Main
{
    public static void main(String[] args) 
    {
        int r,c, row;
        Scanner sc= new Scanner(System.in); 
        System.out.print("Enter no of rows = ");
        row=sc.nextInt();
        // loop to print 2 semicircles at the top
        for(r = row/2; r <= row; r+=2)
        { 
            // print spaces
            for(c = 1; c < row-r; c+=2) 
            {  
                System.out.print(" ");  
            }
            // loop to print left semi-circle
            for(c = 1; c <= r; c++)
            {  
                System.out.print("*");  
            }  
            // print spaces
            for(c = 1; c <= row-r; c++)
            {  
                System.out.print(" ");  
            }  
            // loop to print right semi-circle
            for(c = 1; c <= r; c++)
            {  
                System.out.print("*");  
            }  
            // move to next line/row
            System.out.println("");  
        }  
         
        // loop to print the inverted pyramid
        for(r = row; r >= 1; r--)
        {
            // adds spaces in each row
            for(c = r; c < row; c++)
            {  
                System.out.print(" ");  
            } 
            // keep on printing star from c=1 till (r*2)-1
            for(c = 1; c <= (r*2)-1; c++)
            {  
                System.out.print("*");  
            }  
            // move to next line/row
            System.out.println("");  
        }  
  
    } 
}

Output:

Enter no of rows = 10
    *****     *****
  *******   *******
********* *********
*******************
 *****************
  ***************
   *************
    ***********
     *********
      *******
       *****
        ***
         *

Method-2 : User Input Character

import java.util.Scanner;
public class Main
{
    public static void main(String[] args) 
    {
        int r,c, row;
        Scanner sc= new Scanner(System.in); 
        System.out.print("Enter no of rows = ");
        row=sc.nextInt();
        // Enter any random Character
        System.out.print("Enter any character = ");
        char s=sc.next().charAt(0);
        // loop to print 2 semicircles at the top
        for(r = row/2; r <= row; r+=2)
        { 
            // print spaces
            for(c = 1; c < row-r; c+=2) 
            {  
                System.out.print(" ");  
            }
            // loop to print left semi-circle
            for(c = 1; c <= r; c++)
            {  
                System.out.print(s);  
            }  
            // print spaces
            for(c = 1; c <= row-r; c++)
            {  
                System.out.print(" ");  
            }  
            // loop to print right semi-circle
            for(c = 1; c <= r; c++)
            {  
                System.out.print(s);  
            }  
            // move to next line/row
            System.out.println("");  
        }  
         
        // loop to print the inverted pyramid
        for(r = row; r >= 1; r--)
        {
            // adds spaces in each row
            for(c = r; c < row; c++)
            {  
                System.out.print(" ");  
            } 
            // keep on printing star from c=1 till (r*2)-1
            for(c = 1; c <= (r*2)-1; c++)
            {  
                System.out.print(s);  
            }  
            // move to next line/row
            System.out.println("");  
        }  
  
    } 
}

Output:

Enter no of rows = 10
Enter any character = *
    *****     *****
  *******   *******
********* *********
*******************
 *****************
  ***************
   *************
    ***********
     *********
      *******
       *****
        ***
         *

C Code:

#include<stdio.h>  
int main()
{
    int r,c, row;
     
    printf("Enter no of rows = ");
    scanf("%d", &row);  
    for(r = row/2; r <= row; r+=2){ 
 
        for(c = 1; c < row-r; c+=2) {  
            printf(" ");  
        }
 
        for(c = 1; c <= r; c++){  
            printf("*");  
        }  
     
        for(c = 1; c <= row-r; c++){  
            printf(" ");  
        }  
    
        for(c = 1; c <= r; c++){  
            printf("*");  
        }  

        printf("\n");  
    }  
     
    for(r = row; r >= 1; r--){  
        for(c = r; c < row; c++){  
            printf(" ");  
        }  
        for(c = 1; c <= (r*2)-1; c++){  
            printf("*");  
        }  

        printf("\n");  
    }  
   
    return 0;  
}

Output:

Enter no of rows = 10

    *****     *****
  *******   *******
********* *********
*******************
 *****************
  ***************
   *************
    ***********
     *********
      *******
       *****
        ***
         *

C++ Code:

#include <iostream>

using namespace std;

int main()
{
    int r,c,row;
     
    cout<<"Enter no of rows = ";
    cin>>row;  
    for(r = row/2; r <= row; r+=2){ 
 
        for(c = 1; c < row-r; c+=2) {  
            cout<<" ";  
        }
 
        for(c = 1; c <= r; c++){  
            cout<<"*";  
        }  
     
        for(c = 1; c <= row-r; c++){  
            cout<<" ";  
        }  
    
        for(c = 1; c <= r; c++){  
            cout<<"*";  
        }  

        cout<<"\n";  
    }  
     
    for(r = row; r >= 1; r--){  
        for(c = r; c < row; c++){  
            cout<<" ";  
        }  
        for(c = 1; c <= (r*2)-1; c++){  
            cout<<"*";  
        }  

        cout<<"\n";  
    }  
   
    return 0;  
}
Output:

Enter no of rows = 10

    *****     *****
  *******   *******
********* *********
*******************
 *****************
  ***************
   *************
    ***********
     *********
      *******
       *****
        ***
         *

Also Read: Java Program to Print Inverted Heart Star Pattern

Answer these:

  1. Draw a heart using graphics class in java?
  2. How to print heart emoji in java?
  3. Hollow heart pattern in java?
  4. Pattern program in java?
  5. Java program for love propose?
  6. C program to print heart pattern with name?
  7. Write a program to print a figure in the shape of a heart by the sign o?
  8. Java program to print heart pattern with name?
  9. Java program to print a star pattern?
  10. Java program to print star pattern in triangle?
  11. Java program to print star pattern using while loop?
  12. Write a program to print star pattern in java?
  13. Java program to print heart shape?
  14. Heart shape code?

Related Java Star Pattern Programs:

Java Program to Print Series 6 12 18 24 28 …N

Java Program to Print Series 6 12 18 24 28 …N

In the previous article, we have discussed about Java Program to Find the Sum of Series 1 + (x+2)/2! + (2x+3)/3! + (3x+4)/4! + …… N

In this article we are going to see how to print the series 6 12 18 24 28 …N by using Java programming language. Along with that c program to print series 1 4 9 16, C program to print the series 1 3 5 7 9, C program to print series 1, 4, 9, 16.

Read Also: Java Program to Print Series 8 16 24 32 40 …N

Java Program to Print Series 6 12 18 24 28 …N

In this series it can be seen that

  • Numbers at each position ‘i‘, the term is calculated as 6×i OR
  • First element of series is 6 and next elements are calculated as previous element + 6

For example, if the series has 3 terms, the output will be

6×1   6×2   6×3

6        12      18

For example, if the series has 5 terms, the output will be

6×1  6×2  6×3  6×4  6×5

6       12     18    24     30

Let’s see different ways to find the series.

Method-1: Java Program to Print Series 6 12 18 24 28 …N By Using Multiplication

Approach:

  • Create Scanner class object.
  • Declare an integer variable ‘n‘ which holds the value of number of terms in the series.
  • Prompt the user to enter the value of variable ‘n
  • Run a for loop from i=0 to i<=n.
  • Inside the loop, print 6*i

Program:

import java.util.Scanner;
public class Main 
{
    public static void main(String[] args)
    {
        //create a Scanner object
        Scanner sc = new Scanner(System.in);
        //prompt the user to enter the number of terms
        System.out.print("Enter the number of terms: ");
        int n = sc.nextInt();
        //print the series
        System.out.print("The series is: ");
        for (int i = 1; i <= n; i++)
        {
            System.out.print(6*i + " ");
        }
    }
}
Output:

Enter the number of terms: 10
The series is: 6 12 18 24 30 36 42 48 54 60

Method-2: Java Program to Print Series 6 12 18 24 28 …N By Using Addition

Approach:

  • Create Scanner class object.
  • Declare an integer variable ‘n‘ which holds the value of number of terms in the series.
  • Declare an integer variable say ‘value‘ and initialize it with value 6.
  • Prompt the user to enter the value of variable ‘n
  • Print ‘value‘ which is having value 6.
  • Run a for loop from i=0 to i<n-1.
  • Inside the loop, print value+6 , as by using addition logic when we will add previous element with 6 then we will get next element of the series.

Program:

import java.util.Scanner;
public class Main 
{
    public static void main(String[] args)
    {
        // create a Scanner object
        Scanner sc = new Scanner(System.in);
        // prompt the user to enter the number of terms
        System.out.print("Enter the number of terms: ");
        int n = sc.nextInt();
        int value=6;
        // print the series
        System.out.print("The series is: ");
        System.out.print(value+" ");
        for (int i = 0; i < n-1; i++)
        {
            value=value+6;
            System.out.print(value+" ");
        }
    }
}
Output:

Enter the number of terms: 5
The series is: 6 12 18 24 30

Method-3: Java Program to Print Series 6 12 18 24 28 …N By Using User Defined Method

Approach:

  • Create Scanner class object.
  • Declare an integer variable ‘n‘ which holds the value of number of terms in the series.
  • Prompt the user to enter the value of variable ‘n
  • Then call a user defined method by passing n as parameter.
  • Inside method run a for loop from i=0 to i<=n.
  • Inside the loop, print 6*i

Program:

import java.util.Scanner;
public class Main
{
    public static void main(String[] args)
    {
        //create a Scanner object
        Scanner sc = new Scanner(System.in);
        //prompt the user to enter the number of terms
        System.out.print("Enter the number of terms: ");
        int n = sc.nextInt();
        //call the method to print the series
        printSeries(n);
    }
    
    //user defined method printSeries() to print series
     private static void printSeries(int n) 
    {
        System.out.println("The series is: ");
        for (int i = 1; i <= n; i++) 
        {
            System.out.print(6*i + " ");
        }
    }
}
Output:

Enter the number of terms: 8
The series is: 
6 12 18 24 30 36 42 48

Have you mastered basic programming topics of java and looking forward to mastering advanced topics in a java programming language? Go with these ultimate Advanced java programs examples with output & achieve your goal in improving java coding skills.

Try these to build your conceptual skills:

  1. Write a program to print series in java?
  2. Write a program to print the following series 1 4 7 10?
  3. Write a program to print the following pattern example 1 input 8 output 1 4 5 8 2 3 6 7?
  4. Write a program to print series in java?
  5. Write a program to display to the following series based on the user input?
  6. Write a program to print the following series 1 4 9 16?
  7. Write a program to print the following series 2,4,6,8 10?
  8. Java program to print series 1 12 123?
  9. Java program to print series 1 2 4 8 16?
  10. Java program to print series 2 5 10 17?
  11. Java program to print 1 2 4 8 16?
  12. Java program to print 1 12 123?
  13. Java program to print 1 2 4 7 11?

Related Java Programs:

Java Program to Check Sphenic Number

In the previous article, we have discussed Java Program to Check Emirp Number

In this article we are going to understand what Sphenic number, Sphenic number list, Sphenic number in c is and how we can check whether a number is Sphenic number or not in Java with examples and mischievous numbers program in java.

Also Read: Java Program to Check Autobiographical Number

Program to Check Sphenic Number

Sphenic numbers are numbers which have exactly 3 factors, all prime. A Sphenic number has exactly 8 divisors.

 Example :

30: 2*3*5, Sphenic number
66: 2*3*11, Sphenic number
15: 3*5, Not a Sphenic number

In the above examples the numbers 30 and 66 are Sphenic numbers as they both have 3 prime factors exactly. However 15 is not a Sphenic number.

Practice Java programming from home without using any fancy software just by tapping on this Simple Java Programs for Beginners tutorial.

Approach :

  1. We ask the user to enter/declare a number and store it.
  2. We check whether the number has 8 divisors or not and store them.
  3. Then we check if the first three divisors are prime or not.
  4. If they are prime then the number is a Sphenic Number.

Method-1: Java Program to Check Sphenic Number By Using Static Value

import java.util.*;

public class SphenicNumber
{
    // Creating a boolean array acessible to all functions
    static boolean arr[] = new boolean[10000];

    public static void main(String args[])
    {
        //A number declared;
        int num = 30;
        Prime();
        if(sphenicCheck(num))
        {
            System.out.println(num+" is a Sphenic number");
        }
        else
        {
            System.out.println(num+" is Not a Sphenic number");
        }
    }

    static void Prime()
    {
        // Filling the boolean array with all values set to true
        Arrays.fill(arr,true);
        for(int i = 2; i*i<10000; i++)
        {
            if(arr[i])
            {   
                //Marking all multiples of i as non prime
                for(int j = i*2; j<10000; j= j+i)
                    arr[j] = false;
            }
        }
    }

    //method to check sphenic number
    static boolean sphenicCheck(int num)
    {
        int arr1[] = new int[8];
        int count=0, j = 0,i;
        for(i =1; i<=num;i++)
        {
            // Counts the number of divisors
            if(num%i==0 && count< 8)
            {
                count++;
                arr1[j++]=i;
            }
        }
        // Returns if the number is Sphenic or not
        if(count==8 && (arr[arr1[1]] && arr[arr1[2]] && arr[arr1[3]]))  
            return true;
        else
            return false;
    }
}
Output:

30 is a Sphenic number

Method-2: Java Program to Check Sphenic Number By Using User Input Value

import java.util.*;

public class SphenicNumber
{
    // Creating a boolean array acessible to all functions
    static boolean arr[] = new boolean[10000];

    public static void main(String args[])
    {
        //Taking the number as input from the user using scanner class
        Scanner scan = new Scanner(System.in);
        System.out.print("Enter a number : ");
        int num = scan.nextInt();
        Prime();
        if(sphenicCheck(num))
        {
            System.out.println(num+" is a Sphenic number");
        }
        else
        {
            System.out.println(num+" is Not a Sphenic number");
        }
    }

    static void Prime()
    {
        // Filling the boolean array with all values set to true
        Arrays.fill(arr,true);
        for(int i = 2; i*i<10000; i++)
        {
            if(arr[i])
            {   
                //Marking all multiples of i as non prime
                for(int j = i*2; j<10000; j= j+i)
                    arr[j] = false;
            }
        }
    }

    //method to check sphenic number
    static boolean sphenicCheck(int num)
    {
        int arr1[] = new int[8];
        int count=0, j = 0,i;
        for(i =1; i<=num;i++)
        {
            // Counts the number of divisors
            if(num%i==0 && count< 8)
            {
                count++;
                arr1[j++]=i;
            }
        }
        // Returns if the number is Sphenic or not
        if(count==8 && (arr[arr1[1]] && arr[arr1[2]] && arr[arr1[3]]))  
            return true;
        else
            return false;
    }
}
Output:
Case-1
Enter a number : 30 
30 is Not a Sphenic number

Case-2
Enter a number : 23
23 is Not a Sphenic number

Are you wondering how to seek help from subject matter experts and learn the Java language? Go with these Basic Java Programming Examples and try to code all of them on your own then check with the exact code provided by expert programmers.

Try these:

  1. Write a program in java to check whether a number is a buzz number or not?
  2. How to find a duck number in java?
  3. Mischievous numbers program in java?
  4. Sphenic number in c++?
  5. What are the decreasing numbers code in java?
  6. Sphenic number c++?
  7. Odious number program in java?
  8. Java program to check special number?
  9. Java program to check spy number?
  10. Java program to check special characters?
  11. Java program to check strong number?
  12. What is sphenic number c?

Related Java Programs:

Java Menu Driven Program for String Operations

Java Menu Driven Program for String Operations

In this article we are going to see a menu driven string application by using Java program. Menu driven program in java using switch case example, Menu driven program in java using methods, Menu driven program for array operations in java, Menu driven program in java using if else, Menu driven program in java using scanner class, Menu driven program in java using do while, Menu-driven program in java.

Also Read: Java Menu Driven Program to Perform Basic Operations on Two Matrices

Java Menu Driven Program for String Operations

We will design a menu driven program for 4 String operations. Like this you can add more String operations as well.

  1. String Length
  2. Concatenating two strings
  3. Compare two strings
  4. Trim a string

User Defined Methods Used:

  • len( ) – To find the length of the string
  • concatenate( ) – Add two strings
  • compareStr( ) – Compare two strings
  • trimStr( ) – Trim whitespaces

Approach:

  • In Driver method, take a while loop display all the methods.
  • Ask the user to enter the choice.
  • Use switch case for navigation.
  • To find length of a string we will call the user defined method len( ) function. This function takes the string as input and then using inbuilt length() method of the string library to print the length of the string period.
  • To concatenate two strings we will call the user defined method concatenate( ) which asks the user to input two strings and then concatenate strings(using inbuilt concat() library method) and store it in another variable.
  • To compare two strings, call user defined method compareStr( ) which compares two Strings by using inbuilt equals() method and returns a True if both String matches else returns False value.
  • To trim a string call user defined trimStr( ). This method takes in one string and then uses the string library trim() method to trim the whitespaces from the string.

Program:

import java.util.*;
import java.lang.*;

public class Main
{
    static String s,s1,s2;
    static Scanner sc = new Scanner(System.in); 
    
    //Find length of a String
    static void len()
    {
        sc.nextLine();
        System.out.println("Enter the string");
        s=sc.nextLine();
        System.out.println("The string length is "+ s.length());
    }

    //Concatenate two Strings
    static void concatenate()
    {
        sc.nextLine();
        System.out.println("Enter First string to concat");
        s = sc.nextLine();
        System.out.println("Enter Second string to concat ");
        s1 = sc.nextLine();
        // Concatenating string s and s1 and storing in s2
        s2 = s.concat(s1);
        System.out.println("The concatenated string of "+s+" and "+s1+" is "+s2);
    }
    
    //Compare two Strings
    static void compareStr()
    {
        sc.nextLine();
        System.out.println("Enter First string to compare");
        s = sc.nextLine();
        System.out.println("Enter Second string to compare");
        s1 = sc.nextLine();
        // b stores the comparison result
        boolean b = s.equals(s1);
        if(b)
            System.out.println("The strings "+s+" and "+s1+ " are equal.");
        else
            System.out.println("The strings "+s+" and "+s1+ " are not equal.");
    }
    
    //Trimming spaces from String
    static void trimStr()
    {
        sc.nextLine();
        System.out.println("Enter string to trim");
        s=sc.nextLine();
        // Storing the trimmed string in s1
        s1= s.trim();
        System.out.println("The string "+s+" after trimming is "+s1);
    }
    
    //Driver method
    public static void main(String []args)
    {
        int ch; 
        while(true)
        {
            System.out.println("---\t String Operations\t---");
            System.out.println("1: Find the length Of String");
            System.out.println("2: Concatenate two strings");
            System.out.println("3: Compare two strings");
            System.out.println("4: String Trimming");
            System.out.println("5: Quit");
            System.out.print("Enter your choice: ");  
            ch = sc.nextInt();
            switch (ch)
            {
                case 1:len();
                    System.out.println("\n");
                    break;

                case 2:concatenate();
                    System.out.println("\n");
                    break;

                case 3:compareStr();
                    System.out.println("\n");
                    break;

                case 4: trimStr();
                    System.out.println("\n");
                    break;

                case 5:
                    System.exit(0);

                default: System.out.println("Invalid choice! Retry");
            }
        }

    }
}

Output:

--- String Operations ---
1: Find the length Of String
2: Concatenate two strings
3: Compare two strings
4: String Trimming
5: Quit
Enter your choice: 1
Enter the string
BtechGeeks
The string length is 10


--- String Operations ---
1: Find the length Of String
2: Concatenate two strings
3: Compare two strings
4: String Trimming
5: Quit
Enter your choice: 3
Enter First string to compare
Exception
Enter Second string to compare
Error
The strings Exception and Error are not equal.


--- String Operations ---
1: Find the length Of String
2: Concatenate two strings
3: Compare two strings
4: String Trimming
5: Quit
Enter your choice: 2
Enter First string to concat
Btech
Enter Second string to concat 
Geeks
The concatenated string of Btech and Geeks is BtechGeeks


--- String Operations ---
1: Find the length Of String
2: Concatenate two strings
3: Compare two strings
4: String Trimming
5: Quit
Enter your choice: 4
Enter string to trim
BtechGeeksBest
The string BtechGeeksBest after trimming is BtechGeeksBest


--- String Operations ---
1: Find the length Of String
2: Concatenate two strings
3: Compare two strings
4: String Trimming
5: Quit
Enter your choice: 4
Enter string to trim
India 
The string India after trimming is India


--- String Operations ---
1: Find the length Of String
2: Concatenate two strings
3: Compare two strings
4: String Trimming
5: Quit
Enter your choice: 5

Have you mastered basic programming topics of java and looking forward to mastering advanced topics in a java programming language? Go with these ultimate Advanced java programs examples with output & achieve your goal in improving java coding skills.

Test Yourself Menu Driven Program In Java Questions:

  1. Menu driven program in java using methods?
  2. Menu driven program in java for string operations?
  3. Java string program examples?
  4. Java program to perform string operations?
  5. String operations in java program?
  6. How to do string programs in java?
  7. How to do menu driven program in java?
  8. How to create menu driven program in java?

Java Program to Find Area and Perimeter of Square

In the previous article we have discussed Java Program to Find Area and Perimeter of Rectangle

In this article we will discuss about how to find area and perimeter of a square.

Program to Find Area and Perimeter of Square

Before going into the program, let’s see how we find the area and perimeter of a square.

Formula for area of Square = side*side

Formula for perimeter of Square = 4*side

Where,

  •  side*side represents the length*breadth of the square. (each side of square is same)
  • 4*side represents the 4*length of side of the square. (each side of square is same)

Example:

Example- To find Area of Square

When length of each side of square = 4
Then area of square => area = side*side
                                 => area = 4*4
                                 => area = 16
Example- To find Perimeter of Square

When length of each side of square = 4
Then area of square => perimeter = 4*side
                                 => perimeter = 4*4
                                 => perimeter =16

Now, let’s see the program.

Don’t miss the chance of Java programs examples with output pdf free download as it is very essential for all beginners to experienced programmers for cracking the interviews.

Method-1: Java Program to Find Area and Perimeter of Square By Using Static Values

In this method, the side length of square is already declared in the program and stored by the variable sideLength. Which will be used to calculate the area and perimeter of square by using the formula.

import java.util.*;

public class JavaProgram
{
    public static void main(String args[])
    {
        int sideLength;
        // length of each side of square is given
        sideLength = 5;
        //finding area of square
        int area = sideLength*sideLength;
        //finding perimeter of square
        int perimeter = 4*sideLength;
        
        System.out.print("Area of Square = " +area);
        
        System.out.print("\nPerimeter of Square = " +perimeter);
    }
   
}
Output:

Area of Square = 25
Perimeter of Square = 20

Method-2: Java Program to Find Area and Perimeter of Square By Using Dynamic Values

In this method, the side length of square will be taken as user input and then the area and perimeter of square will be calculated as per method-1. Method-1 and Method-2 is same the difference is only in assigning side length value of square.

import java.util.*;

public class JavaProgram
{
    public static void main(String args[])
    {
        int side, perimeter, area;
        //Scanner class object created fro taking user input
        Scanner sc = new Scanner(System.in);
        //taking the length of any side of square from the user
        System.out.print("Enter length of any side of square : ");
        side = sc.nextInt();
        
        //finding area of square
        area = side*side;
        //finding perimeter of square
        perimeter = 4*side;
        
        System.out.print("Area of Square = " +area);
        
        System.out.print("\nPerimeter of Square = " +perimeter);
    }
}
Output:

Enter length of any side of square : 4
Area of Square = 16
Perimeter of Square = 16

Method-3: Java Program to Find Area and Perimeter of Square By Using User Defined Method

In this method, first the side length of square will be taken by the user and this value will be passed to the user defined method calculate() where the logic to find area and perimeter is implemented.

import java.util.*;

public class JavaProgram
{
    public static void main(String args[])
    {
        int side;
        //Scanner class object created fro taking user input
        Scanner sc = new Scanner(System.in);
        //taking the length of any side of square from the user
        System.out.print("Enter length of any side of square : ");
        side = sc.nextInt();
        calculate(side);
    }
    
    public static void calculate(int sideLength)
    {
        //finding area of square
        int area = sideLength*sideLength;
        //finding perimeter of square
        int perimeter = 4*sideLength;
        
        System.out.print("Area of Square = " +area);
        
        System.out.print("\nPerimeter of Square = " +perimeter);
    }
   
}
Output:

Enter length of any side of square : 4
Area of Square = 16
Perimeter of Square = 16

Explore complete java concepts from the Java programming examples and get ready to become a
good programmer and crack the java software developer interview with ease.

Answer these:

  1. Write a program in java to accept the diagonal of a square find and display the area and perimeter?
  2. Write a program in java to find the area perimeter and diagonal of a square?
  3. Write a java program to find the area and perimeter of square and circle using interface?
  4. Write a program to input the area of a square and find its perimeter?
  5. How to find the area and the perimeter of a square?
  6. How to get area and perimeter of a square?
  7. Write a java program to find the perimeter of a square?
  8. Java program to find area and perimeter of triangle?
  9. Program to find area of rectangle using inheritance in java?
  10. Java program to find area and perimeter of square?
  11. Java program to calculate area and perimeter of square?
  12. Area of square program in java using scanner?
  13. Safe square area program in java?
  14. Java program to find area of square?

Related Java Programs:

Java Program to Find Product of Digits of a Number

Java Program to find product of digits of a number

In the previous article, we have seen Java Program to Find Sum of Digits of a Number

In this article we are going to see how to find product of digits of a number using Java programming language. Program to calculate product of digits of a number is depends on product of digits of a number. C program to find product of n numbers using while loop, helps the sum of digits of a number in java.

Read Also: Java Program to Find Product of Sum of Odd Digits and Even Digits of a Given Number

Java Program to Find Product of Digits of a Number

As we know dividing any number by 10, the remainder always gives the unit place digit. The number will be reduced to zero after we exhausted all its digits. We will use this theory to get our output.

Example:

528= 5*2*8 = 80
341= 3*4*1= 12
12= 1*2= 2
80451= 8*0*4*5*1 = 0

Let’s see different ways to find product of digits of a number.

Method-1: Java Program to Find Product of Digits of a Number By Using Modulo Operator and Number is Integer Value

Approach:

  1. Create Scanner class object.
  2. Ask user to enter the number.
  3. Declare a variable product = 1 to store the sum of the digits.
  4. Use the modulo operator to obtain the unit place digit and multiply it to product.

Program:

import java.util.Scanner;

public class Main
{
    public static void main(String[] args) 
    {
        // creating scanner class object
        Scanner sc = new Scanner(System.in);
        // taking input from user
        System.out.print("Enter a number: ");
        int n = sc.nextInt();
        // calling method to find product of digits
        int product = findProduct(n);
        // printing product of digits
        System.out.println("Product of each digit of the number: " + product);
    }

    //user defined method
    //to find product of each digits of a number
    private static int findProduct(int n) 
    {
        // variable to store product
        int product = 1;
        while (n > 0) {
            // extracting last digit and multiplying to sum
            product *= n % 10;
            n /= 10;
        }
        return product;
    }
}

Output:

Enter a number: 12
Product of each digit of the number: 2

Method-2: Java Program to Find Product of Digits of a Number By Using Modulo Operator and Number is Numeric String Value

Approach:

  1. Use the Integer.parseInt() method to covert the numeric string into an integer.
  2. Declare a variable product=1 to store the product of the digits.
  3. Use the modulo operator to obtain the unit place digit and multiply it to product.

Program:

public class Main
{
    public static void main(String[] args) 
    {
        String num = "345";
        
        //calling method to find sum of digits
        int sum = findProduct(num);
        //printing product of digits
        System.out.println("Product of each digit of the number: " + sum);
    }
    
    //user defined method
    private static int findProduct(String num) 
    {
        //convert numeric string into int type
        int n = Integer.parseInt(num);
        //variable to store product
        int product = 1;
        while (n > 0) 
        {
            //extracting last digit and multiplying to product
            product *= n % 10;
            n /= 10;
        }
        return product;
    }
}

Output:

Product of each digit of the number: 60

Grab the opportunity to learn all effective java programming language concepts from basic to advance levels by practicing these Java Program Examples with Output

Test Yourself:

  1. Write a program to find product of digits of a number python?
  2. Write a java program to display product of 1 to 5 numbers?
  3. Product of numbers from 1 to 10 in java?
  4. Sum and product of digits of a number in python?
  5. Program to calculate product of digits of a number?
  6. Sum and product of digits of a number in c?
  7. Java program to find product of digits of a number?
  8. Find the number java program?
  9. Java program to find lucky number?
  10. How to find the factor of a number in java?
  11. How to get digits of number in java?
  12. Product of digits of a number in c++?

Related Java Programs:

Java Program to Compute 2(a2+b2) where Value of a and b are Given

Java Program to Compute 2(a2+b2) where Value of a and b are Given

In the previous article we have discussed Java Program to Compute an-bn when n is a natural number

In this program we are going to see how to compute  2(a2+b2)   where value of a and b are given by using Java Programming language. For example, Take two positive integers from the user verify if a b 3 a 3 b 3 3a 2b 3ab 2 in java,  a1 b2 c 3 program in java by using Equations in java and Java equation solver.

Also Read: Java Program to Compute an-bn when n is a Natural Number

Java Program to Compute 2(a2+b2) where Value of a and b are Given

The formula of 2(a2+b2) is given below.

2(a2+b2)  = (a + b)2 + (a – b)2 =[( a + b )( a + b )] + [( a – b )( a – b )]

Now we will convert this into a valid Java expression.

Let x= 2(a2+b2)

=  (( a + b )*( a + b )) + (( a – b )*( a – b ))

Example:

Suppose a=4 and b=2, then

2(a2+b2)
= (( a + b )*( a + b )) + (( a - b )*( a - b ))
= (( 4 + 2 ) * ( 4 + 2)) + (( 4 - 2 ) * ( 4 - 2 ))

Now let’s see different ways to compute 2(a2+b2)

Method-1: Java Program to Compute 2(a2+b2) where Value of a and b are Given By Using Static Input Value

Approach:

  • Declare and initialize two integer variables say a and b.
  • By using the formula compute 2(a2+b2).
  • Print the result.

Program:

class Main
{
    public static void main(String[] args)
    {
        //declare the first integer variable with an integer value
        int a= 4;
        System.out.println("a= " +a);
        //declare the second integer variable with an integer value
        int b= 2;
        System.out.println("b= "+b);
        //declare another integer variable and assigned the formulated value to it.
        int c=(( a + b ) * ( a + b )) + (( a - b )*( a - b ));
        System.out.println("2(a2+b2)= "+c);
    }
}
Output:

a= 4
b= 2
2(a2+b2)= 40

Method-2: Java Program to Compute 2(a2+b2) where Value of a and b are Given By Using User Input Value and Pow() Function

Approach:

  • Declare two integer variables say a and b.
  • Take the value of a and b as user input by using Scanner class.
  • Then By using the formula compute 2(a2+b2).
  • Print the result.

Program:

import java.lang.Math;
import java.util.Scanner;
class Main
{
    public static void main(String[] args)
    {
        //create object of scanner class.
        Scanner sc=new Scanner(System.in);
        System.out.print("a= ");
        //Take the first input from the user.
        int a= sc.nextInt();
        System.out.print("b= ");
        //Take the second input from the user.
        int b= sc.nextInt();
        //declare another double variable and assigned the formulated value to it.
        double c=(Math.pow(a+b,2)) + (Math.pow(a-b,2));
        System.out.println("2(a2+b2)= "+(int)c);
    }
}
Output:

a= 4
b= 5
2(a2+b2)= 82

Method-3: Java Program to Compute 2(a2+b2) where Value of a and b are Given By Using User Defined Method

Approach:

  • Declare two integer variables say a and b.
  • Take the value of a and b as user input by using Scanner class.
  • Then call a user defined method say computeValue() and pass a and b as parameter.
  • Then inside method by using the formula compute 2(a2+b2).
  • Print the result.

Program:

import java.util.Scanner;
class Main
{
    public static void main(String[] args)
    {
        //create object of scanner class.
        Scanner sc=new Scanner(System.in);
        System.out.print("a= ");
        //Take the first input from the user.
        int a= sc.nextInt();
        System.out.print("b= ");
        //Take the second input from the user.
        int b= sc.nextInt();
        //call the user defined method
        computeValue(a,b);
    }
    //define the method
    public static void computeValue(int a, int b)
    {
        //declare another integer variable and assigned the formulated value to it.
        int c=(( a + b )*( a + b )) + (( a - b )*( a - b ));
        System.out.println("2(a2+b2)= "+c);
    }
}
Output:

a= 8
b= 10
2(a2+b2)= 328

Grab the opportunity to learn all effective java programming language concepts from basic to advance levels by practicing these Java Program Examples with Output.

Answer yourself:

  • Write a program in java to find the roots of a quadratic equation?
  • Write a program that solves for x in a quadratic equation of the form?
  • a b whole square program in java?
  • Quadratic equation in java?
  • a=1 b=2 c 3 program in java?

Related Java Programs: