Suppose someone tries to store into a single integer a student number and a stud
ID: 3861736 • Letter: S
Question
Suppose someone tries to store into a single integer a student number and a student percentage score for a certain exam. For example, the integer 10085 might be used to store the fact that student number 100 received the value 85 on a certain exam. For example, the integer 876597 might be used to store the fact that student number 8765 received the value 97 on a certain exam. Assuming no one scored more than 99 on a certain exam and assuming you will receive data with atleast three digits or more so the student number and percentage score always both exist, write code that reads in a single integer value and then breaks this packed integer into its two fields, the student number and the student percentage score.
Explanation / Answer
StudentNOPercentage.java
import java.util.Scanner;
public class StudentNOPercentage {
public static void main(String[] args) {
//Declaring variables
int number,student_no,marks,count=0,num;
//Scanner object is used to get the inputs entered by the user
Scanner sc=new Scanner(System.in);
/* This loop continues to execute until
* the user enters valid number
*/
while(true)
{
//Getting the number entered by the user
System.out.print("Enter the number :");
number=sc.nextInt();
num=number;
/* This loop will check whether the
* user entered number is having atleast 3 digits
*/
while(num>0)
{
num=num/10;
count++;
}
if(count>=3)
{
break;
}
else
{
//If user entered number is less than 3 digits the display error message
System.out.println("Invalid.Must be atleast 3 Digits ** ");
continue;
}
}
//getting the student number
student_no=number/100;
//Getting the marks
marks=number%100;
//Displaying the student number
System.out.println("Student Number:"+student_no);
//Displaying the marks
System.out.println("Marks:"+marks);
}
}
__________________
Output:
Enter the number :95
Invalid.Must be atleast 3 Digits **
Enter the number :10085
Student Number:100
Marks:85
____________Thank You
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.