Java : Getting User Input

 


package com.company;
import java.util.Scanner;

public class Taking_Input{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Taking Input from User");
System.out.println("Enter First Number");
int a = sc.nextInt();
System.out.println("Enter second number");
int b = sc.nextInt();
int sum = a+b;
System.out.println(sum);

System.out.println("Taking user name");
String str = sc.next();
System.out.println(str);

boolean bool = sc.hasNextInt();
System.out.println(bool);

}
}

Exercise :- 

1. Write a program to calculate the percentage of a given students in CBSC board exam . His
Marks from 5 subjects must be taken as input from the keyboard (Marks are out of 100)
import java.util.Scanner;

public class Excercise{
public static void main(String[] args) {
Scanner sc =
new Scanner(System.in);
System.out.println("Enter The Marks which u get in your CBSE Board");
System.out.println("Enter The Marks of Hindi");
int a = sc.nextInt();
System.out.println("Enter The Marks of English");
int b = sc.nextInt();
System.out.println("Enter The Marks of Maths");
int c = sc.nextInt();
System.out.println("Enter The Marks of Science");
int d = sc.nextInt();
System.out.println("Enter The Marks of Computer");
int e = sc.nextInt();
System.out.println("The Total numbers of subjects are :");
int sum = a+b+c+d+e;
System.out.println(sum);
System.out.print("Percentage = ");
float f = sc.nextFloat();
float per = sum/f;
System.out.print(per);
System.out.print("%");
}
}