Swapnanil Dutta PCC-CS593

Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

Swapnanil Dutta Roll 12

1.
Question: ​Write a program to print your name.
Code:

public​ ​class​ ​name​{


​public​ ​static​ ​void​ ​main​(​String​[] ​args​) {
System.out.​println​(​"Swapnanil Dutta"​);
}
}

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

public​ ​class​ ​SumAndRev​{


​public​ ​static​ ​void​ ​main​(​String​[] ​args​) {
​int​ n ​=​ Integer.​parseInt​(args[​0​]);
Operations obj ​=​ ​new​ ​Operations​();
System.out.​println​(​"Reverse: "​ ​+​ obj.​reverse​(n));
System.out.​println​(​"Sum: "​ ​+​ obj.​sumOfDigits​(n));
}
}
Output:
6.
Question: ​Write a program to find the factorial of a given integer number using recursion
(take input using command-line argument).

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:

You might also like