Pencil Count Program in java

 

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.Scanner;
public class Count 
{
    public static void main (String[] args) {
        int penc=0;
        Scanner sc=new Scanner(System.in);
        System.out.println("Enter the standard");
        int stan=sc.nextInt();
        if(stan<1||stan>12)
        {
            System.out.println("Invalid Standard");
        }
        else
        {
            for(int i=1;i<=stan;i++)
            {
                penc+=i*i;
            }
            System.out.println("Nila gets "+penc+" pencils");
        }
    }
}
Previous
Next Post »