Category Archives: Pratice Problem

Java program to find month using switch statement

import java.io.BufferedReader;
import java.io.InputStreamReader;

public class MonthFinder {
    public static void main(String[] args) {
        InputStreamReader isr = new InputStreamReader(System.in);
        BufferedReader br = new BufferedReader(isr);
        try {
            System.out.println("Enter a numeric month:");
            String input = br.readLine();
            int month = Integer.parseInt(input);
            String strMonth;
            switch (month) {
                case 1:
                    strMonth = "January";
                    break;
                case 2:
                    strMonth = "February";
                    break;
                case 3:
                    strMonth = "March";
                    break;
                case 4:
                    strMonth = "April";
                    break;
                case 5:
                    strMonth = "May";
                    break;
                case 6:
                    strMonth = "June";
                    break;
                case 7:
                    strMonth = "July";
                    break;
                case 8:
                    strMonth = "August";
                    break;
                case 9:
                    strMonth = "September";
                    break;
                case 10:
                    strMonth = "October";
                    break;
                case 11:
                    strMonth = "November";
                    break;
                case 12:
                    strMonth = "December";
                    break;
                default:
                    strMonth = "Invalid Month";
            }
            System.out.println("Month " + month + " is:" + strMonth);

        } catch (Exception e) {
            System.out.println(e.getMessage());
            e.printStackTrace();
        }
    }
}

Java program to find grade


import java.io.BufferedReader;
import java.io.InputStreamReader;

public class Grade {
    public static void main(String[] args) {
        InputStreamReader isr = new InputStreamReader(System.in);
        BufferedReader br = new BufferedReader(isr);
        try {
            System.out.println("Enter a valid score:");
            String input = br.readLine();
            int score = Integer.parseInt(input);
            String grade = "";
           /* if (score >= 90) {
                grade = "A+";
            } else if (score >= 80 && score < 90) {
                grade = "A";
            } else if (score >= 70 && score < 80) {
                grade = "A-";
            } else if (score >= 60 && score < 70) {
                grade = "B+";
            } else if (score >= 50 && score < 60) {
                grade = "B";
            } else if (score >= 40 && score < 50) {
                grade = "B-";
            } else {
                grade = "F";
            }*/

            if (score >= 90) {
                grade = "A+";
            } else if (score >= 80) {
                grade = "A";
            } else if (score >= 70) {
                grade = "A-";
            } else if (score >= 60) {
                grade = "B+";
            } else if (score >= 50) {
                grade = "B";
            } else if (score >= 40) {
                grade = "B-";
            } else {
                grade = "F";
            }
            System.out.println("score:" + score + " grade:" + grade);
        } catch (Exception e) {
            System.out.println(e.getMessage());
            e.printStackTrace();
        }
    }
}

Java program to calculate age

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.time.LocalDate;
import java.time.Period;

public class CalculateAge {
    public static void main(String[] args) {
        InputStreamReader isr = new InputStreamReader(System.in);
        BufferedReader br = new BufferedReader(isr);
        try {
            System.out.println("Enter year of birth:");
            String in = br.readLine();
            int year = Integer.parseInt(in);
            System.out.println("Enter month of birth:");
            in = br.readLine();
            int month = Integer.parseInt(in);
            System.out.println("Enter day of birth:");
            in = br.readLine();
            int day = Integer.parseInt(in);
            System.out.println("date of birth (mm-dd-yyyy):" + month + "-" + day + "-" + year);
            LocalDate today = LocalDate.now();
            LocalDate birthday = LocalDate.of(year, month, day);
            Period period = Period.between(birthday, today);
            System.out.println("Age:" + period.getYears() + " years "
                    + period.getMonths() + " months and " + period.getDays() + " days");
        } catch (Exception e) {
            System.out.println(e.getMessage());
            e.printStackTrace();
        }
    }
}

Java program to check whether the numbers are pythagorean triple or not

A Pythagorean triple consists of three positive integers x, y, and z, such that x2 + y2 = z2. Such a triple is commonly written (x, y, z), and a well-known example is (3, 4, 5) , i.e 32 + 42 = 9 + 16 = 25 = 52. If (x, y, z) is a Pythagorean triple, then so is (kx, ky, kz) for any positive integer k. A primitive Pythagorean triple is one in which x, y and z are coprime. A right triangle whose sides form a Pythagorean triple is called a Pythagorean triangle.

// java program to check whether the numbers are pythagorean triple or not
import java.io.BufferedReader;
import java.io.InputStreamReader;

public class PythagoreanTriple {
    public static void main(String[] args) {
        InputStreamReader isr = new InputStreamReader(System.in);
        BufferedReader br = new BufferedReader(isr);
        try {
            System.out.println("Enter number1:");
            String input = br.readLine();
            int a = Integer.parseInt(input);
            System.out.println("Enter number2:");
            input = br.readLine();
            int b = Integer.parseInt(input);
            System.out.println("Enter number3:");
            input = br.readLine();
            int c = Integer.parseInt(input);
            System.out.println("You Entered:" + a + " " + b + " " + c);
            if ((Math.pow(a, 2) + Math.pow(b, 2) == Math.pow(c, 2))
                    || (Math.pow(b, 2) + Math.pow(c, 2) == Math.pow(a, 2))
                    || (Math.pow(a, 2) + Math.pow(c, 2) == Math.pow(b, 2))) {
                System.out.println("The Integers are pythagorean triple");

            } else {
                System.out.println("The Integers are not pythagorean triple");
            }

        } catch (Exception e) {
            System.out.println(e.getMessage());
            e.printStackTrace();
        }
    }
}

Count Digits

You are given a string S. Count the number of occurrences of all the digits in the string S.

 

 

#include <iostream>
using namespace std;

int main()
{ int a = 0, b = 0, c = 0, d = 0, e = 0,f=0,g=0,h=0,i=0,j=0;
    string s;
    // Read given string from STDIN
    cin >> s;

    int s_len = s.length();
    // Iterate over each character in the string
    for(int k=0; k<s_len; k++) {

        // This can be done in a better way using hashing, which simplifies the implementation,
        // however for the purpose of this article we'll restrict the implementation to naive way

        // Check for each character in if else
        if(s[k] == '0') {
            a++;
        } else if(s[k] == '1') {
            b++;
        } else if(s[k] == '2') {
            c++;
        } else if(s[k] == '3') {
            d++;
        } else if(s[k] == '4') {
            e++;
        }
        else if(s[k] == '5') {
            f++;
        }
        else if(s[k] == '6') {
            g++;
        }
        else if(s[k] == '7') {
            h++;
        }
        else if(s[k] == '8') {
            i++;
        }
        else if(s[k] == '9') {
            j++;
        }
    }
    // Print out the result to STDOUT
    cout << "0 " << a << endl;
    cout << "1 " << b << endl;
    cout << "2 " << c << endl;
    cout << "3 " << d << endl;
    cout << "4 " << e << endl;
    cout << "5 " << f << endl;
    cout << "6 " << g << endl;
    cout << "7 " << h << endl;
    cout << "8 " << i << endl;
    cout << "9 " << j << endl;
    
    return 0;
}

Life, the Universe, and Everything

Your program is to use the brute-force approach in order to find the Answer to Life, the Universe, and Everything. More precisely… rewrite small numbers from input to output. Stop processing input after reading in the number 42. All numbers at input are integers of one or two digits.

//import for Scanner and other utility classes*/
import java.util.*;

class TestClass {
public static void main(String args[] ) throws Exception {
/*
* Read input from stdin and provide input before running
* Use either of these methods for input

//BufferedReader
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String line = br.readLine();
int N = Integer.parseInt(line);*/

//Scanner
Scanner s = new Scanner(System.in);

while(s.hasNextInt()){
int n=s.nextInt();
if(n==42)
break;
else
System.out.println(n);
}

}
}

Roy and Profile Picture

Roy wants to change his profile picture on Facebook. Now Facebook has some restriction over the dimension of picture that we can upload.
Minimum dimension of the picture can be L x L, where L is the length of the side of square.

Now Roy 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, user is prompted to upload another one. Print “UPLOAD ANOTHER” in this case.
[2] If width and height, both are large enough and
(a) if the photo is already square then it is accepted. Print “ACCEPTED” in this case.
(b) else 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:
First line contains L.
Second line contains N, number of photos.
Following N lines each contains two space separated integers W and H.

Output:
Print appropriate text for each photo in a new line.

Constraints:
1 <= L,W,H <= 10000
1 <= N <= 1000

/* IMPORTANT: Multiple classes and nested static classes are supported */

/*
 * uncomment this if you want to read input.
//imports for BufferedReader
import java.io.BufferedReader;
import java.io.InputStreamReader;*/

//import for Scanner and other utility classes
import java.util.*;


class TestClass {
    public static void main(String args[] ) throws Exception {
        /*
         * Read input from stdin and provide input before running
         * Use either of these methods for input

        //BufferedReader
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String line = br.readLine();
        int N = Integer.parseInt(line);*/

        //Scanner
        Scanner s = new Scanner(System.in);
        int L = s.nextInt();
        int N = s.nextInt();

        for (int i = 0; i < N; i++) {
            int W=s.nextInt();
            int H=s.nextInt();
        
        if(W<L || H<L){
           System.out.println("UPLOAD ANOTHER"); 
        }
        else if((L <= W)&&(L <= H)){
            if(W == H){
System.out.println("ACCEPTED");
}
else {

        System.out.println("CROP IT");
    }
    }
    }
    }
}

Count Divisors

You have been given 3 integers l, r and k. Find how many numbers between l and r
(both inclusive) are divisible by k. You do not need to print these numbers, you just have to find their count.

Input Format
The first and only line of input contains 3 space separated integers l, r and k.

Output Format
Print the required answer on a single line.

Constraints
1lr1000

1k1000

 


//import for Scanner and other utility classes
import java.util.*;

class TestClass {
    public static void main(String args[] ) throws Exception {
     
        //Scanner
        Scanner s = new Scanner(System.in);
        int l = s.nextInt();
        int r = s.nextInt();
        int k = s.nextInt();
        int count=0;

        for (int i = l; i <=r; i++) {
            if(i%k==0){
                count++;
            }
        }


        System.out.println(count);
    }
}

Polar Coordinates

Task
You are given a complex z. Your task is to convert it to polar coordinates.

Input Format

A single line containing the complex number z.

Output Format

Output two lines:
The first line should contain the value of r.
The second line should contain the value of Φ.

Sample Input

  1+2j

Sample Output

 2.23606797749979 
 1.1071487177940904


import cmath
complex_number=complex(input())
print(abs(complex_number))
print(cmath.phase(complex_number))

Find Product

You have been given an array A of size N consisting of positive integers. You need to find and print the product of all the number in this array Modulo 10^9+7

.

Input Format:
The first line contains a single integer N

denoting the size of the array. The next line contains N

space separated integers denoting the elements of the array

Output Format:
Print a single integer denoting the product of all the elements of the array Modulo 10^9+7

.

Constraints:
1N10^3
1A[i]10^3

 

import java.util.Scanner;

public class FindProduct {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
    long arr[] = new long[n];

    long M = 1000000007;
    long answer = 1;

    for (int i = 0; i <n ; i++){
arr[i] = sc.nextLong();
answer = (answer * arr[i]) % M;

}
System.out.println(answer);
}

}