Page 65 - CA_Blue( J )_Class10
P. 65
{
count=count+1;
n=n/10;
}
System.out.println("Number of digits are:" + count);
}}
Output: Number of digits are: 3
In the above program, the counter variable counts the number of digits in the number "n". As long as the while loop
is true, the loop continues and the counter increases by 1. So, the output is Number of digits are: 3.
ii. Accumulator: Its working is similar to a counter, but the increment value is different for each recurrence of the loop.
It should always be initialized to avoid a garbage value during execution. For example:
class use_of_accumulator
{
public static void main(String args[])
{
int acc=0,i=1;
do
{
acc=acc+i;
i=i+1;
}while(i<=10);
System.out.println("Total value :" + acc);
}
}
Output: Total value:55
In the above program, as the value of i increases by 1, the accumulator (acc) variable adds the value of i. Thus for
each repetition different value of i is accumulated to acc. So, the output is Total value: 55.
4.3.2 Relational Operators
Relational operators compare two variables or expressions and return a boolean value either true or false as the result.
They work on numbers and characters. But they are not applicable to string or boolean types. There are six relational
operators in Java.
Name of Example
Operator Description Output
Operator int a = 10, b = 2
> Greater Than It checks whether the left operand is greater than the a>b true
right operand. If yes, then returns true otherwise false.
>= Greater than or It checks whether the left operand is greater than or a>=b true
Equal To equal to the right operand. If yes, then returns true
otherwise false.
< Less Than It checks whether the left operand is less than the right a<b false
operand. If yes, then returns true otherwise false.
<= Less Than or Equal It checks whether the left operand is less than or equal a<=b false
To to the right operand. If yes, then returns true otherwise
false.
== Equal To It checks whether the left operand is equal to the right a==b false
operand. If yes, then returns true otherwise false.
!= Not Equal To It checks whether the left operand is not equal to the a!=b true
right operand. If yes, then returns true otherwise false.
63
Operators in Java 63

