Pages

Tuesday 29 November 2016

Sethuraman wants to change his profile picture on Facebook.

Sethuraman wants to change his profile picture on Facebook. Now Facebook has some restriction over the dimension of a picture that we can upload.
The minimum dimension of the picture can be 
L x L, where  L  is the length of the side of a square.
Now Sethuraman has  N  photos of various dimensions.
Dimension of a photo is denoted as 
W x H 
where 
W  - width of the photo and  H  - Height of the photo
When any photo is uploaded following events may occur:
[1] If any of the width or height is less than L, the user is prompted to upload another one. Print "UPLOAD ANOTHER" in this case.
[2] If width and height, are large enough then it is accepted. Print "
ACCEPTED" in this case.
(b) else the user is prompted to crop it. Print "
CROP IT" in this case.
(quotes are only for clarification)
Given L, N, W and H as input, print appropriate text as output.
Input specification:
The first line contains 
L.
The second line contains 
N, the number of photos.
Following N lines each contains two space separated integers 
W  and  H.
Output specification:
Print appropriate width height and the text for each photo in a new line.
Constraints:
1 <= 
L,W,H  <= 10000
1 <= 
N  <= 1000
Sample Input:
180
3
640 480
120 300
180 180
Sample Output:
640 480 CROP IT
120 300 UPLOAD ANOTHER
180 180 ACCEPTED


Source Code:-

import java.util.*;

class Test{
    public static void main(String[] args){
        Scanner s=new Scanner(System.in);
        int L=s.nextInt();
        int n=s.nextInt();
        int W,H;
        for(int i=0;i<n;i++){
            W=s.nextInt();
            H=s.nextInt();
            if(W<L || H<L){
                System.out.println(W+" "+H+" UPLOAD ANOTHER");
            }
            else if(W==H && H==L){
                System.out.println(W+" "+H+" ACCEPTED");
            }
            else{
                System.out.println(W+" "+H+" CROP IT");
            }
        }
    }
}

Smith number

 A  Smith number is a composite number, the sum of whose digits is the sum of the digits of its prime factors obtained as a result of prime factorization (excluding 1). The first few such numbers are 4, 22, 27, 58, 85, 94, 121 ………………..
Examples:
1.  666
Prime factors are 2, 3, 3, and 37
Sum of the digits are (6+6+6) = 18
Sum of the digits of the factors (2+3+3+(3+7)) = 18
2.   4937775
Prime factors are 3, 5, 5, 65837
Sum of the digits are (4+9+3+7+7+7+5) = 42
Sum of the digits of the factors (3+5+5+(6+5+8+3+7)) = 42
Write a program to input a number (<1000) and display whether the number is a Smith number or not.
Input specification:The first line contains the value for N
Output specification:
print “ n is a Smith number“ or “ n is not a Smith number” or “n is an Invalid input”
Sample Input 1:
666
Sample Output 1:
666 is a Smith number
Sample Input 2:
102
Sample Output 2:
102 is not a Smith number
Sample Input 3:
1024
Sample Output 3:
1024 is an Invalid input


Source Code:-

import java.util.*;

class Test{
    public static void main(String []args){
        Scanner s=new Scanner(System.in);
        int n=s.nextInt();
        int i=2,m=n;
        int sum=0;
        int sumOfDig=sumOfDigit(n);
        if(n<1000){
            while(n>1){
                if(n%i==0){
                    sum=sum+sumOfDigit(i);
                    n=n/i;
                }
                else{
                    i++;
                }
            }
            if(sumOfDig==sum){
                System.out.println(m+" is a Smith number");
            }
            else{
                System.out.println(m+" is not a Smith number");
            }
        }
        else{
            System.out.println(n+" is an Invalid input");
        }
    }
    private static int sumOfDigit(int num){
        int sum=0;
        while(num>0){
            sum=sum+num%10;
            num/=10;
        }
        return sum;
    }
}

An ISBN (International Standard Book Number) is a ten digit code which uniquely identifies a book.

An ISBN (International Standard Book Number) is a ten digit code which uniquely identifies a book.
The first nine digits represent the Group, Publisher and Title of the book and the last digit is used to check whether ISBN is correct or not.
Each of the first nine digits of the code can take a value between 0 and 9. Sometimes it is necessary to make the last digit equal to ten; this is done by writing the last digit of the code as X.
To verify an ISBN, calculate 10 times the first digit, plus 9 times the second digit, plus 8 times the third and so on until we add 1 time the last digit. If the final number leaves no remainder when divided by 11, the code is a valid ISBN.
For Example:
1. 0201103311 = 10*0 + 9*2 + 8*0 + 7*1 + 6*1 + 5*0 + 4*3 + 3*3 + 2*1 + 1*1 = 55
Since 55 leaves no remainder when divided by 11, hence it is a valid ISBN.
2. 007462542X = 10*0 + 9*0 + 8*7 + 7*4 + 6*6 + 5*2 + 4*5 + 3*4 + 2*2 + 1*10 = 176
Since 176 leaves no remainder when divided by 11, hence it is a valid ISBN.
3. 0112112425 = 10*0 + 9*1 + 8*1 + 7*2 + 6*1 + 5*1 + 4*1 + 3*4 + 2*2 + 1*5 = 71
Since 71 leaves remainder when divided by 11, hence it is not a valid ISBN.
Design a program to accept a ten digit code from the user. For an invalid input, display an appropriate message. Verify the code for its validity in the format specified below:

Input Specifications.
The first line contains - ISBN number
Output Specification :
Print “N is a valid ISBN number” or “ N is not valid ISBN number” or “N is an Invalid Input” 
Sample Input:
007462542X
Sample output
007462542X is a valid ISBN number


Source Code:-

import java.util.*;

class Test{
    public static void main(String []args){
        Scanner s=new Scanner(System.in);
        String isbn=s.next().toUpperCase();
        int sum=0,i=10;
        try{
        if(isbn.length()==10){
            for(char c:isbn.toCharArray()){
                if(c=='X'){
                    sum+=i*10;
                }
                else if(c>='0' && c<='9'){
                    sum+=i*Character.getNumericValue(c);
                }
                else{
                    throw new Exception();
                }
                i--;
            }
            if(sum%11==0){
                System.out.println(isbn+" is a valid ISBN number");
            }
            else{
                System.out.println(isbn+" is not a valid ISBN number");
            }
        }
        else{
            System.out.println(isbn+" is an Invalid input");
        }
        }
        catch(Exception e){
            System.out.println(isbn+" is an Invalid input");
        }
    }
}