0% found this document useful (0 votes)
4 views12 pages

Number Based Problems

The document provides a series of Java code examples for solving various number-based problems, including finding factors, determining if a number is prime, composite, perfect, abundant, deficient, pronic, amicable, and betrothed. It also includes methods for counting factors, summing factors, printing factor pairs, and calculating the least common multiple (LCM) and greatest common divisor (GCD) of two numbers. Each example is structured with a basic template and includes explanations of the mathematical concepts involved.

Uploaded by

HARISH S ECE
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views12 pages

Number Based Problems

The document provides a series of Java code examples for solving various number-based problems, including finding factors, determining if a number is prime, composite, perfect, abundant, deficient, pronic, amicable, and betrothed. It also includes methods for counting factors, summing factors, printing factor pairs, and calculating the least common multiple (LCM) and greatest common divisor (GCD) of two numbers. Each example is structured with a basic template and includes explanations of the mathematical concepts involved.

Uploaded by

HARISH S ECE
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

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{

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 ==2){
System.out.println(“Prime Number”);
}
else{
System.out.println(“Not a Prime Number”);
}
}
}

2. Find a number it is a composite number or not.(Its a number which


has more than one factor(excluding 1,n) example., 8=2,4= 2 factors.

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”);
}
}
}

3. Find a number it is a perfect number or not.(Here sum of factor is


equal to the number itself.) [ example 6 = factors 1+2+3 = 6.
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("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");
}
}
}

5. Find a number if a number is a deficient number.(Sum of factors is


less tha the number itself.)[ example 21=> factors 1,3,7= 11<21.]

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");
}
}
}

7. Print all the factors of the given number.

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);
}
}

9. Find the sum of factors of the given number.


import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
long num = scanner.nextLong();
long sum = 0;
for (long checkFactor = 1; checkFactor <= num; checkFactor++) {
if (num % checkFactor == 0)
sum += checkFactor;
}
System.out.println(sum);
}
}

10. 1 + 2 + 5 = 8(sum of factors)","6":1}'>. Find whether the given two


numbers are amicable pair or not.If sum of factors of first number
equals to the 2nd number as well as sum of factors of 2nd number
equal to first number, its Amicable.
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;

public class Main {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
long num1 = scanner.nextLong();
long num2 = scanner.nextLong();
long sumFactors1 = 0, sumFactors2 = 0;

for (long i = 1; i <= num1/2; i++) {


if (num1 % i == 0)
{
sumFactors1 += i;
}
}

for (long i = 1; i <= num2/2; i++) {


if (num2 % i == 0){

sumFactors2 += i;
}
}

if (num1 == sumFactors2 && num2 == sumFactors1)


System.out.println("Amicable Pair");
else
System.out.println("Not an Amicable Pair");
}
}

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;

public class BetrothedPair {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
long num1 = scanner.nextLong();
long num2 = scanner.nextLong();
long sum1 = 0, sum2 = 0;

for (long i = 1; i <= num1 / 2; i++) {


if (num1 % i == 0) sum1 += i;
}

for (long i = 1; i <= num2 / 2; i++) {


if (num2 % i == 0) sum2 += i;
}
if (sum1 - 1 == num2 && sum2 - 1 == num1)
System.out.println("Betrothed Number");
else
System.out.println("Not a Betrothed Number");
}
}

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;

public class Main {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
long input = scanner.nextLong();

for (long i = 1; i * i <= input; i++) {


if (input % i == 0) {
System.out.println(i + " * " + (input / i));
}
}
}
}

13.Find the LCM of the given two numbers.

import java.util.Scanner;

public class Main {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
long num1 = scanner.nextLong();
long num2 = scanner.nextLong();

long max;
long step;
long lcm;

if (num1 > num2) {


max = num1;
step = num1;
} else {
max = num2;
step = num2;
}

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);
}
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy