You are on page 1of 7

PART A

(PART A : TO BE REFFERED BY STUDENTS)

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:

Command Line Parameters

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)

Roll. No. 48 Name: Nikhil Aher


Class: SE , Div-B Batch: B3
Date of Experiment: 10/08/2022 Date of Submission: 13/08/2022
Experiment No.: 2 Grade:

B.1 Software Code written by student:


B.2 Input and Output:

B.3 Observations and learning:


We learned how to take input through command line arguments and to convert
taken input from string to integer data type.
B.4 Conclusion:
We can take input from the user from methods more than using scanner class
and buffered reader class as we performed in the previous experiment.
B.5 Question of Curiosity
1. What will be the output of the following program?

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

2. What is use of Operator?


Ans :- An operator is used to manipulate individual data items and return a
result.

3. What will be the output of the following program?

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

5. 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 :- true

You might also like