Java program to count the number of pencils for student

Java program to count the number of pencils for student. Thilagar writes with pencils at school and at home. His parents purchased her one pencil while he was in first grade. He required 5 pencils in her second grade, 14 pencils in his third grade, and so on. From the provided standard, how many pencils does he get?

import java.util.Scanner;
public class Main
{
public static void main (String[] args) {
int penc=0;
Scanner sc=new Scanner(System.in);
System.out.println("Enter the student's Standard");
int stan=sc.nextInt();
if(stan<1||stan>12)
{
System.out.println("invalid standard or above 1 and below 12");
}
else
{
for(int i=1;i<=stan;i++)
{
penc+=i*i;
}
System.out.println("Thilagar gets "+penc+" pencils");
}
}
}

 

Output:

Enter the student’s Standard
33
invalid standard or above 1 and below 12

What is logical OR in java program and how to implement

|| is a logical operator that can be read as “OR OR” or “Logical OR.” This operator is used to conduct a “logical OR” operation, which is comparable to the OR gate function in digital electronics.

If the first condition is true, the logical || operator does not examine the second condition. It only tests the second condition if the first is false. Whether the first condition is true or false, the bitwise | operator always verifies both criteria.

What is for loop and how to execute in java

Initialization happens first and only once in a for loop, which means that the initialization component of the for loop only runs once. Second step: Each iteration of the for loop evaluates the condition; if the condition is true, the statements within the for loop body are performed.

What is if else statement in java

The conditional statements in Java are as follows: If a particular condition is true, use if to specify a block of code to be run. If the same condition is false, use otherwise to specify a block of code to run. If the initial condition is false, use else if to define a new condition to test.