Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
7 views10 pages

Some Programs Computer Record

The document contains multiple Java programs that implement various mathematical and number theory concepts, including Smith Numbers, Adam Primes, Vampire Numbers, Lucky Numbers, Denominations, Roman to Number conversion, and Date Difference calculation. Each program includes methods for performing specific calculations and user input handling. The programs demonstrate different algorithms and logic for identifying properties of numbers and converting formats.

Uploaded by

kprahul1111e
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)
7 views10 pages

Some Programs Computer Record

The document contains multiple Java programs that implement various mathematical and number theory concepts, including Smith Numbers, Adam Primes, Vampire Numbers, Lucky Numbers, Denominations, Roman to Number conversion, and Date Difference calculation. Each program includes methods for performing specific calculations and user input handling. The programs demonstrate different algorithms and logic for identifying properties of numbers and converting formats.

Uploaded by

kprahul1111e
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/ 10

SMITH NUMBER

import java.util.*;
public class SmithNumber
{
static int sod(int n)
{
int sum = 0;

while(n > 0)
{
sum += n%10; //Adds each digit of n to sum
n /= 10;
}

return sum;
}

static int sopf(int n)


{
int sum = 0;

int factor = 2;
while(n > 1)
{
if(n%factor == 0)
{
//Add each digit of the prime factor found to the final sum
sum += sod(factor);
n /= factor;
}
else
{
//If n is not divisible by factor, factor is incremented to find the next prime factor
factor += 1;
}
}

return sum;
}

static boolean isComposite(int n)


{
for(int factor = 2; factor < n; factor++)
{
if(n%factor == 0)
{
//If a factor other than 1,n is found, return true, as it is composite
return true;
}
}

return false;
}

public static void main(String[]args)


{
Scanner in = new Scanner(System.in);

int m,n,sum_of_digits,sum_of_prime_factors;
String result = "";

//Getting limits input


System.out.print("Enter lower limit : ");
m = in.nextInt();
System.out.print("Enter upper limit : ");
n = in.nextInt();

//Iterating through each number in range [m,n]


for(int num = m; num <= n; num++)
{
//Skiping the number if prime, as it has to be composite
if(!isComposite(num))
{
continue;
}

sum_of_digits = sod(num);
sum_of_prime_factors = sopf(num);

if(sum_of_digits == sum_of_prime_factors)
{
//Adding the number to final result string, if it is a Smith Number
result = result + num + ",";
}
}
//Checking if any Smith Numbers were found in the limits, and printing appropriate
message
System.out.println(result.length() == 0?"No Smith Numbers found within the
limits":result.substring(0,result.length()-1));
}
}

ADAM PRIME

import java.util.*;
public class AdamPrime
{
static int reverse(int n)
{
int rev = 0;
for(int copy = n; copy >0;copy/=10)
{
rev = rev*10 + copy%10;
}
return rev;
}
static boolean isPrime(int n)
{
for(int i = 2; i < n;i++)
{
if(n%i == 0)
{
return false;
}
}

return true;
}

public static void main(String[]args)


{
Scanner in = new Scanner(System.in);
int m,n;

System.out.print("Enter lower limit : ");


m = in.nextInt();
System.out.print("Enter upper limit : ");
n = in.nextInt();
String result = "";
for(int i = m; i <= n;i++)
{
int a = i*i;
int b = reverse(i) * reverse(i);
if(isPrime(i) && reverse(a) == b)
{
result+=i + ",";
}
}

System.out.println(result.substring(0,result.length() -1));
}
}

Vampire number
import java.util.*;
public class VampireNumber
{
static String sort(String s)
{
String ans = "";
char arr[] = new char[s.length()];
for(int i = 0; i < s.length();i++)
{
arr[i] = s.charAt(i);
}
for(int i = 0; i < s.length(); i++)
{
char c = arr[i];
int min = i;
for(int j = i+1; j <s.length();j++)
{
if(arr[j] < arr[min])
{
min = j;
}
}
char temp = arr[i];
arr[i] = arr[min];
arr[min] = temp;
ans += arr[i];
}
return ans;
}
static int count(int n)
{
return (n + "").length();
}
static boolean comp(int n)
{
for(int i = 2; i < n;i++)
{
if(n%i == 0)
{
return true;
}
}
return false;
}
public static void main(String[]args)
{
Scanner in = new Scanner(System.in);
int n = in.nextInt();

if(!(comp(n) && count(n)%2 == 0))


{
System.out.println("Invalid");
System.exit(0);
}

int len = count(n);


int arr[] = new int[len];
int copy = n;
for(int i = 0; i < len;i++)
{
arr[i] = copy%10;
copy/=10;
}

for(int a = 10; a < Math.pow(10,len/2);a++)


{
if(n%a == 0 && count(a) == len/2 && count(n/a) == len/2)
{
int b = n/a;

String ab = a + "" + b;
String ns = n + "";
ab = sort(ab);
ns = sort(ns);

if(ns.equals(ab))
{
System.out.println(a + " x " + b);
System.exit(0);
}
}
}
}
}

LUCKY NUMBER

import java.util.*;
public class LuckyNumber
{
public static void main(String[]args)
{
Scanner in = new Scanner(System.in);
int n;
n = in.nextInt();
int arr[] = new int[n];

for(int i = 0; i < n;i++)


{
arr[i] = i+1;
}
for(int jump = 2; jump <= n;jump++)
{
int c= 0;
for(int i = 0; i < n;i++)
{
if(arr[i] == 0)
continue;
else
{
c++;
if(c == jump)
{
arr[i] = 0;
c = 0;
}
}
}

for(int i = 0;i < n;i++)


{
if(arr[i] != 0)
{
System.out.println(arr[i]);
}
}
}
}

DENOMINATIONS

import java.util.*;
public class Denominations
{
static void words(int n)
{
String digits[] =
{"ZERO","ONE","TWO","THREE","FOUR","FIVE","SIX","SEVEN","EIGHT","NINE"};
String result = "";
for(int copy = n; copy > 0; copy/=10)
{
result = digits[copy%10] +" " + result;
}

System.out.println(result);
}
static void denom(int n)
{
int copy = n;
int notes[] = {500,200,100,50,20,10,5,2,1};

for(int i = 0; n > 0;n%=notes[i],i++)


{
if(n/notes[i]!=0)
System.out.println(notes[i] + " x " + (n/notes[i]) + " = " + notes[i] * (n/notes[i]));
}
System.out.println("Total = " + copy);
}
public static void main(String[]args)
{
Scanner in = new Scanner(System.in);

System.out.print("Enter amount : ");


int n = in.nextInt();

words(n);
denom(n);

}
}

ROMAN TO NUMBER
import java.util.*;
public class RomanToNumber
{
public static void main(String[]args)
{
Scanner in = new Scanner(System.in);

System.out.print("Enter a valid Roman Number : ");


String s = in.next();

String roman[] = {"M","CM","D","CD","C","XC","L","XL","X","IX","V","IV","I"};


int num[] = {1000,900,500,400,100,90,50,40,10,9,5,4,1};
int ans = 0;

int i = 0;
while(s.length()!=0)
{
if(s.indexOf(roman[i]) == 0)
{
ans += num[i];
s= s.substring(roman[i].length());
}
else
{
i++;
}
}

System.out.println(ans);
}
}

DATE DIFFERENCE

import java.util.*;
public class DateDifference
{
static boolean isLeap(int year)
{
if((year%4 == 0 && year%100 != 0) || year%400 == 0)
return true;
else
return false;
}
public static void main(String[]args)
{
Scanner in = new Scanner(System.in);

System.out.println("Enter dates in dd/mm/yyyy format :");


//Receives user input
String s1 = in.next();
String s2 = in.next();

//First and second dates seperated


int d1 = Integer.valueOf(s1.substring(0,2));
int m1 = Integer.valueOf(s1.substring(3,5));
int y1 = Integer.valueOf(s1.substring(6));

int d2 = Integer.valueOf(s2.substring(0,2));
int m2 = Integer.valueOf(s2.substring(3,5));
int y2 = Integer.valueOf(s2.substring(6));

//monts[2] = 0, February case handled seperately

int months[] = {0,31,0,31,30,31,30,31,31,30,31,30,31};

int ans = 0;

//simulates each day until intitial date becoems final date


while(d1 != d2 || m1 != m2 || y1 != y2)
{
ans++;
d1++;
if((d1 == months[m1] + 1 && m1!=2) || (m1 == 2 && ((d1 == 30 && isLeap(y1)) || (d1 ==
29 && !(isLeap(y1))))))
{
m1++;
d1 = 1;
}
if(m1 == 13)
{
m1 = 1;
y1++;
}
}

System.out.println(ans);

}
}

You might also like