OOPM JAVA - Print the first Armstrong number starting from your roll no.

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: 

Students have used if-else and while loop to implement and solved above problem.

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.

  • if statement
  • if-else statement
  • nested if statement
  • 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

  • Start
  • Declare integer type a,b,c
  • a=(Integer.parseInt)arge[0]
  • b=(Integer.parseInt)args[1]
  • if (a>10)
  • c=a+b;
  • Display c
  • Stop


CODE :

public class Armstrong{

  public static void main(String[] args) 

   {

    int c = 0, a, temp, n;

    int t;

    t = 36; //It is the number to check armstrong number after it

    do 

    {

      c = 0;

      n = t;

      temp = n;

      while (n > 0) 

      {

        a = n % 10;

        n = n / 10;

        c = c + (a * a * a);

      }

      t++;

    } while (temp != c);

    System.out.print("First armstrong number after your roll no. is "+c);

  }

}


Output:

(App used for compiling: Dcoder

Dcoder is a very simple and useful mobile app which has most of the languages in it. So give this app a try if you want to learn coding.)


Observations and learning: 

In this program we have seen that when user inputs the the number via command line arguments, then it is checked by the compiler that it is an armstrong number or not if it is not an Armstrong number then it will display the next Armstrong number.


Conclusion: 

Input given by the user via Command Line Argument is checked in this program and displayed that the number is Armstrong number or it will display next Armstrong number.


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

        

    }

}

OUTPUT:- 

       2 is greater than 1


2. What is use of ? : Operator?

The ternary operator can be thought of as a simplified version of the if-else statement with a value to be 

returned.The ternary operator is a part of Java’s conditional statements. 

The syntax of ternary operator is 

variable var = (booleanExpression) ? value1 if true : value2 if false 

EXAMPLE:- 

public static void main( String args[] ) {

int number = 3; 

 String msg = (number % 2 == 0) ? "The number is even!" : "The number is odd!"; 

 System.out.println(msg); 

 } 

}


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

        

    }

}

OUTPUT:-

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

        }

    }

}

OUTPUT:- 

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

        }

    }

}

OUTPUT:- 

true

 

Similar Programs

Write a program to find maximum between two numbers.

Write a program to find maximum between three numbers.

Write a program to check whether a number is even or odd.

Write a program to check whether a year is leap year or not.

Write a program to check whether a number is negative, positive or zero.

Write a program to check whether a number is divisible by 5 and 11 or not.

Write a program to count total number of notes in given amount.

Write a program to check whether a character is alphabet or not.

Write a program to input any alphabet and check whether it is vowel or consonant.

Write a program to input any character and check whether it is alphabet, digit or special character.


Comments