Pencil Count Program in Java Solution

Question:-

Nila uses pencils to write at school and at home. When she was in her 1st standard, her parents bought her one pencil. In her 2nd standard, she needed 5 pencils, and in 3rd standard, she needed 14 pencils and so on. How many pencils does she get from the provided standard?

Note : If the input given is not between 1 to 12  then the output should be “Invalid Standard”

Sample Input 1:
Enter the standard: 4
Sample Output 1:
Nila gets 30 pencils

Sample Input 2:
Enter the standard: 5
Sample Output 2:
Nila gets 55 pencils

Sample Input 3:
Enter the standard:14
Sample Output 3:
Invalid Standard

Code:-
import java.util.*;
class Count{
    public static void main(String args[]){
        Scanner sc = new Scanner(System.in);
        int n, count=0;
        System.out.println("Enter the standard: ");
        n= sc.nextInt();
        if(n<1 || n>12){
            System.out.println("Invalid Standard");
        }
        else{
            count = (n*(n+1)*(2*n+1))/6;
            System.out.println("Nila gets "+count+" pencils");
        }
    }
}

Leave a Comment

Scroll to Top