Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 36

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,
(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
31 FREQUENCY OF PRIME-ADAM INTEGERS IS: 3
Example 2 THE PRIME-ADAM INTEGERS ARE: 11 13
31 FREQUENCY OF PRIME-ADAM INTEGERS IS: 3
INPUT:m = 100 n =200
OUTPUT: THE PRIME-ADAM INTEGERS ARE: 101 103
113 FREQUENCY 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
=450 OUTPUT:
INVALID INPUT.
ALGORITHM

1. MAIN Function
2. START

3. Prompt the user to input a number.


4. Read the number int n from the user.
5. Prime Check Function:
6. Call the function isPrime(n) to check if int n is a prime number.
a. Define isPrime(n):
i. If n is less than or equal to 1, return false.
ii. For each integer iii from 2 to the square root of n:
1. If n is divisible by iii (i.e., n%i==0n \% i == 0n%i==0), return false.
iii. If no divisors are found, return true.
7. Adam Number Check Function:
8. Call the function is_adam(n) to check if n is an Adam number.
a. Define is_adam(n):
i. Reverse the digits of n to get rev.
ii. Square both n and rev to get their squares.
iii. Reverse the digits of the squares of n and rev.
iv. If reverse of n square is equal to square of n reverse, return true.
Otherwise, return false.
9. Back to Main after calling to print the result:
10.If both isPrime(n) and is_adam(n) return true, print "It is a prime Adam number."
11.Otherwise, print "It is not a prime Adam number."
12.STOP
CODE

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

static boolean isPrime(int a)


{
int c=0;
for(int i = 1;i<=a;i++) //prime
{
if(a%i ==
2) c++;
}
if(c != 2)
return
true; else
return false;
}

static boolean is_adam(int n )


{
int rev =
num_rev(n*n); int
rev2 =
num_rev(n);
int sq= rev2
*rev2; if(sq ==
rev)
{
return true;
}
else
{
return false;
}
}

static int num_rev(int n)


{

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 corresponding to ‘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 condition specified. 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.
Example 4 INPUT: DAY NUMBER: 150
YEAR: 2018
DATE AFTER (N DAYS): 330
OUTPUT: DATE AFTER (N DAYS) OUT OF RANGE.
ALGORITHM

1. Start
2. Initialize Scanner to read user input.
3. Prompt the user to input the number of days d.
4. Read the number of days d.
5. Validate if d is in the range 1 to 366.
6. Prompt the user to input the year y.
7. Read the year y.
8. Validate if y is in the range 1000 to 9999.
9. Prompt the user to input the value of
N. 10. Read the value of N.
11. Validate if N is in the range 1 to 100.
12. Call the method date(d, y) to print the initial date.
13. Check if the sum of d and N exceeds 365 or 366 considering leap
years. 14.If the sum exceeds:
15. Increment the year y by 1.
16. Calculate the remaining days and call date with new days and
year. 17.If the sum does not exceed:
18.Call date with the sum of d and N and the same year y.
19.End

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.

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.
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.

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

 START
MAIN FUNCTION ()
 Initialize and object ‘obj’ for the input of scanner.
 Print “Input no. greater than 2”.
 Store the input in a variable N.
 Initialize two loop variable i =1 and j=1.
 Initialize a for loop with the variable i which runs from 1 to 25 and increases by 1 and
implement steps 7 to 10.
 Initialize another for loop with variable j which runs from 1 to 50 and increases by 1
and implement steps 8 to 10.
 Now if both i and j are prime and even implement steps 9 to10.
 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
 Initialize a variable c equal to 0 and i equal to 1.
 Initialize a for loop which runs form 1 to less than equal to a and increases by 1 and
implement steps 13.
 If (a%i is equal to zero increase c by 1.
 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;
}
}
Question: 4
Write a program to check if a no. is Circular Prime or not and also 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
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

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: 119
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

 START
 Initialize a Scanner object to read user input.
 Prompt the user to input the range n and m.
 Read the values n and m.
 Initialize a counter c to 0.
 Loop from i = n to i <= m:
 Check if i is a magic number using the isMagic function.
 Check if i is a composite number using the isComposite function.
 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.
 End
 Initialize number to a and sum to 0.
 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.
 Return true if number equals 1, else return false.
 If a is less than or equal to 1, return false.
23.Initialize a counter c to 0.
 Loop from i = 1 to i <= a:
 If a is divisible by i, increment c.
 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);
}

static boolean ismagic(int a)


{
int number=a;
int sum=0;
while(number>=9)
{
while(number>0)
{
int digit =number%10;
sum+=digit;
number/=10;
}
number = sum;
sum=0;
}
if(number==1)
{
return true;
}
else
{
return false;
}
}

static boolean iscomposite(int a)


{
int c=0;
for(int i = 1;i<=a;i++) //composite
{
if(a%i ==0)
c++;
}
if(c > 2)
return true;
else
return false;
}

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

You might also like