B48 Java Exp2
B48 Java Exp2
Experiment No. 02
Aim: Write a program to check and print the first Armstrong number starting from your roll
no. Take input from the command line arguments.
Objective: To make the students aware of control structure and loops
Objective:
1. Students have used if-else and while loop to implement and solved above problem
2. Students are exposed to reading of input data from consol through command line.
Theory:
Command Line Parameter
The java command-line argument is an argument i.e. passed at the time of running the java
program. The arguments passed from the console can be received in the java program and it
can be used as an input.
When an application is launched, the runtime system passes the command-line arguments to
the application's main method via an array of String. If an application needs to support a
numeric command-line argument, it must convert a String argument that represents a number.
All of the Number classes — Integer, Float, Double, and so on — have parseXXX methods that
convert a String representing a number to an object of their type.
Java If-else Statement
The Java if statement is used to test the condition. It returns true or false. There are various
types of if statement in java.
o if statement
o if-else statement
o nested if statement
o if-else-if ladder
if Statement
The if statement tests the condition. It executes the if statement if condition is true.
Syntax:
if(condition){
//code to be executed
}
if-else statement
if (TestExpr)
{
stmtT;
}
else
{
stmtF; }
if-else-if ladder
if (TestExpr1){
stmtT1;}
else if(TestExpr2){
stmtT2;}
else if(TestExpr3){
stmtT3;
.. .}
else if(TestExprN){
stmtTN;}
else{
stmtF;}
Algorithm:
1. Start
2. Declare integer type a,b,c
3. a=(Integer.parseInt)args[0]
4. b=(Integer.parseInt)args[1]
5. if (a>10)
6. c=a+b;
7. Display c
8. Stop
While(true)
{
no = roll_no
sum=0;
while (no != 0)
{
digit = no % 10;
sum += digit*digit*digit
no /= 10
}
if(roll_no == sum)
break;
roll_no++;
}
abc = a3+b3+c3
PART B
(PART B : TO BE COMPLETED BY STUDENTS)
(Students must submit the soft copy as per following segments within two hours of the
practical. The soft copy must be uploaded at the end of the practical)
class IfExample
{
public static void main(String s[])
{
if( 1 > 2 )
{
System.out.println(" 1 is greater than 2");
}
else
System.out.println(" 2 is greater than 1");
}
}
Ans :- 2 is greater than 1
class IfExample
{
public static void main(String s[])
{
if( 1 < 2 )
{
System.out.println("1 is less than 2");
}
else
System.out.println("2 is less than 1");
System.out.println("Hello From IfExample");
}
}
Ans :- 1 is less than 2
Hello From IfExample
4. What will be the output of the following program?
public class ShortTest {
public static void main(String[] args) {
boolean x = true;
boolean y = false;
if (x && y) {
System.out.println(true);
} else {
System.out.println(false);
}
}
}
Ans :- false
public class ShortTest {
public static void main(String[] args) {
boolean x = true;
boolean y = false;
if (x || y) {
System.out.println(true);
} else {
System.out.println(false);
}
}
}
Ans :- true