Tuesday, July 16, 2019

Smith Number | Programming | Eclipse JAVA | Follow on Youtube for more



import java.util.Scanner;
public class smith_number
{
public static int sum(int number)
{
int sum = 0;
while(number!=0)
{
int mod = number%10;
sum = sum+mod;
number = number/10;
}
return sum;
}
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter the number");
int n = sc.nextInt();
int n1 = n;
int check = 2;
int result1 = sum(n);
int result2 = 0;
while(n!=0)
{
if(n%check==0)
{
n = n/check;
result2  = result2+sum(check);
}
else if(check>n1)
break;
else
check++;
}
if(result1==result2)
System.out.println(n1+" is a Smith Number");
else
System.out.println(n1+" is not a Smith Number");
sc.close();
}
}
/*
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

Input   : n = 13
Output  : No
Prime factorization = 13 and 13 = 13,
But 13 is not a smith number as it is not
a composite number
*/

Keith Number | Programming | Eclipse JAVA | Follow on Youtube for more


import java.util.*;
public class keith_number
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter the number");
String s = sc.next();//taking it as String input
int length = s.length();//taking the string length
int n1 = Integer.parseInt(s);//converting the string into integer
int n2 = n1;//making copy of the number for digit separation operation
int a[] = new int[100];
int insert = length;
while(n2!=0)
{
int mod = n2%10;
a[--insert] = mod;//each digits are stored in array separately
n2 = n2/10;
}
int flag = length;
int sum = 0;
int count= 0;
while(true)//Going for infinite loop
{
for(int i=count;i<flag;i++)//adding last n numbers, where n stands for number of digits
{
sum += a[i];
}
if(sum>n1)//if the sum exceeds the original number, we exit the program
{
System.out.println(n1+" is not a Keith Number");
System.exit(0);
}
if(sum==n1)//if sum is equal to number, than it is a Keith number
{
System.out.println(n1+" is a Keith Number");
System.exit(0);
}
a[flag++] = sum;//adding the sum back to the array, and length of array iteration also exceeds
count++;//increasing the array starting value
sum=0;//initializing the sum back to zero
}
}
}
/*
A n digit number x is called Keith number if it appears in a special sequence
 (defined below) generated using its digits. The special sequence has first n terms
 as digits of x and other terms are recursively evaluated as sum of previous n terms.

The task is to find if a given number is Keith Number or not.

Examples:

Input : x = 197
Output : Yes
197 has 3 digits, so n = 3
The number is Keith because it appears in the special
sequence that has first three terms as 1, 9, 7 and
remaining terms evaluated using sum of previous 3 terms.
1, 9, 7, 17, 33, 57, 107, 197, .....

Input : x = 12
Output : No
The number is not Keith because it doesn't appear in
the special sequence generated using its digits.
1, 2, 3, 5, 8, 13, 21, .....

Input : x = 14
Output : Yes
14 is a Keith number since it appears in the sequence,
1, 4, 5, 9, 14, ...
 */

EMIRP Number | Programming | Eclipse JAVA | Follow on Youtube for more


import java.util.Scanner;
public class EMIRP_number
{
public static boolean check(int n)
{
int count=0;
for(int i=1;i<n;i++)
{
if(n%i==0)
count++;
}
if(count==1)
return true;
return false;
}
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter a number");
int n = sc.nextInt();
int n1 = n;
boolean b1 = check(n);
int rev = 0;
while(n!=0)
{
int mod = n%10;
rev  = rev*10+mod;
n/=10;
}
boolean b2 = check(rev);
if(b1==true && b2==true)
System.out.println(n1+" is an EMIRP number");
else
System.out.println(n1+" is not an EMIRP number");
}
}
/*
Input : n = 13
Output : 13 is Emirp!
Explanation :
13 and 31 are both prime numbers.
Thus, 13 is an Emirp number.

Input : n = 27
Output : 27 is not Emirp.
*/

ICSE COMPUTER SCIENCE SPECIMEN PAPER | QUESTION 7 | PLEASE DO WATCH THE VIDEO

  import  java.util.*; class icse_specimen_1 {     public static void main(String[] args)     {         Scanner sc1 = new Scanner(System.in)...