Skip to main content

Java User Input Exercise


Exercise 1:

Write a simple java program to take user input of a number and store it inside an integer variable. Later print the number on the screen.

 

Solution 1:

 

import java.util.Scanner;

 

public class UserInpur {

 

     public static void main(String[] args) {

 

 

          Scanner sc = new Scanner(System.in);

          System.out.println("Enter a number");

          int x = sc.nextInt();

          System.out.println("You have entered:");       

          System.out.println(x);

 

     }

 

}

 

Exercise 2:

Write a simple java program to take user input of two integer values and find out the large & small number.

 

Solution 2:

 

import java.util.Scanner;

import java.lang.*;

 

public class UserInpur {

 

     public static void main(String[] args) {

 

 

          Scanner sc = new Scanner(System.in);

          System.out.println("Enter 1st number");

          int x = sc.nextInt();

          System.out.println("Enter 2nd number");

          int y = sc.nextInt();

          System.out.println("The large number is "+Math.max(x,y));      

          System.out.println("The small number is "+Math.min(x,y));      

 

     }

 

}

 

Exercise 3:

Write a simple java program to take user input of some integer values and store them inside an array. Later print those values on the screen. Make sure the size of the array is also defined by a user input.

 

Solution 3:

import java.util.Scanner;

import java.lang.*;

 

public class UserInpur {

 

     public static void main(String[] args) {

 

 

          Scanner sc = new Scanner(System.in);

          System.out.println("How many numbers do you want to enter?");

          int size = sc.nextInt();

          int [] numbers = new int[size];

        

          for (int i=0;i<size;i++) {

              System.out.println("Enter number "+(i+1));

              numbers[i]=sc.nextInt();

          }

        

          System.out.println("*******************");

          System.out.println("Numbers you entered:");

          System.out.println("*******************");

          for (int i=0;i<size;i++) {

               System.out.println("Number "+(i+1)+" is:"+numbers[i]);

          }

 

     }

 

}

 

Exercise 4:

Write a simple java program to take user input of some integer values and store them inside an array. Now print the largest number on the screen. Make sure the size of the array is also defined by a user input.

 

Solution 4:

 

import java.util.Scanner;

import java.lang.*;

 

public class UserInpur {

 

     public static void main(String[] args) {

 

 

          Scanner sc = new Scanner(System.in);

          System.out.println("How many numbers do you want to enter?");

          int size = sc.nextInt();

          int [] numbers = new int[size];

         

          for (int i=0;i<size;i++) {

              System.out.println("Enter number "+(i+1));

              numbers[i]=sc.nextInt();

          }

         

          for (int i=0;i<size;i++) {

             

              if(i==(size-1)) {

                   System.out.println("The Largest Number is "+numbers[i]);

              }else {

                   if(numbers[i]>numbers[i+1]) {

                        int temp = numbers[i+1];

                        numbers[i+1] = numbers[i];

                   }

              }

          }

         

         

     }

 

}

   


Comments

Popular posts from this blog

What is Java

Java is a general purpose programming language. Which means this programming language is used in many purposes apart from being used only in android application development. There are a few similarities between Java programming language and C++ programming language. But Java programming language is having some advanced features which you are going to learn in future. In programming languages you write programs in the form of statements in many lines and those statements are executed one by one. In Java, the execution happens really fast and concurrently it can execute many statements. Java is a platform independent (Java compiled byte code can be run across different operating systems) and object oriented programming language.   Why should we learn Java programming language?   Easy to learn: Java is a easy to learn programming language. Once you start learning Java programming, you will actually be learning the real programming and you will observe everyth...

Java Operators

In this lesson you are goin g to learn about operators in Java and also what are the different types of operators used in Java programming.   In our previous lesson we have learnt how to take user input in Java. We were performing different types of arithmetic operations in order to create a simple ration shop billing application which took different values as the user input and calculated total billing amount.   During the programming the billing application we used different signs like addition (+) , multiplication (*) etc. These signs are called operators in Java programming.   Types of Operators in Java There are five types of operators in Java. Arithmetic Operators Bitwise Operators Assignment Operators Comparison Operators Logical Operators 1.Java Arithmetic Operators Arithmetic operators are used to perform mathematical operations such addition, division, multiplication, subtraction and modulation. Let’s see the example of arithmetic operators in below:   ...