Page 215 - Computer science 868 Class 12
P. 215
Ans. int i, j;
for(i=1,j=1; i<=5; i++,j++)
{
System.out.println(i+j);
}
2. Convert the following into if else.
int discount=(price>=10000)?(price-(price*5.0/100.0)):(price-100);
Ans. int discount;
if(price>=10000)
discount=(price-(price*5.0/100.0));
else
discount=(price-100);
3. How many times the following loop will execute?
int i = 1;
while (i < 10)
if (i++ % 2 == 0)
System.out.println(i);
Ans. 4 times
4. Give the output of the following program code.
int i=-12;
do
{
System.out.println(i);
i=i+2;
}while(i<0);
Ans. Output
-12
-10
-8
-6
-4
-2
5. What is being calculated in the following program?
int num = sc.nextInt();
int s=0;
for(;num>0;num/=10)
{
s=s+num%10;
}
System.out.println("Result : "+s);
Ans. Sum of the digits of the number in the variable “num”.
6. Convert the following do while loop into a while loop.
int i=20;
do
{
System.out.println(i*2);
i=i-2;
213
Statements and Scope 213

