EduLearn - Online Education Platform

Welcome to EduLearn!

Start learning today with our wide range of courses taught by industry experts. Gain new skills, advance your career, or explore new interests.

Browse Courses

A company has installed a smart elevator system in its office building. The elevator operates based on the following rule.

The elevator can move between 1st to 10th floor

Users can enter their desired floor,but if the floor is outside the range,the system should print “Invalid floor”

The elevator starts from 1st floor and moves to the requested floors one by one,printing each floor it reaches

If the user enters 0, the program should exit with “elevator stopped”

import java.util.*;

 

class ASC

{

public static void main(String args[])

{

Scanner s=new Scanner(System.in);

 

int current=1;

 

while(true)

{

System.out.println(“Enter the floor from 1-10/0 to stop:”);//4

int floor=s.nextInt();//4

 

if(floor==0)//4==0

{

System.out.println(“Elevator stopped”);

break;

}

 

else if(floor>10)//4>10

{

System.out.println(“Invalid floor”);

}

 

else

{

if(floor>current)//4>1

{

for(int i=current+1; i<=floor; i++)//i=4;4<=4

{

System.out.println(“Moving to floor:”+i);//2 3 4

}

}

else if(floor<current)

{

for(int i=current-1; i>=floor; i–)

{

System.out.println(“Moving to floor:”+i);

}

}

else

{

System.out.println(“you are already on floor”+current);

}

 

current=floor;

}

}

 

}

}

Java practice programs

Leave a Reply

Your email address will not be published. Required fields are marked *

EduLearn - Online Education Platform

Welcome to EduLearn!

Start learning today with our wide range of courses taught by industry experts. Gain new skills, advance your career, or explore new interests.

Browse Courses

Popular Courses

[Course Image]

Introduction to Programming

Learn the fundamentals of programming with Python in this beginner-friendly course.

12 Hours Beginner
[Course Image]

Data Science Essentials

Master the basics of data analysis, visualization, and machine learning.

20 Hours Intermediate
[Course Image]

Web Development Bootcamp

Build modern websites with HTML, CSS, JavaScript and popular frameworks.

30 Hours Beginner
[Course Image]

Digital Marketing Fundamentals

Learn SEO, social media marketing, email campaigns and analytics.

15 Hours Beginner
Educational Website Footer