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]; } } } } } |
No comments: