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

Saturday, June 29, 2019

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




import java.util.*;
public class mersenne_number
{
static boolean prime_check(double n)
{
int count = 0;
for(int i=1;i<n;i++)
{
if(n%i==0)
count++;
}
if(count==1)
return true;
else
return false;
}
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter till where mersenne number is to be generated");
int n = sc.nextInt();
double a[] = new double[100];// storing the answers
int count = 0;
for(int i=2;i<n;i++)
{
double number = Math.pow(2, i)-1; //   2^k-1
if(number<=n)
{
boolean result = prime_check(number);
if(result==true)
{
a[count++]  =  number;
}
}
else
{
break;
}
}
if(count!=0)
{
System.out.println("The Mersenne number are as follows :");
for(int i=0;i<count;i++)
{
System.out.println(a[i]);
}
}
else
System.out.println("There are no mersenne number");
}
}
/*
Mersenne Prime is a prime number that is one less than a power of two. In other words,
any prime is Mersenne Prime if it is of the form 2^k-1 where k is an integer greater than
or equal to 2. First few Mersenne Primes are 3, 7, 31 and 127.

The task is print all Mersenne Primes smaller than an input positive integer n.
Examples:

Input: 10
Output: 3 7
3 and 7 are prime numbers smaller than or
equal to 10 and are of the form 2^k-1

Input: 100
Output: 3 7 31
 */

Friday, June 28, 2019

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




import java.util.*;
class Pronic_Number
{
    public static void main1(String []args)
    {
        int num,count=0,d;
        Scanner sc=new Scanner(System.in);
        System.out.println("Enter the number");
        num=sc.nextInt();
        for( int i=1;i<(num/2);i++)//6
        {
            d=i*(i+1);
            if(d==num)
            {
            count=count+1;
            break;
             }
            else
            d=0;
        }
        if(count>0)
        System.out.println(num+" is a pronic number");
        else
        System.out.println(num+" is not a pronic number");
    }
}
/*
 A pronic number is a something which is a product of 2 consecutive numbers.
 
Input  : 6
Output : Pronic Number
Explanation: 6 = 2 * 3 i.e 6 is a product
of two consecutive integers 2 and 3.

Input :56
Output :Pronic Number
Explanation: 56 = 7 * 8 i.e 56 is a product
of two consecutive integers 7 and 8.

Input  : 8
Output : Not a Pronic Number
Explanation: 8 = 2 * 4 i.e 8 is a product of
2 and 4 which are not consecutive integers.
*/


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






import java.util.*;
// Harshad number is also called niven number
public class harshad_number
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter the number to be checked");
int n  = sc.nextInt();
int n1 = n;
int sum = 0;
while(n1!=0)
{
int rem  = n1%10;
sum  = sum + rem;
n1 = n1/10;
}
if(n%sum==0)
{
System.out.println(n+" is a harshad number");
}
else
{
System.out.println(n+" is not a harshad number");
}
}
}
/*
Input: 3
Output: 3 is a Harshad Number

Input: 18
Output: 18 is a Harshad Number

Input: 15   
Output: 15 is not a Harshad Number


54       9
*/

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




Code:
import java.util.Scanner;
public class duck_number
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter the number to be checked");
String s = sc.next();
char c = s.charAt(0);
if(c=='0')
System.out.println(s+" is not a duck number");
else
{
int count = 0;
for(int i=1;i<s.length();i++)
{
c = s.charAt(i);
if(c=='0')
count++;
}
if(count>0)
System.out.println(s+" is a duck number");
else
System.out.println(s+" is not a duck number");
}
}
}
/*
 Input : 707069
Output : It is a duck number.
Explanation: 707069 does not contains zeros at the beginning.

Input : 02364
Output : It is not a duck number.
Explanation: in 02364 there is a zero at the beginning of the number.
 */

Data Types in JAVA | Understanding Variables | UNICODE | LIKE SHARE AND SUBSCRIBE |


This video explains about primitive and non primitive data types in java.
It explains all 8 types of data types:
the arithmetic types:

A. The integral types:
1. byte
2. short
3. int
4. long

B. The floating-point types:
5. float
6. double

C. boolean, the type whose values are either true or false

D. char, the character type whose values are 16-bit Unicode characters

It also explains about Local, Global and Static variable type.

Downloading and Installing Eclipse | Like Share Subscribe|

This video shows you complete process of how to download and install Eclipse.
 It works everytime.

Eclipse Download:  https://www.eclipse.org/downloads/

Please Like Share and Subscribe..


Running program in Command Prompt using Text File | Follow Youtube for More |

This video shows you how you can run simple java program using text file and command prompt. It also teaches you basic commands of command prompt.



Java Environment Setup(100% working)| Running text files from Command Prompt| Susbscribe to Youtube for more

The video shows you how to setup java path, so that you can run java program through command prompt that is using a text file.

Link for Jdk : https://www.oracle.com/technetwork/java/javase/downloads/jdk11-downloads-5066655.html


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