Skip to main content

Posts

Showing posts with the label JAVA

Java Program to check if Number is Armstrong or not

 Logic for  Armstrong   number :)  Armstrong Number An  Armstrong  number is a positive m-digit number that is equal to the sum of the m th  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:  1 1  =  1 2:  2 1  =  2 3:  3 1  =  3 153:  1 3  + 5 3  + 3 3  = 1 + 125+ 27 =  153 125:  1 3  + 2 3  + 5 3  = 1 + 8 + 125 =  134 (Not an Armstrong Number) 1634:  1 4  + 6 4  + 3 4  + 4 4  = 1 + 1296 + 81 + 256 =  1643 //Armstrong number list:0,1,153,370,371,407,1634 //Eg 153 rest to 3(Total number of digit ) each digit if addition come original number. package javaPrograms; import java.util.Scanner; public class ArmstrongNumber { public static vo...

Java Program to reverse the Array elements

 package javaPrograms; import java.util.Scanner; public class ReverseArray { public static void main(String[] args) { Scanner sc = new Scanner (System.in); System.out.println("Enter the number of Elements::"); int n =sc.nextInt(); System.out.println("Enter the "+ n +" number of array elements"); int a[] = new int[n]; for(int i=0;i<n;i++) { a[i]=sc.nextInt(); } System.out.println("The reverse of array Elements are as follow"); for(int i=a.length-1;i>=0;i--) { System.out.println(a[i]); } } }

Java Program to find Maximum element from the Array

 package javaPrograms; import java.util.Scanner; public class MaximumElementInArray { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("Enter the Length of Array::"); int n = sc.nextInt(); System.out.println("Enter the "+n+" number of Array Element::"); int a[] = new int[n]; for(int i=0;i<n;i++) { a[i] =sc.nextInt(); } int max=a[0];    for(int i=1;i<a.length;i++) {    if(a[i]>max) {    max=a[i];    }    }    System.out.println("Maximum Element is::"+max); } }

Java Prgram to find the minimum element in the Array

 package javaPrograms; import java.util.Scanner; public class ArrayMinElement { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("Enter the Length of Array::"); int n = sc.nextInt(); System.out.println("Enter the "+n+" number of Array Element::"); int a[] = new int[n]; for(int i=0;i<n;i++) { a[i] =sc.nextInt(); } int min=a[0];    for(int i=1;i<a.length;i++) {    if(a[i]<=min) {    min=a[i];    }    }    System.out.println("Minimum Element is::"+min); } }

Java program to Sort the Array in ascending order

 package javaPrograms; import java.util.Scanner; public class ArrayAscending { public static void main(String[] args) { //Create object of scanner class Scanner sc = new Scanner(System.in); System.out.println("Enter the number:"); int n= sc.nextInt(); //Declare and initialize Array and other Required variables.. int a[] = new int [n];    int i,j,temp=0;   //Now get all array elements from user System.out.println("Enter" + n   +"number of array elements:  ");//5 for(i=0;i<n;i++) { a[i]=sc.nextInt(); } //Print all array elements System.out.println("All array elements are: ");//4 6 3 2 1 8 for(i=0;i<n;i++) { System.out.println(" "+a[i]); } //Now arrange array elements in ascending order for(i=0;i<n;i++) { for(j=i+1;j<n;j++) { if(a[i]>a[j]) { temp=a[i]; a[i]=a[j]; a[j]=temp; } } } //Print array ...

Java Program to check number is palindrome or not

 package javaPrograms; import java.util.Scanner; public class PalindromeNum { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("Enter the number::"); int no=sc.nextInt(); int temp = no; int rev=0,rem; while(temp!=0) { rem=temp%10; rev = rev*10+rem; temp=temp/10; } if(no==rev) { System.out.println(no+" is Palindrome number"); } else { System.out.println(no+" is not palindrome number"); } } }

Java Prgram to remove duplicate character in a string

 package Java; import java.util.Scanner; public class RemoveDuplicateCharacter { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("Enter the String"); String w = sc.next();   String nw = " "; System.out.println("Original String: "+w); for(int i=0;i<w.length();i++) { char ch = w.charAt(i); if(ch!=' ') { nw = nw + ch; w = w.replace(ch, ' '); } } System.out.println("New word = "+nw); } }

Java program to check string is palindrome or not

 package javaPrograms; import java.util.Scanner; public class Palindrome { public static void main(String args[])     {         Scanner sc = new Scanner(System.in);         System.out.print("Enter the string you want to check:");        String str = sc.next();        String org_str=str;         String rev="";         int len=str.length();         for(int i = len - 1; i >= 0; i--)         {             rev = rev + str.charAt(i);         }         if(org_str.equals(rev))         {             System.out.println("The string is palindrome.");         }         else         {       ...

Java Program to find factorial of a number

Java Program to find factorial of a number:  package javaPrograms; import java.util.Scanner; public class Factorial { public static void main(String[] args) { Scanner sc =  new Scanner(System.in); System.out.println("Enter the number::"); int no = sc.nextInt(); int fact=1; for(int i=1;i<=no;i++) { fact = fact*i; } System.out.println(fact); } }

Java Program to check number is Prime or not

 //Prime number logic: Prime number is the number which is divisible by 1 and it self package javaPrograms; import java.util.Scanner; public class PrimeNumber { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("Enter the number::"); int n= sc.nextInt(); int temp=0; for(int i=2;i<=n-1;i++) {     //2-3 if(n%2==0) { temp= temp+1; } } if(temp==0) { System.out.println("Number is  Prime"); } else { System.out.println("Number is not Prime"); } } }

Java program to find Odd and Even number

 Below is the Java program to find find Odd and Even number. package javaPrograms; public class EvenOdd { public static void main(String[] args) { int a= 91; if(a%2==0) { System.out.println("Number is Even"); } else { System.out.println("Number is odd"); } } } =========================================================== Same program by taking an input from user: package javaPrograms; import java.util.Scanner; public class OddEvenUser { public static void main(String[] args) { int Number; Scanner sc = new Scanner(System.in); System.out.println("\n Please Enter any integer Value: "); Number = sc.nextInt(); if (Number % 2 == 0) { System.out.println("\n You have entered EVEN Number"); } else { System.out.println("\n You have entered ODD Number"); } } }