Monday 15 February 2016

Program to take input of a number and check whether it is a Smith number

Write a program to take input of a number and check whether it is a Smith number or not.


import java.util.Scanner;

public class Test {
    public static void main(String[] args) {   
       Scanner sc=new Scanner(System.in);
       System.out.print("Enter a number: ");
       int num = sc.nextInt();
       
       
     
       
        if(isSmithNumber(num)){
            System.out.println(num+" is Smith Number");
        }else{
            System.out.println(num+" is not Smith Number");

        }
    }
   
   
    public static boolean isSmithNumber(int num) {

        int sumOfDigits = getSumOfDigits(num);

        int sumOfPrimeFactors = getSumOfPrimeFactors(num);

        if (sumOfDigits == sumOfPrimeFactors) {
            return true;
        } else {
            return false;

        }

    }
   
   

    public static int getSumOfPrimeFactors (int num){       
      int sum=0;
     
      int numCopy = num;
     
      int divisor=2;     
      while(divisor<numCopy && divisor>1){
          if(num%divisor==0){
              sum = sum + getSumOfDigits(divisor);
              num=num/divisor;
              divisor--;
          }
          divisor++;
      }
     
      return sum;
     
    }
   
    public static int getSumOfDigits (int num){       
        int sum = 0;

        while (num > 0) {
            int digit = num % 10;
            sum = sum + digit;
            num = num / 10;
        }

        return sum;

    }
   
}

Java program which prints the pattern

Write a Java program which prints the following pattern on the console when the input for no. of rows (n=5).


Ans:

public class Test {
    public static void main(String[] args) {
        int rows=5;
        upper(rows);
        lower(rows-1);
    }
    static void upper(int rowCount){
        int starCount=1;
        int spaceCount=rowCount-1;
        for(int i=1;i<=rowCount;i++){
            for(int j=1;j<=spaceCount;j++){
                System.out.print("  ");
            }
            for(int j=1;j<=starCount;j++){
                System.out.print("* ");
            }
            starCount+=2;
            spaceCount--;
            System.out.println();
        }
    }
    static void lower(int rowCount){
        int starCount=rowCount*2-1;
        int spaceCount=1;
        for(int i=1;i<=rowCount;i++){
            for(int j=1;j<=spaceCount;j++){
                System.out.print("  ");
            }
            for(int j=1;j<=starCount;j++){
                System.out.print("* ");
            }
            starCount-=2;
            spaceCount++;
            System.out.println();
        }
    }

}

Java program which prints the pattern

Write a Java program which prints the following pattern on the console when the input for no. of rows (n=5).



Ans:

public class TestPattern {
    public static void main(String[] args) {
        int rows=5;
        print(rows);
    }
    static void print(int rowCount){
        int symbolCount=1;
        int spaceCount=rowCount-1;
        for(int i=1;i<=rowCount;i++){
            for(int j=1;j<=spaceCount;j++){
                System.out.print("  ");
            }
            for(int j=1;j<=symbolCount;j++){
                if(j%2==0){
                    System.out.print("* ");
                }else{
                    System.out.print(i+" ");
                }
              
            }
            symbolCount+=2;
            spaceCount--;
            System.out.println();
        }
    }  

}

Java program which prints the pattern

Write a Java program which prints the following pattern on the console when the input for no. of rows (n=5).


A.

public class TestPattern {
    public static void main(String[] args) {
        int rows=5;
        print(rows);
    }
    static void print(int rowCount){       
        for(int i=1;i<=rowCount;i++){
           
            boolean isEvenRow = i%2==0;
            if(isEvenRow){
                System.out.print("  ");
            }
           
            int columnCount = isEvenRow?(rowCount-2):rowCount;
           
            for(int j=1;j<=columnCount;j++){
                System.out.print("* ");               
            }
           
            System.out.println();
        }
    }
   

}

Program to check the longest word and shortest word in a string

Write a java program to check the longest word and shortest word in a string.Also check their lengths.


public class Test {
    public static void main(String[] args) {

        String str = "This, is";
       
       
       
        Test t=new Test();

       
        t.findLongAndShortWord(str);

    }
   
   

    public void findLongAndShortWord(String str){
        str=str+" ";
        String longestWord="";
        String shortestWord="";
       
        int maxLength=Integer.MIN_VALUE;
        int minLength=Integer.MAX_VALUE;
       
        String word="";
       
        for(int i=0; i<str.length(); i++){
            char ch=str.charAt(i);
            if(Character.isWhitespace(ch)){
                if(word.length()>maxLength){
                    longestWord=word;
                    maxLength =word.length();
                }
               
                if(word.length()<minLength){
                    shortestWord=word;
                    minLength =word.length();
                }
                word="";
               
            }else if(Character.isLetter(ch)){
                word+=ch;
            }
           
        }
       
        System.out.println("longest word= "+longestWord+" length= "+longestWord.length());
        System.out.println("shortest word= "+shortestWord+" length= "+shortestWord.length());

    }
   
   
}

Java program to check for Fascinating Number

Write a Program in Java to input a number and check whether it is a Fascinating Number or not..
Fascinating Numbers : Some numbers of 3 digits or more exhibit a very interesting property. The property is such that, when the number is multiplied by 2 and 3, and both these products are concatenated with the original number, all digits from 1 to 9 are present exactly once, regardless of the number of zeroes.
Let’s understand the concept of Fascinating Number through the following example:
Consider the number 192,
192 x 1 = 192
192 x 2 = 384
192 x 3 = 576
Concatenating the results : 192384576
It could be observed that ‘192384576’ consists of all digits from 1 to 9 exactly once. Hence, it could be concluded that 192 is a Fascinating Number.
Some examples of fascinating Numbers are : 192, 219, 273, 327, 1902, 1920, 2019 etc.

Programming Code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
/**
* The class FascinatingNumber inputs a number and checks if it a Fascinating Number or not
* @author : www.guideforschool.com
* @Program Type : BlueJ Program - Java
*/
 
import java.util.*;
class FascinatingNumber
{
    boolean isUnique(String q)
    {
        int A[] = {0,0,0,0,0,0,0,0,0,0}; //to store frequency of every digit from '0' to '9'
        int i, flag = 0;
        char ch;
        for(i=0; i<q.length(); i++)
        {
            ch = q.charAt(i);
            A[ch-48]++;
            /*  increasing A[5] if ch='5' as '5'-48 = 53-48=5
             *  (ASCII values of '0' to '9' are 48 to 57) */
        }
 
        for(i=1; i<10; i++)
        {
            //checking if every digit from '1' to '9' are present exactly once or not
            if(A[i]!=1)
            {
                flag = 1; //flag is set to 1 if frequency is not 1
                break;
            }
        }
 
        if(flag == 1)
            return false;
        else
            return true;
    }
    public static void main(String args[])
    {
        Scanner sc = new Scanner(System.in);
        FascinatingNumber ob = new FascinatingNumber();
 
        System.out.print("Enter a number : ");
        int n = sc.nextInt();
        String p = Integer.toString(n); //converting the number to String
 
        if(p.length()<3)
            System.out.println("Number should be of atleast 3 digits.");
 
        else
        {
            String s = Integer.toString(n*1) + Integer.toString(n*2) + Integer.toString(n*3);
            /*  Joining the first, second and third multiple of the number
             *  by converting them to Strings and concatenating them*/
            if(ob.isUnique(s))
                System.out.println(n+" is a Fascinating Number.");
            else
                System.out.println(n+" is not a Fascinating Number.");
        }      
    }
}

Output:

Enter a number : 273
273 is a Fascinating Number.
 
Enter a number : 853
853 is not a Fascinating Number.
 
Enter a number : 95
Number should be of atleast 3 digits.

Java Program to check for Disarium Number

Write a Program in Java to input a number and check whether it is a Disarium Number or not.
Note: A number will be called DISARIUM if sum of its digits powered with their respective position is equal to the original number.
For example 135 is a DISARIUM
(Workings 11+32+53 = 135, some other DISARIUM are 89, 175, 518 etc)

Programming Code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
/**
* The class Disarium inputs a number and checks whether
* it is a Disarium number or not
* @author : www.javaforschool.com
* @Program Type : BlueJ Program - Java
*/
import java.io.*;
class Disarium
    {
    public static void main(String[] args)throws IOException
        {
            BufferedReader br=new BufferedReader (new InputStreamReader(System.in));
            System.out.print("Enter a number : ");
            int n = Integer.parseInt(br.readLine());
            int copy = n, d = 0, sum = 0;
            String s = Integer.toString(n); //converting the number into a String
            int len = s.length(); //finding the length of the number i.e. no.of digits
             
            while(copy>0)
            {
                d = copy % 10; //extracting the last digit
                sum = sum + (int)Math.pow(d,len);
                len--;
                copy = copy / 10;
            }
             
            if(sum == n)
                System.out.println(n+" is a Disarium Number.");
            else
                System.out.println(n+" is not a Disarium Number.");
        }
    }

Output:

Enter a Number : 135
135 is a Disarium Number.
Enter a Number : 219
219 is not a Disarium Number.
Enter a Number : 89
89 is a Disarium Number.