Swapnanil Dutta PCC-CS593
Swapnanil Dutta PCC-CS593
Swapnanil Dutta PCC-CS593
1.
Question: Write a program to print your name.
Code:
Output:
2.
Question: Write a program to read the price of an item in the decimal form (like 75.95)
and print the output in paise (like 7595 paise).
Code:
public class ToPaisa {
public static void main(String[] args) {
double price = Double.parseDouble(args[0]);
System.out.println("Price: " + (int) (price * 100) +"
Paise");
}
}
Output:
3.
Question: Write a program to convert the given temperature in Fahrenheit to Celsius
using the following conversion formula:
C = (F-32)/1.8
Code:
public class TempConvert{
public static void main(String[] args) {
double fahrenheit = Double.parseDouble(args[0]);
double celcius = (fahrenheit - 32) / 1.8;
System.out.println("Celcius: " + celcius);
}
}
Output:
4.
Question: Write a program to determine sum of the following series for given value of n:
(1 + 1/2 + 1/3 + ...... + 1/n). Print the result up to two decimal places.
Code:
public class SumOfSeries {
public static void main(String[] args) {
int n = Integer.parseInt(args[0]);
double series = 0.0;
for (int i = 1; i <= n; i++)
series += 1.0 / i;
System.out.println("Series: " + String.format("%.2f",
series));
}
}
Output:
5.
Question: Write a program to find the sum of digits and reverse of a given integer
number (take input using command-line argument).
Code:
class Operations {
int reverse(int n) {
int rev = 0;
int copy = n;
while (copy != 0) {
rev = rev * 10 + (copy % 10);
copy /= 10;
}
return rev;
}
int sumOfDigits(int n) {
int sum = 0;
while (n != 0) {
sum += n % 10;
n /= 10;
}
return sum;
}
}
Code:
public class Facto {
public static int facto(int n) {
if (n == 0)
return 1;
return n * facto(n - 1);
}
public static void main(String[] args) {
int n = Integer.parseInt(args[0]);
System.out.println("Factorial: " + facto(n));
}
}
Output: