Final
Final
Question: 1
A Prime-Adam integer is n positive integer (without lending zeros) which is a prime as well as an Adam
number.
Prime number: A number which has only two factors, i.e. J and the number itself. Example: 2, 3, 5, 7 . .
. etc.
Adam number: The square of n number and the square of its reverse and reverse to each other.
Example: If n=13 and reverse of 'n'= 31, then,
date(d, y)
20. Initialize daysInMonth array with the number of days in each month, considering if the
year is a leap year.
21. Initialize month index i to 0.
22. Loop through the months subtracting the days until d is less than the days in the current
month.
23. Get the month name by calling month_name(i + 1).
24.Get the suffix by calling suffix(d).
25. Print the formatted date in the form d + suffix + " " + month_name + ", " + y.
isleap(y)
26. Return true if y is divisible by 400.
27. Return false if y is divisible by 100 but not by 400.
28.Return true if y is divisible by 4 but not by 100.
29. Return false otherwise.
suffix(d)
30. Return "th" if d is between 11 and 13.
31.Switch on d % 10:
32.Return "st" for 1.
33.Return "nd" for 2.
34.Return "rd" for 3.
35. Return "th" for other cases.
month_name(i)
36. Switch on i:
37. Return the corresponding month name for the month index i.
CODE
import java.util.*;
class days_to_date
{
public static void main()
{
Scanner obj = new Scanner(System.in);
System.out.println("Input the no. days");
int d = obj.nextInt();
if(d<1 || d>366)
{
System.out.println("NO. of days out of range");
System.exit(0);
}
System.out.println("Input the no of years");
int y = obj.nextInt();
if( y <1000 || y >9999)
{
System.out.println("Years out of range");
System.exit(0);
}
System.out.println("Input the value of N");
int N = obj.nextInt();
if(N<1 || N > 100)
{
System.out.println("Days after(N days) out of range");
System.exit(0);
}
obj.close();
date(d,y);
if(N+d >366)
{
y++;
}
date(d+N,y);
}
static boolean isleap(int y)
{
if(y%100 ==0)
{
return true;
}
else if(y%4 == 0)
{
return true;
}
else if(y%400 ==0 )
{
return true;
}
else
{
return false;
}
}
static void date(int d ,int y)
{
int i =0,c=0;
while(d>31)
{
if(i== 1 || i==3 || i==5 || i==7 || i==8 || i== 10 || i==12)
{
d=d-31;
}
if(i == 6 || i==4 || i== 11|| i==9)
{
d=d-30;
}
if(i==2)
{
if(isleap(y))
{
d=d-29;
}
else
{
d=d-28;
}
}
i++;
c++;
}
String m_n= month_name(i);
String suff= suffix(d);
System.out.println(d+" "+suff+" "+m_n+","+y);
}
static String suffix(int d)
{
String st="";
if(d==1)
{
st="st";
}
if(d==2)
{
st="nd";
}
if(d==3)
{
st="rd";
}
if(d>3)
{
st= "th";
}
return st;
}
static String month_name(int i)
{
String m;
switch(i)
{
case 1: m="January";
break;
case 2: m="February";
break;
case 3: m="March";
break;
case 4: m="April";
break;
case 5: m="May";
break;
case 6: m="June";
break;
case 7: m="July";
break;
case 8: m="August";
break;
case 9: m="September";
break;
case 10: m="October";
break;
case 11: m="November";
break;
case 12: m="December";
break;
default: m="";
}
return m;
}
}
QUESTION:3
A Goldbach number is a positive even integer that can be expressed as the sum of two odd
primes.
Note: All even integer numbers greater than 4 are Goldbach numbers.
Example: 6 = 3 + 3
10 = 3 + 7
10 = 5 + 5
Hence, 6 has one odd prime pair 3 and 3. Similarly, 10 has two odd prime pairs, i.e. 3 and 7,
5 and 5.
Write a program to accept an even integer ‘N’ where N > 9 and N < 50. Find all the odd
prime pairs whose sum is equal to the number ‘N’.
Test your program with the following data and some random data:
Example 1:
INPUT: N = 14
OUTPUT: PRIME PAIRS ARE: 3, 11
7, 7
Example 2:
INPUT: N = 30
OUTPUT: PRIME PAIRS ARE: 7, 23
11, 19
13, 17
Example 3:
INPUT: N = 17
OUTPUT: INVALID INPUT. NUMBER IS ODD.
Example 4:
INPUT: N = 126
OUTPUT: INVALID INPUT. NUMBER OUT OF RANGE.
ALGORITHM
1. START
MAIN FUNCTION ()
2. Initialize and object ‘obj’ for the input of scanner.
3. Print “Input no. greater than 2”.
4. Store the input in a variable N.
5. Initialize two loop variable i =1 and j=1.
6. Initialize a for loop with the variable i which runs from 1 to 25 and increases by 1 and
implement steps 7 to 10.
7. Initialize another for loop with variable j which runs from 1 to 50 and increases by 1
and implement steps 8 to 10.
8. Now if both i and j are prime and even implement steps 9 to10.
9. Now if the sum of i and j is equal to the number input implement step 10
10.Print i”+”j+”=”+N
isPrime(int a) Function
11. Initialize a variable c equal to 0 and i equal to 1.
12. Initialize a for loop which runs form 1 to less than equal to a and increases by 1 and
implement steps 13.
13. If (a%i is equal to zero increase c by 1.
14. If c is equal to 2 return true else return false.
15.STOP
PROGRAM
import java.util.*;
class goldbach
{
public static void main()
{
Scanner obj =new Scanner(System.in);
System.out.println("Input the no.");
int N =obj.nextInt();
for(int i =1;i<=25;i++)
{
for(int j = 1;j<=50;j++)
{
if(isPrime(i) && isPrime(j) && i%2!=0 && j%2!=0)
{
if(i+j == N)
{
System.out.println(i+"+"+j+"="+N);
}
}
}
}
}
static boolean isPrime(int a)
{
int c=0;
for(int i = 1;i<=a;i++) //prime
{
if(a%i == 0)
c++;
}
if(c == 2)
return true;
else
return false;
}
}
Ques: 4
Write a program to check if a no. is Circular Prime or not andalso print
all the combinations of the no.
A prime number is said to be a circular prime if after any cyclic
permutations of the digits, it remains a prime.
Examples:
Input : n = 113
Output : Yes
All cyclic permutations of 113 (311
and 131) are prime.
Input : 1193
Output : Yes
Algorithm
1) START
2) Initialize n
3) Input the number from the user which is to be checked in the variable n
4) Create a Function checkprime which takes a input n
5) Initialize c to 0 and i to 1
6) Repeat steps 6 to 8 till I less than n
7) If remainder of n divided by i is equal to 0
8) Increase c by 1
9) Increase i by 1
10) If c is equal to 2
11) Return true otherwise
12) Return false
13) Function is_circular_prime takes an input n
14) Initialize t1 to n and count to 0
15) Repeat steps 15 and 16 till t1 is not equal to 0
16) Increase count by 1
17) Divide t1 by 10
18) Initialize num to n and p to -1
19) Initialize rem to the remainder of num divided by 10
20) Initialize d to the quotient of num divided by 10
21) Reinitialize num to 10 to the power count-1 multiplied by rem and add d
22) If the return of function checkprime called using variable num if false
23) Initialize p to 1
24) Print num
25) Repeat above steps 19 to 24 till n is not equal to num
26) If p is equal to -1
27) Print Yes! It is a circular prime number
28) Otherwise Print No It is not a circular prime number
29) Call function is_circular_prime using variable n from the main function
30) STOP
Java Program
import java.util.*;
class circular_prime
{
public void main()
{
Scanner obj = new Scanner(System.in);
System.out.println("Input the no.");
int n = obj.nextInt();
is_circular_prime(n);
}
boolean checkprime(int n)
{
int c =0;
for(int i =1;i<n;i++)
{
if(n%i == 0)
{
c++;
}
}
if(c==2)
{
return false;
}
else
{
return true;
}
}
void is_circular_prime(int n)
{
int t1=n;int count=0;
while(t1!=0)
{
count++;
t1/=10;
}
int num = n;int p=-1;
System.out.println("All the circular combination");
do
{
int rem = num%10;
int d = num/10;
num = (int) (Math.pow(10,count-1)*rem)+d;
if(!checkprime(num))
{
p=1;
}
System.out.println(num);
}while(n != num);
if(p==-1)
{
System.out.println("Yes! it is a circular prime no.");
}
else
{
System.out.println("No it is not a circluar prime no.");
}
}
}
OUTPUT
Input the no.
1193
All the circular combination
3119
9311
1931
1193
Yes! it is a circular prime no.
Question: 5
Given two positive numbers M and N, such that M is between 100 and 10000 and N is less
than 100. Find the smallest integer that is greater than M and whose digits add up to N. For
example, if M = 100 and N = II, then the smallest integer greater than 100 whose digits add
up to II is 119.
Write a program to accept the numbers M and N from the user and print the smallest required
number whose sum of all its digits is equal to N. Also, print the total number of digits present
in the required number. The program should check for the validity of the inputs and display an
appropriate message for an invalid input.
Test your program with the sample data and some random data:
Given two positive numbers M and N,
than 100. Find the smallest integer that
example, if M = 100 and N = I l, then
11 is 119.
Write a program to accept the numbers
M - 1500
M — 99
such that M is between 100 and 10000 and N is less
is greater than M and whose digits add up IO N. For
the snallest integer greater than 100 whose digits add
M and N from the user and print the smallest required
Example I
INPUT:
N-11
OUTPUT:
Example 2
INPUT:
N = 25
OUTPUT:
Example 3
INPUT:
N=ll
OUTPUT:
Example 4
INPUT:
130
OUTPUT:
M - 100
The required number = 119
Total number of digits = 3
The required number = 1699
Total number of digits = 4
INVALID INPUT
M-112
INVALID INPUT
ALGORITHM
1. START
2. Initialize a Scanner object to read user input.
3. Prompt the user to input the first value m.
4. Read the first value m.
5. Prompt the user to input the second value n.
6. Read the second value n.
7. Check if m is outside the range [100, 10000]:
8. If true, print "Invalid input: m must be between 100 and 10000" and exit the program.
9. Check if n is outside the range [1, 100]:
10. If true, print "Invalid input: n must be between 1 and 100" and exit the program.
11.Loop from i = m to i <= 10000:
12.Calculate the digit sum of i using the digit_sum function.
13.Compare the digit sum with n:
14. If they are equal, print "The required number = i".
15. Print the number of digits in i using the no_of_digits function.
16.Break the loop.
17.Close the Scanner object.
18.End
Function digit_sum(n):
19. Initialize sum to 0.
20. While n is greater than 0:
21. Get the last digit d of n.
22.Add d to sum.
23.Remove the last digit from n by dividing n by 10.
24.Return sum.
Function no_of_digits(n):
25.Initialize count to 0.
26.While n is greater than 0:
27.Remove the last digit from n by dividing n by 10.
28.Increment count by 1.
29. Return count.
30. STOP.
CODE
import java.util.*;
class digit_sum
{
public static void main()
{
Scanner obj = new Scanner(System.in);
System.out.println("Input the first value");
int m = obj.nextInt();
System.out.println("Input te second value ");
int n = obj.nextInt();
if(m > 10000 || m<100)
{
System.out.println("invalid input");
System.exit(0);
}
if(n>100 || n<1)
{
System.out.println("invalid input");
System.exit(0);
}
for(int i =m;i<=10000;i++)
{
if(digit sum(i) == n)
{
System.out.println("The required number ="+i);
System.out.println("Number of Digits ="+no_digit(i));
break;
} } }
static int digit sum(int n)
{
int t = n;
int d;int sum=0;
while(t>0) //armstrong
{
d = t%10;
sum += d;
t/= 10;
}
return sum;
}
static int no_digit(int n)
{
int t=n,d,c=0;
while(t>0) //armstrong
{
d = t%10;
c++;
t/= 10;
}
return c;
}
}
Question: 6
A Composite Magic number is a positive integer which is composite as well as a magic number.
Composite number: A composite number is a number that has more than two factors.
Magic number:
For example: IO
Factors are: l, 2, 5, IO
A magic number is a number in which the eventual sum of the digits is equalto I
For example: +0=1
Accept two positive integers m and n, where m is less than n as user input. Display the number
of
Composite magic integers that are in the range between m and n (both inclusive) and output
them along with the frequency, in the format specified below.
Test your program with the sample data and some random data:
Example I:
INPUT: m=10
n=lOO
OUTPUT:
THE COMPOSITE MAGIC INTEGERS ARE:
10, 28, 46, 55, 64, 82, 91, 100
FREQUENCY OF COMPOSITE MAGIC INTEGERS IS: 8
n = 99
Example 2:
INPUT: m = 1200
n = 1300
OUTPUT:
THE COMPOSITE MAGIC INTEGERS ARE:
1207, 1216, 1225, 1234, 1243, 1252, 1261, 1270, 1288
FREQUENCY OF COMPOSITE MAGIC INTEGERS IS: 9
ALGORITHM
1. START
2. Initialize a Scanner object to read user input.
3. Prompt the user to input the range n and m.
4. Read the values n and m.
5. Initialize a counter c to 0.
6. Loop from i = n to i <= m:
7. Check if i is a magic number using the isMagic function.
8. Check if i is a composite number using the isComposite function.
9. If both conditions are true, print i and increment the counter c.
10.Print the total number of magic composite numbers found.
11.Close the Scanner object.
12. End
Function isMagic(a):
13. Initialize number to a and sum to 0.
14. While number is greater than or equal to 10:
15.Reset sum to 0.
16.While number is greater than 0:
17.Extract the last digit of number.
18.Add the digit to sum.
19.Remove the last digit from number.
20.Set number to sum.
21. Return true if number equals 1, else return false.
Function isComposite(a):
22. If a is less than or equal to 1, return false.
23.Initialize a counter c to 0.
24. Loop from i = 1 to i <= a:
25. If a is divisible by i, increment c.
26. Return true if c is greater than 2, else return false.
27.STOP.
CODE
import java.util.*;
class magic_composite
{
public static void main()
{
Scanner obj = new Scanner(System.in);
System.out.println("Input the range");
int n = obj.nextInt();
int m=obj.nextInt();
int c=0;
for(int i =n;i<=m;i++)
{
if(ismagic(i) && iscomposite(i))
{
System.out.println(i);
c++;
}
}
System.out.println("No of magic composite no. "+c);
}
}
Question: 7
Write a program to check if a no. is Smith Number or not.
A Smith number is a composite number where the sum of its digits is equal to
the sum of its prime factors
Ex: -
Input: n = 4
Output: Yes
Prime factorization = 2, 2 and 2 + 2 = 4
Therefore, 4 is a smith number
Input: n = 6
Output: No
Prime factorization = 2, 3 and 2 + 3 is
not 6. Therefore, 6 is not a smith number
Input: n = 666
Output: Yes
Prime factorization = 2, 3, 3, 37 and
2 + 3 + 3 + (3 + 7) = 6 + 6 + 6 = 18
Therefore, 666 is a smith number
Algorithm
1) START
2) Input the no. from the user
3) Set sum1 and sum 2 to 0
4) Call function sum using variable sum1
5) Function Sum accepts the variable and stores it in a variable n
6) Initialize s=0 and r=0
7) Repeat steps 8 to 10 till n! = 0
8) Save the remainder of n divided by 10 in variable r
9) Add r in the variable s
10) Divide the number n by 10
11) Return s to main function
12) Value of snow stored in sum1
13) Function primesum is called by variable s2
14) Punction primesum accepts the no. and stores in a variable n
15) Initialize f=2, t1, t=n, d, sum =0
16) Repeat steps from 17 to 24 till t is greater than 1
17) If remainder of t divided by f is 0 repeat steps from 18 to 23 otherwise
perform the step 24
18) Store the value of f in t1
19) Repeat the steps 20 to 22 till t1 is not equal to 0
20) Store the remainder of t1 divided by 10 in variable d
21) Add the value of d in sum
22) Divide t1 by 10
23) Divide t by f
24) Increase f by 1
25) Return sum to the main function
26) Value of sum is stored in s2
27) Now in main function if s1 is equal to s2
28) Print It is a smith no.
29) Otherwise
30) Print It is not a Smith no.
31) STOP
Java Program
import java.util.*;
class smudge
{
int sum(int n)
{
int s=0;
while(n!=0)
{
int r=n%10;
s+=r;
n/=10;
}
return s;
}
boolean checkprime(int n)
{
int c=0;
for(int i =1;i<n;i++)
{
if(n%i == 0)
{
c++;
}
}
if(c==2)
{
return true;
}
else
{
return false;
}
}
int primesum(int n)
{
int f=2;int t1;int t=n;int d,sum=0;
while(t>1)
{
if(t%f == 0)
{
t1=f;
while(t1!=0)
{
d=t1%10;
sum+=d;
t1/=10;
}
t/=f;
}
else
{
f++;
}
}
return sum;
}
public void main()
{
Scanner obj = new Scanner(System.in);
System.out.println("Input the no.");
int n = obj.nextInt();
int s1=sum(n);
int s2=primesum(n);
if(s1==s2)
{
System.out.println("Yes it is a smith no.");
}
else
{
System.out.println("No it is not a smith no.");
}
System.out.println(s1);
System.out.println(s2);
}
}
Question: 8
Write a Java program that calculates the difference between two given dates. The
program should prompt the user to input two dates in the format (dd mm yyyy)
and then output the difference in terms of years, months, and days. Your program
should handle leap years and different month lengths correctly.
Example: 1
Input the first date (dd mm yyyy):
12 05 2020
Input the second date (dd mm yyyy):
25 12 2023
Difference = 3 years 7 months 13 days
Example: 2
Input the first date (dd mm yyyy):
28 02 2020
Input the second date (dd mm yyyy):
01 03 2021
Difference = 1 years 0 months 1 days
ALGORITHM
1. Start
2. Initialize Scanner to read user input.
3. Prompt the user to input the first date (d1, m1, y1).
4. Read the first date (d1, m1, y1).
5. Prompt the user to input the second date (d2, m2, y2).
6. Read the second date (d2, m2, y2).
7. Call date_difference(d1, m1, y1, d2, m2, y2) to calculate and print the difference
between the two dates.
8. Close Scanner.
date_difference(d1, m1, y1, d2, m2, y2)
9. Initialize daysInMonth array with the number of days in each month:
10.daysInMonth = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}
11.Check if the first year is a leap year using isLeap(y1):
12.If isLeap(y1) is true, set daysInMonth[1] = 29
13. Adjust days if needed:
14. If d1 > d2:
15. Decrement m2 by 1
16. Add the number of days in the previous month to d2
17.Adjust months if needed:
18. If m1 > m2:
19. Decrement y2 by 1
20.Add 12 to m2
21.Calculate the difference:
22.fd = d2 - d1
23.fm = m2 - m1
24.fy = y2 - y1
25.Print the difference in years, months, and days:
26.Difference = fy years fm months fd days
isLeap(y)
27.If y is divisible by 400, return true.
28.Else if y is divisible by 100, return false.
29.Else if y is divisible by 4, return true.
30.Else, return false.
CODE
import java.util.*;
class date_difference
{
public static void Main()
{
Scanner obj = new Scanner(System.in);
System.out.println("Input the first date");
int d1 = obj.nextInt();
int m1 = obj.nextInt();
int y1 = obj.nextInt();
System.out.println("Input the second date");
int d2 = obj.nextInt();
int m2 = obj.nextInt();
int y2 = obj.nextInt();
date_difference(d1,m1,y1,d2,m2,y2);
}