Free Data Structures and Algorithms Course









Subscribe below and get all best seller courses for free !!!










OR



Subscribe to all free courses

Write a menu‐driven class to accept a number from the user and check whether it is a Automorphic number or not in Java.

Program to demonstrate a menu‐driven class to accept a number from the user and check whether it is a Automorphic number or not in Java.

package com.hubberspot.java.example;

import java.util.Scanner;

public class AutomorphicTest {

    public static void main(String[] args) {

        char program;
        Scanner scanner = new Scanner(System.in);
        do { 

            System.out.println("Press 'A' for Automorphic Number Test or 'E' for Exit");

            program = scanner.nextLine().toUpperCase().charAt(0);

            if(program != 'A' && program != 'E') { 
                System.out.println();
                System.out.println("You entered : " + program);
                System.out.println("Please enter P or E to proceed ... try again");
                System.out.println();
                continue;
            } 
            else { 
                switch( program )
                {
                case 'A': 
                    System.out.print( "Enter number for Automorphic Number checking: " );

                    int number = scanner.nextInt();
                    String line = scanner.nextLine();

                    if( isAutomorphicNumber( number ) )
                    {
                        System.out.println( number + " is a Automorphic number." );
                    }
                    else
                    {
                        System.out.println( number + " is not a Automorphic number." );
                    }
                    break;
                }
            }

            System.out.println();

            if( program == 'E') { 
                System.out.println("Exiting from the program...");
            }
        }
        while(program != 'E');


    }

    private static boolean isAutomorphicNumber(int number) {

        int square = number * number;

        String numberToString = "" + number;
        String squareToString = "" + square;
        
        if( squareToString.endsWith( numberToString ) )
        {
            return true;
        }
        return false;

    }

}



Output of the program : 

 

Write a program to calculate and print the sum of the following series: Sum(x) = x/2 + x/5 + x/8 + … + x/100.

Program to calculate and print the sum of the following series: Sum(x) = x/2 + x/5 + x/8 + … + x/100 in Java.

package com.hubberspot.java.series.problems;

import java.util.Scanner;

public class Series {

    public static void main(String[] args) {
        
        Scanner scanner = new Scanner(System.in);
        
        System.out.println("Enter the value of x : ");
        
        double x = scanner.nextDouble();        
        double sumOfSeries = 0;
        double numerator = x;
        double denominator;
        double term;

        for(int i = 2; i <= 100; i = i + 3) { 
            
            denominator = i;
            term = numerator / denominator;
            sumOfSeries = sumOfSeries + term;
            
        }
        
        System.out.println("Sum of the series : " + sumOfSeries);
        
    }
}


Output of the program : 

 

Write a program to calculate and print the sum of the following series: Sum(x) = 2 – 4 + 6 – 8 + 10 - 12 … - 100

Program to demonstrate how to calculate and print the sum of the following series:
Sum(x) = 2 – 4 + 6 – 8 + 10 - 12 … - 100 in Java.


package com.hubberspot.java.series.problems;

public class Series {

    public static void main(String[] args) {

        int sumOfSeries = 0;
        
        int i = 2;
        
        while( i <= 100 ) {
            
            if(i % 4 == 0) { 
                
                sumOfSeries = sumOfSeries - i;
            
            }
            else {
                
                sumOfSeries = sumOfSeries + i;
            
            }
            
            i = i + 2;
        }
        
        System.out.println("Sum of the series : " + sumOfSeries);

    }

}


Output of the program : 

 
 
© 2021 Learn Java by Examples Template by Hubberspot