Tag Archives: Java

Java program to convert celsius to fahrenheit

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.text.DecimalFormat;

public class CelsiusToFahrenheit {
    public static void main(String[] args) {
        System.out.println("This program will convert temperature in celsius into fahrenheit");
        InputStreamReader isr = new InputStreamReader(System.in);
        BufferedReader br = new BufferedReader(isr);
        try {
            System.out.println("Enter Temperature in Celsius:");
            String input = br.readLine();
            float tempC = Float.parseFloat(input);
            float tempF = tempC * 9 / 5 + 32;
            DecimalFormat df = new DecimalFormat("0.00");
            System.out.println("Temperature in Fahrenheit:" + df.format(tempF));

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

        }
    }
}

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);
    }
}