Professional Documents
Culture Documents
prime_adam_merged_final_1 (1)
prime_adam_merged_final_1 (1)
prime_adam_merged_final_1 (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,
(13)2 = 169 (31)2 = 961
961 which is reverse of 169 thus 13, is an Adam number.
Accept two positive integers m and n, where m is less than n as user input. Display all Prime-
Adam integers that are in the range between m and n (both inclusive) and output them along
with the frequency, in the format given below:
Test your program with the following data and some random data:
Example 1
INPUT: m=10 n = 100
OUTPUT: THE PRIME-ADAM INTEGERS ARE:11 13
31FREQUENCY OF PRIME-ADAM INTEGERS IS: 3
Example 2 THE PRIME-ADAM INTEGERS ARE: 11 13
31FREQUENCY OF PRIME-ADAM INTEGERS IS: 3
INPUT:m = 100 n =200
OUTPUT: THE PRIME-ADAM INTEGERS ARE: 101 103
113FREQUENCY OF PRIME-ADAM INTEGERS IS: 3
Example 3
INPUT: m = 50 n = 70
OUTPUT: THE PRIME-ADAM INTEGERS
ARE: FREQUENCY OF PRIME-ADAM
INTEGERS IS: O
Example 4
INPUT: m = 700 n
=450OUTPUT: INVALID
INPUT.
ALGORITHM
1. MAIN Function
2. START
import
java.util.*;
class
prime_adam
{
public static void main()
{
Scanner obj = new
Scanner(System.in);
System.out.println("input the
no.");
int n = obj.nextInt();
if(isPrime(n) && is_adam(n))
{
System.out.println("It is a prime adam no.");
}
else
{
System.out.println("It is not a prime adma no.");
}
}
int rev=0,
rem=0; while
(n > 0)
{
rem = n % 10;
rev = (rev * 10) +
rem; n = n / 10;
}
return rev;
}
}
Question: 2
Design a program to accept a day number (between 1 and 366), year (in 4 digits) from the
user to generate and display the corresponding date. Also, accept ‘N’ (1
<= N <= 100) from the user to compute and display the future date correspondingto ‘N’ days
after the generated date. Display an error message if the value of the day number, year and
N are not within the limit or not according to the conditionspecified. Test your program with
the following data and some random data:
Example 1 INPUT: DAY NUMBER: 255
YEAR: 2018
DATE AFTER (N DAYS): 22
OUTPUT: DATE: 12 TH SEPTEMBER, 2018
DATE AFTER 22 DAYS: 4 TH OCTOBER, 2018
Example 2INPUT: DAY NUMBER: 360
YEAR: 2018
DATE AFTER (N DAYS): 45
OUTPUT: DATE: 26 TH DECEMBER, 2018
DATE AFTER 45 DAYS: 9 TH FEBRUARY, 2019
Example 3 INPUT: DAY NUMBER: 500
YEAR: 2018
DATE AFTER (N DAYS): 33
OUTPUT: DAY NUMBER OUT OF RANGE.
1. Start
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.
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 oddprime
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
START
MAIN FUNCTION ()
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
CODE
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
1. START
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
START
}
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 tothe 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 = 666Output:
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
CODE
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);
}
}