Found 7442 Articles for Java

Find all divisors of a natural number in java

Ankith Reddy
Updated on 25-Jun-2020 12:16:02

9K+ Views

Following is the Java program which prints all the divisors of a given number.Programimport java.util.Scanner; public class DivisorsOfNaturalNumber {    public static void main(String args[]) {       Scanner sc = new Scanner(System.in);       System.out.println("Enter required number :");       int num = sc.nextInt();             for(int i = 1; i

Prime factors in java

Arjun Thakur
Updated on 06-Nov-2023 03:33:46

38K+ Views

Factors are the numbers we multiply to get another number.factors of 14 are 2 and 7, because 2 × 7 = 14.Some numbers can be factored in more than one way.16 can be factored as 1 × 16, 2 × 8, or 4 × 4.A number that can only be factored as 1 times itself is called a prime number.The first few primes are 2, 3, 5, 7, 11, and 13.The list of all the prime-number factors of a given number is the prime factors of a number. The factorization of a number into its prime factors and expression of ... Read More

Prime factors in java

Arjun Thakur
Updated on 06-Nov-2023 03:33:46

38K+ Views

Factors are the numbers we multiply to get another number.factors of 14 are 2 and 7, because 2 × 7 = 14.Some numbers can be factored in more than one way.16 can be factored as 1 × 16, 2 × 8, or 4 × 4.A number that can only be factored as 1 times itself is called a prime number.The first few primes are 2, 3, 5, 7, 11, and 13.The list of all the prime-number factors of a given number is the prime factors of a number. The factorization of a number into its prime factors and expression of ... Read More

Check if a large number is divisible by 3 or not in java

Arjun Thakur
Updated on 25-Jun-2020 12:05:58

5K+ Views

If the sum of the digits of a number is divisible by 3, then the number is divisible by 3.Some examples of numbers divisible by 3 are as follows.The number 85203 is divisible by 3 because the sum of its digits (8 + 5 + 2 + 0 + 3 = 18) is divisible by 3.The number 79154 is not divisible by 3 because the sum of its digits (7 + 9 + 1 + 5 + 4 = 26) is not divisible by 3.Programimport java.util.Scanner; public class DivisibleBy3 {    public static void main(String args[]) {       Scanner sc = new Scanner(System.in);       System.out.println("Enter a number :");       String num = sc.nextLine();       int digitSum = 0;             for(int i = 0; i

Check if a large number is divisible by 3 or not in java

Arjun Thakur
Updated on 25-Jun-2020 12:05:58

5K+ Views

If the sum of the digits of a number is divisible by 3, then the number is divisible by 3.Some examples of numbers divisible by 3 are as follows.The number 85203 is divisible by 3 because the sum of its digits (8 + 5 + 2 + 0 + 3 = 18) is divisible by 3.The number 79154 is not divisible by 3 because the sum of its digits (7 + 9 + 1 + 5 + 4 = 26) is not divisible by 3.Programimport java.util.Scanner; public class DivisibleBy3 {    public static void main(String args[]) {       Scanner sc = new Scanner(System.in);       System.out.println("Enter a number :");       String num = sc.nextLine();       int digitSum = 0;             for(int i = 0; i

Check if a large number is divisible by 11 or not in java

Chandu yadav
Updated on 25-Jun-2020 12:03:01

1K+ Views

A number is divisible by 11 if the difference between the sum of its alternative digits is divisible by 11.i.e. if (sum of odd digits) – ( sum of even digits) is 0 or divisible by 11 then the given number is divisible by 11.Programimport java.util.Scanner; public class DivisibleBy11 {    public static void main(String args[]) {       Scanner sc = new Scanner(System.in);       System.out.println("Enter a number :");       String num = sc.nextLine();       int digitSumEve = 0;       int digitSumOdd = 0;            for(int i = 0; i

Check if a large number is divisible by 11 or not in java

Chandu yadav
Updated on 25-Jun-2020 12:03:01

1K+ Views

A number is divisible by 11 if the difference between the sum of its alternative digits is divisible by 11.i.e. if (sum of odd digits) – ( sum of even digits) is 0 or divisible by 11 then the given number is divisible by 11.Programimport java.util.Scanner; public class DivisibleBy11 {    public static void main(String args[]) {       Scanner sc = new Scanner(System.in);       System.out.println("Enter a number :");       String num = sc.nextLine();       int digitSumEve = 0;       int digitSumOdd = 0;            for(int i = 0; i

To check divisibility of any large number by 9 in java

Arjun Thakur
Updated on 25-Jun-2020 12:01:29

1K+ Views

If the sum of the digits of a number is divisible by 9, then the number is divisible by 9.Some examples of numbers divisible by 9 are as follows. The number 51984 is divisible by 9 because the sum of its digits (5+ 1 + 9 + 8 + 4 = 27) is divisible by 9.The number 91403 is not divisible by 9 because the sum of its digits (9 + 1 + 4 + 0 + 3 = 17) is not divisible by 9.Programimport java.util.Scanner; public class DivisibleBy9 {    public static void main(String args[]) {       Scanner sc = new Scanner(System.in);       System.out.println("Enter a number :");       String num = sc.nextLine();       int digitSum = 0;       for(int i = 0; i

To check divisibility of any large number by 9 in java

Arjun Thakur
Updated on 25-Jun-2020 12:01:29

1K+ Views

If the sum of the digits of a number is divisible by 9, then the number is divisible by 9.Some examples of numbers divisible by 9 are as follows. The number 51984 is divisible by 9 because the sum of its digits (5+ 1 + 9 + 8 + 4 = 27) is divisible by 9.The number 91403 is not divisible by 9 because the sum of its digits (9 + 1 + 4 + 0 + 3 = 17) is not divisible by 9.Programimport java.util.Scanner; public class DivisibleBy9 {    public static void main(String args[]) {       Scanner sc = new Scanner(System.in);       System.out.println("Enter a number :");       String num = sc.nextLine();       int digitSum = 0;       for(int i = 0; i

GCD of an array of numbers in java

Ankith Reddy
Updated on 25-Jun-2020 12:00:32

2K+ Views

ProgramFollowing is the example to calculate the GCD of the numbers of an array.Live Demopublic class GCDOfArrayofNumbers{    public static int gcd(int a,int b){       int res = 0;       while (b > 0){          int temp = b;          b = a % b;          a = temp;          res = a;       }       return res;    }    public static void main(String arg[]){       int[] myArray = {3, 6, 8};       int result = gcd(myArray[0],myArray[1]);       for(int i = 2; i < myArray.length; i++){          result = gcd(result, myArray[i]);       }       System.out.println("Gcd of n numbers is: "+result);    } }OutputGCD of n numbers is: 1

Advertisements