Number Based Problems
Number Based Problems
Basic Temlate:
import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
System.out.println("Enter the number:");
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
//Intialization
for(int i = 1; i < n; i++)
{
if (n % i == 0)
//Logic
}
if (check)
System.out.println("Yes");
else
System.out.println("No");
}
}
Factors :
A factor of a number is a number which divides into it exactly
without leaving remainder.
👍
Example:
1. Finding the factors of a number
import java.util.*;
class Main{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
for(int i = 1; i<=n; i++){
if(n%i==0){
System.out.println(i);
}
}
}
}
Iteration Table:
1. Find a number it is a prime number or not.(Prime number is a number
its divisible by 1 and itself.)[example., 5 has a factor of 1,5].
import java.util.*;
class Main{
import java.util.*;
class Main{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int count =0;
for(int i =1; i<=n; i++){
if(n%i==0){
Count++;
}
}
if(count>3){
System.out.println(“Composite Number”);
}
else{
System.out.println(“Not Composite Number”);
}
}
}
class Main{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int sum=0;
for(int i=1; i<n;i++){
if(n%i==0){
sum = sum+i;
}
}
if(sum==n){
System.out.println("Perfect");
}
else{
System.out.println("Not a Perfect");
}
}
}
4. Find if a number is a abundant Number. (Here sum of factor is greater
than the number itself.) [ example 12 = factors 1,2,3,4,6 = 16>12.
import java.util.*;
class Main{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int sum=0;
for(int i=1; i<n;i++){
if(n%i==0){
sum = sum+i;
}
}
if(sum>n){
System.out.println("Abudant");
}
else{
System.out.println("Not a Abudant");
}
}
}
import java.util.*;
class Main{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int sum=0;
for(int i=1; i<n;i++){
if(n%i==0){
sum = sum+i;
}
}
if(sum<n){
System.out.println("Deficient");
}
else{
System.out.println("Not a Deficient");
}
}
}
6. Pronic Number:
Find if a number is pronic (or) Not pronic number is the product
of two consecutive integers, n(n+1). E.g. 56 = 7(7+1)
import java.util.*;
class Main{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int sum=0;
for(int i=1; i<n;i++){
if(n%i==0){
if(i*(i+1)==n){
sum = i;
}
}
}
if(sum!=0){
System.out.println("Pronic");
}
else{
System.out.println("Not Pronic");
}
}
}
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
long num = scanner.nextLong();
for (long factor = 1; factor <= num; factor++) {
if (num % factor == 0)
System.out.print(factor + ",");
}
// System.out.print(num);
}
}
8.
Count the number of factors for the given number.
import java.util.Scanner;
public class FactorCounter {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
long num = scanner.nextLong();
long count = 0;
for (long checkFactor = 1; checkFactor <= num; checkFactor++) {
if (num % checkFactor == 0)
count++;
}
System.out.println(count);
}
}
import java.util.Scanner;
sumFactors2 += i;
}
}
11.
1 + 2 + 5 = 8(sum of factors)"}'>Given 2 integer inputs, check whether
the two numbers are betrothed numbers or not. If the sum of factors
of num1 is one more than the num2 and sum of factors fo num2 is
one more than num1, its Betrothed numbers.
Note: Dont include the number as factor for finding sum of factors
i.e., 10 -> 1 + 2 + 5 = 8(sum of factors)
import java.util.Scanner;
12.Given an input, print the factors of the number in pairs such that
the
product of the pair gives the input.
import java.util.Scanner;
import java.util.Scanner;
long max;
long step;
long lcm;
while (true) {
if (max % num1 == 0) {
if (max % num2 == 0) {
lcm = max;
break;
}
}
max = max + step;
}
System.out.println(lcm);
}
}
14. Find the greastest common divisor between the given two numbers.
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int num1 = scanner.nextInt();
int num2 = scanner.nextInt();
int gcd = 1;
for (int fact = 1; fact <= num1 && fact <= num2; fact++) {
if (num1 % fact == 0 && num2 % fact == 0) {
gcd = fact;
}
}
System.out.println(gcd);
}
}